How to get result in dynamically created SQL execution?

  • Example:

    declare @stmt varvhar(255)

    set @stmt = 'SELECT COUNT(*) FROM EMPLOYEE'

    EXECUTE (@stmt)

     

    The question is how to get the count from the above statement ?

    Thanks,

    Eric Sulistiawan

     

  • I am not sure exactly what you are attempting to achieve, but there are several possible solutions to your question.

    Does this Answer your question??

    declare @iCount int

    SELECT @iCount=COUNT(*) FROM EMPLOYEE

    Print @iCount

    Steve

  • See prior recent thread...

    http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=93526#bm93703

    I hope the address paste works ok, I used the new forum search with "sp_executesql" key word, and T-SQL as only section to search. Worked great



    Once you understand the BITs, all the pieces come together

  • declare @sqlstring nvarchar(1000), @table_name varchar(25)

    declare @result int

    set @table_name = 'pubs.dbo.authors'

    set @sqlstring = 'select @C = count(*) from ' + @table_name

    exec sp_executesql @sqlstring, N'@c INT OUTPUT', @result OUTPUT

    select @result

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

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

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