Creating a stored procedure in SQL
I need to create a stored procedure in SQL where the clause WHERE is a parameter. Is this possible? If yes, how?

Software/Hardware used:
ASKED: October 13, 2008  6:50 PM
UPDATED: April 8, 2010  4:03 PM

Answer Wiki:
This can be done using dynamic SQL. <pre>CREATE PROCEDURE YourProcedure @WhereColoum sysname, @WhereValue varchar(20) AS DECLARE @SQL nvarchar(4000) SET @SQL = 'SELECT * FROM YourTable WHERE ' + @WhereColoum + ' = ''' + @WhereValue + '''' exec (@SQL)</pre>
Last Wiki Answer Submitted:  October 14, 2008  5:35 am  by  Denny Cherry   64,520 pts.
All Answer Wiki Contributors:  Denny Cherry   64,520 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Thanks for posting this. How about creating a stored procedure with a parameter for the table? In other words, the table will be named something different each time but the query remains the same.

 175 pts.

 

The concept is the same.

In this case the complete SELECT statement is a string constructed in the procedure, so any part of it could be variable. You would just need to send the table name to the procedure as a parameter, and concatenate it where appropriate.

 63,535 pts.

 

Thank you Carlos. Can you give me a script example?

 175 pts.

 

While we’re at it, do you think you could help with another post of mine? Here is what I asked?

“I want to have a stored procedure run on a schedule that automatically creates a database output file as a result and saves it to a specific location on my network. Can someone please tell me how to do this?”
Thanks again for your help with this.

 175 pts.

 

Here’s the modified example:

CREATE PROCEDURE YourProcedure
       @TableName sysname,
       @WhereColoum sysname,
       @WhereValue varchar(20)
 AS
 DECLARE @SQL nvarchar(4000)
 SET @SQL = 'SELECT *
 FROM ' + @TableName +
 ' WHERE ' + @WhereColoum + ' = ''' + @WhereValue + ''''
 exec (@SQL)
 63,535 pts.

 

Thanks Carlos. I’m getting an error message. Will this code work in TSQL? I’m getting a syntax error at “SET @SQL = ‘SELECT *”

 175 pts.

 

Yes, it is T-SQL code.

Did you copy-pasted it ? If so, try replacing the quotes, as this editor converts them to other similar (but incorrect) character.

Here’s the code again (without using the code tool, and with a minor correction at the end of the sql string):

CREATE PROCEDURE YourProcedure
@TableName sysname,
@WhereColoum sysname,
@WhereValue varchar(20)
AS
DECLARE @SQL nvarchar(4000)
SET @SQL = ‘SELECT * FROM ‘ + @TableName +
‘ WHERE ‘ + @WhereColoum + ‘ = “‘ + @WhereValue + ‘”‘
exec (@SQL)

 63,535 pts.

 

Thank you. That fixed it. It was just a matter of the text editor.

 175 pts.

 

Carlos:
Can you please help with this one:
“I want to have a stored procedure run on a schedule that automatically creates a database output file as a result and saves it to a specific location on my network. Can someone please tell me how to do this?”

 175 pts.