Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 11th, 2009, 03:46 PM
Authorized User
 
Join Date: Jan 2009
Posts: 23
Thanks: 10
Thanked 0 Times in 0 Posts
Default Scalar Variable when using a stored procedure

Could someone please help me!
I am trying to pull information out of my database using a stored procedure.

Code:
ALTER PROCEDURE GetParticipantbyParticipantId
AS
 
SELECT Participant.UserId, Participant.FirstName, 
Participant.LastName, aspnet_Users.UserName FROM 
Participant INNER JOIN aspnet_Users ON Participant.
UserId = aspnet_Users.UserId WHERE aspnet_Users.UserName =@UserName
RETURN 
The error message I am getting is "Must declare the scalar variable @UserName"

Could anyone advise me where and how I can declare this variable.

Basically, I want it to read it from the current logged in user - ie the result which is stored in "asp:LoginName#LoginName1"

I am pretty new to using procedures. I have been able to achieve a
result using the following WHERE clause in a sql statement stored in the .aspx page:
Code:
WHERE (aspnet_Users.UserName LIKE '%' + @UserName + '%')"
The following is what is stored in the code behind:

Code:
ProtectedSub SqlDataSource2_Selecting(ByVal sender AsObject, _
ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
Handles SqlDataSource2.Selecting
Dim userName AsString = Context.User.Identity.Name
e.Command.Parameters("@UserName").Value = userName
EndSub
ProtectedSub SqlDataSource1_Selecting(ByVal sender AsObject, _
ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
Handles SqlDataSource1.Selecting
Dim userName AsString = Context.User.Identity.Name
e.Command.Parameters("@UserName").Value = userName
EndSub
When I try to plug this into another page, I cannot get it to work. I need to be able to access user data from a number of pages, so I thought a procedure would be a standard way to do this. Could anyone give me some (simple) advice to get this working. Thanks in advance.
 
Old March 11th, 2009, 04:24 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default Adding parameters to sql server stored procedures

Hi Spider
You're very close.

As you have already worked out, when you run a plain SQL statement which uses parameters, such as
Code:
WHERE (aspnet_Users.UserName LIKE '%' + @UserName + '%')"
sql server essentially replaces the places where @UserName appears with the value you specify.

Stored procedures work slightly differently in that the parameter is passed as, well, a parameter to the stored procedure, which will itself do the replacement, in exactly the same sort of way you pass parameters to a method in VB. There is a fairly good step by step, if slightly long-winded, introduction to stored procedures and input variables at http://www.sql-server-performance.co...basics_p3.aspx

When the ASP.NET page calls the stored procedure, it only needs to send the name of the procedure and the values to give the parameters. So it actually runs something like
Code:
EXEC GetParticipantbyParticipantId param1Value, param2Value
So the question is how to tell the procedure what parameters to expect. To do this, you add them in a list after the name of the procedure and before the AS bit. So your one would look something like:
Code:
ALTER PROCEDURE GetParticipantbyParticipantId (
  @UserName VARCHAR(20)
) AS
 
SELECT Participant.UserId, Participant.FirstName, 
Participant.LastName, aspnet_Users.UserName FROM 
Participant INNER JOIN aspnet_Users ON Participant.
UserId = aspnet_Users.UserId WHERE aspnet_Users.UserName =@UserName
RETURN
Note that putting brackets round the list is optional, but I find it helps readability.

HTH
Phil

Last edited by philip_cole; March 11th, 2009 at 04:28 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Temp Table Variable in Stored Procedure rweide SQL Server ASP 2 April 15th, 2011 03:50 PM
Must declare the scalar variable stapes ASP.NET 2.0 Basics 1 August 15th, 2008 10:03 AM
Must declare the scalar variable kofibull Visual Studio 2005 0 November 24th, 2006 05:47 PM
"Must declare the scalar variable" Orchid85 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 September 22nd, 2006 10:56 AM
Passing Variable MS Proj to SQL Stored Procedure Abaxt Access VBA 2 July 7th, 2005 11:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.