views:

63

answers:

2

Hello,

I reading some old ScottGu's blogs on Linq2SQL. Now I'm doing the SPROC part. I'd like to know what's the exact meaning of @variable.

See this from ScottGu's Blog

ALTER PROCEDURE dbo.GetCustomersDetails
(
  @customerID nchar(5),
  @companyName nvarchar(40) output
)
AS
SELECT @companyName = CompanyName FROM Customers
WHERE CustomerID = @customerID

SELECT *
FROM Orders
WHERE CustomerID = @customerID
ORDER BY OrderID

I'm kind of lost as, so far, I've though of anything preceded by a '@' as a placeholder for user input. But, in the example above, it looks like '@companyName' is used as a regular variable like in C# for instance (SELECT @companyName = ...). But, @companyName is not known yet.

So, what the true nature a something preceded by a '@' like above? a vriable? a simple placeholder to accommodate user entered value?

Thanks for helping

+7  A: 

It is simply a variable.

Remember that stored procedures can have input and output parameters. @companyName in that case is a variable holding the value that will be output when the procedure GetCustomersDetails is called (note the output after the parameter declaration).

This procedure is also returning a result set in addition to the output parameter. You also have the option of setting a return code if you wish, so there are at least three ways of returning data from a stored procedure that can all be used at the same time: output parameters, result sets, and return codes.

RedFilter
@OrbMan: So If I understand what you mean, there is only 1 value that will be entered by the caller of the SPROC? [@companyName] --
Richard77
@Richard77, yes, there is only one input variable, but it is `@customerID`, not `@companyName`. `@companyName` is followed by `output`, that is how you know it is an output parameter.
RedFilter
@Richard77: So, you give the procedure a customer ID, and it tells you their company name.
RedFilter
But what about the SELECT * thing that follows? won't it rise an exception since the expected output value is a single company name (not rows of orders as that selct statement suggest)
Richard77
@Richard77: See my addition, above.
RedFilter
Thanks to all -- I think it's better to start a new thread for how to retrieve the value as I've got the answer to my original question.
Richard77
+1  A: 

see the inline comments:

ALTER PROCEDURE dbo.GetCustomersDetails
(
  @customerID nchar(5),            --input parameter to stored procedure
  @companyName nvarchar(40) output --output parameter to stored procedure, can be changed by the procedure and the value retrieved by the caller
)
AS
SELECT @companyName = CompanyName FROM Customers  --set the output parameter as the last row from this query
WHERE CustomerID = @customerID  --use the input parameter to filter the query

SELECT *
FROM Orders
WHERE CustomerID = @customerID  --filter this query on the input parameter 
ORDER BY OrderID

You can declare local variables, they are not just parameters of stored procedures:

DECLARE @localVariable int     --integer local variable
       ,@Another       char(1) --multiple on one DECLARE

usually @@.... are system values, like @@ROWCOUNT and @@SPID, but you can do crazy things like:

DECLARE @@@@wtf        int   --this is valid, and works
KM
@KM -- By the way, what the point of the last part, which starts with SELECT * ? is it conttected with the rest since there is only one Output expected -- @companyName --? won't it rise an excetion as only @companyName is expected to be output? very confusing.
Richard77
When you call a stored procedure, it can return various resultsets AND output variables, in particular, this stored procedure returns one variable AND a result set, containing the orders related to the customer
Jhonny D. Cano -Leftware-
@Jhonny D. Cano -- And How do I retrieve the value from that SPROC using LINQ? Something like var myOrder = Northwind.GetCustomersDetails(int @CustomerID) or var MyCustomerDetails = Northwind.GetCustomersDetails(int @CustomerID)? or maybe an objet in which the customer can be found along with the set of orders?
Richard77
Well I Don't know with linq, but at a ADO.Net level, you would add a SqlParameter to the SqlCommand, and set its direction as Input, Output or both, according to the stored procedure definition.
Jhonny D. Cano -Leftware-