How can we get the values of dynamic queries into local variables?

  •  Can some one tell me that how can i get the multiple or single value results of dynamic query into a local variables.

    e.g in following codes i want to store the values of fname into local variables

    CODE 1:

    Create Procedure Getnames @tableName VARCHAR(100)

    AS

    BEGIN

    DECLARE @Query VARCHAR(5000)

    Set @Query = 'SELECT fname FROM ' + @tableName

    Execute @Query

    END

     

    CODE 2:

    Create Procedure GetName @tableName VARCHAR(100), @id INT

    AS BEGIN DECLARE @Query VARCHAR(5000)

    SET @Query = 'SELECT fname FROM ' + @tableName + ' WHERE id = ' + @id

    Execute @Query

    END

  • Please refrain from posting the same question multiple times. See my reply here: http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=5&messageid=162243

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply