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 Cherry64,520 pts.
All Answer Wiki Contributors: Denny Cherry64,520 pts.
If you live outside the United States, by submitting your email address you consent to having your personal data transferred to and processed in the United States.
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.
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.
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.
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?”
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.
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.
Thank you Carlos. Can you give me a script example?
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.
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)Thanks Carlos. I’m getting an error message. Will this code work in TSQL? I’m getting a syntax error at “SET @SQL = ‘SELECT *”
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)
Thank you. That fixed it. It was just a matter of the text editor.
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?”