Create parameter for a table in a procedure

  • 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

  • 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.

  • 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)


    Bru Medishetty

    Blog -- LearnSQLWithBru

    Join on Facebook Page Facebook.comLearnSQLWithBru

    Twitter -- BruMedishetty

  • Thanks for the quick responses. Did some more reading after I posted and understand a lot more about table value parameters.

  • You are welcome.

    These thing come slow

    just keep going.

  • No worries. You are welcome..


    Bru Medishetty

    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