Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. dynamic sql and store output in variable sql server2005

dynamic sql and store output in variable sql server2005

Scheduled Pinned Locked Moved Database
sharepointdatabasesql-servertoolsxml
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    declare @SQL nvarchar(100) set @SQL = '' select @SQL = @SQL + FieldName + ' as [' + Description + '], ' from FieldsInfo where TableName = 'Emp' set @SQL = 'select '+ left(@SQL, len(@SQL)-1) + ' from Emp FOR XML AUTO, ELEMENTS, ROOT(''customers'')' exec sp_executesql @SQL above is dynamic sql and when it is executed by sp_executesql then i got output as as xml. if i want to store that xml in a variable. so then what i need to add in my tsql script....please help

    tbhattacharjee

    C C 2 Replies Last reply
    0
    • T Tridip Bhattacharjee

      declare @SQL nvarchar(100) set @SQL = '' select @SQL = @SQL + FieldName + ' as [' + Description + '], ' from FieldsInfo where TableName = 'Emp' set @SQL = 'select '+ left(@SQL, len(@SQL)-1) + ' from Emp FOR XML AUTO, ELEMENTS, ROOT(''customers'')' exec sp_executesql @SQL above is dynamic sql and when it is executed by sp_executesql then i got output as as xml. if i want to store that xml in a variable. so then what i need to add in my tsql script....please help

      tbhattacharjee

      C Offline
      C Offline
      Corporal Agarn
      wrote on last edited by
      #2

      You would normally use a dynamic code like this for interface with a front end like .NET. What you need to do is add INTO mytable before the FROM or define a table and use INSERT INTO mytable.

      1 Reply Last reply
      0
      • T Tridip Bhattacharjee

        declare @SQL nvarchar(100) set @SQL = '' select @SQL = @SQL + FieldName + ' as [' + Description + '], ' from FieldsInfo where TableName = 'Emp' set @SQL = 'select '+ left(@SQL, len(@SQL)-1) + ' from Emp FOR XML AUTO, ELEMENTS, ROOT(''customers'')' exec sp_executesql @SQL above is dynamic sql and when it is executed by sp_executesql then i got output as as xml. if i want to store that xml in a variable. so then what i need to add in my tsql script....please help

        tbhattacharjee

        C Offline
        C Offline
        Costica U
        wrote on last edited by
        #3

        Replace "exec sp_executesql @SQL" with:

        declare @xml xml
        set @SQL = 'set @xml =('+@SQL+')' -- stores query result to @xml variable
        exec sp_executesql @SQL,N'@SQL nvarchar(4000),@xml xml OUTPUT',@SQL,@xml OUTPUT
        --- display result
        print convert(varchar(MAX),@xml)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups