February 2, 2012 at 12:29 pm
This seems really easy but I can't find any references to what I'm doing wrong.
Here is my code:
CREATE PROCEDURE dbo.QryAllTable (@TableName varchar(50))
AS
BEGIN
SELECT *
FROM @TableName
END
GO
The procedure won't create because it says I have invalid syntax on the From line. Can I not pass a string a string parameter to the From clause?
Thanks for the help.
TL
February 2, 2012 at 12:36 pm
It ill not work.
You are selecting from a STRING not from a table.
If are you trying do create a generic "select * from" using a parameter to pass the name of the table you can achieve it using dynamic SQL.
Read about it in BOL and try again.
February 2, 2012 at 12:40 pm
You will have to have a local string variable and pass the parameter value to that local variable and execute that..
in short, this is how it should look like..
DECLARE @sql VARCHAR(1000)
SET @sql = 'SELECT * FROM ' + @TABLENAME
EXEC (@SQL)
Blog -- LearnSQLWithBru
Join on Facebook Page Facebook.comLearnSQLWithBru
Twitter -- BruMedishetty
February 2, 2012 at 12:51 pm
Thanks for the quick responses. Did some more reading after I posted and understand a lot more about table value parameters.
February 2, 2012 at 1:00 pm
You are welcome.
These thing come slow
just keep going.
February 2, 2012 at 1:02 pm
No worries. You are welcome..
Blog -- LearnSQLWithBru
Join on Facebook Page Facebook.comLearnSQLWithBru
Twitter -- BruMedishetty
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply