Dynamic SQL

  •  Is it possible to replace this:

    DECLARE @sql varchar(255)

    DECLARE @tablename varchar(255)

    DECLARE @rcount int

    set @sql = 'Select COUNT(*) as COUNT into ##TEMPCOUNT from ' + @tablename

    EXECUTE sp_executesql @SQL

    SELECT @rcount = [COUNT] FROM ##TEMPCOUNT

    with:

    set @sql = 'Select ' + @rcount + ' = COUNT(*) as COUNT from ' + @tablename

    EXECUTE sp_executesql @SQL

    and eliminate the temporary table creation?

  • Not possible. The variable @rcount is out of scope in Dynamic SQL.

    May be steev's answers in this helps

    http://qa.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=202194#bm202408

    Regards,
    gova

  • Thanks a lot Govin.

     

    Steve's answer helped.

    This will do:

    set @sql = 'select @rcount = Count(*) from ' + @tablename

    exec sp_executesql @sql, N'@rcount int OUTPUT', @rcount OUTPUT

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

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