Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to pass an SQL Select statement with a variable to a stored procedure. But I'm getting the following error:

Must declare the scalar variable "@date".

Here is my Stored Procedure:

PROCEDURE [dbo].[sp_ExecuteQuery]
@query nvarchar(150),
@name nvarchar(10)
AS
EXEC (@query)


...and here is how I execute the Stored Procedure:

EXEC sp_ExecuteQuery SELECT * FROM sqltable WHERE name=@name,'john'

The stored procedure has two (2) parameters:

@query = SELECT * FROM sqltable WHERE name=@name
@name = 'john'

Notice that the sql statement has a parameter named @name. The value of this parameter will be retrieved from the variable @name inside the stored procedure (which is john).

Unfortunately, I got the above error. I just want to know if this is possible. Any help is highly appreciated. Thanks!


[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 2-Jul-12 23:27pm
v2
Comments
OriginalGriff 3-Jul-12 5:27am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

1 solution

you can write your SP as
SQL
Alter PROCEDURE [dbo].[sp_ExecuteQuery]
@query nvarchar(150),
@name nvarchar(10)
AS
EXEC (@query + @name)


and your execute statement will be like this
SQL
EXEC sp_ExecuteQuery 'SELECT * FROM sqltable WHERE name=','john'
 
Share this answer
 
v2
Comments
jacebeleren 3-Jul-12 5:32am    
Thanks for helping me out. Your solution works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900