Getting query from variable into another query

  • Hello Experts,

    I want to PRINT the variable value into the query i am using MSSQL 2005 please see what i am trying to do.

    Declare @sql nvarchar(max)

    @sql = 'select [Q1],[A1_COMMENT],[Q2],[A2_COMMENT],[Q3],[A3_COMMENT]'

    (PRINT @sql FOR XML PATH('QUESTIONS'), Type)

    FROM #tmp_DataFeed inn LEFT JOIN tblOutput tao ...

    When i try to print the @sql variable it always give me the error "Incorrect syntax near the keyword 'print'."

    One more thing i dont wanted to use a dynamic query for this actually wanted to modify the existing stored procedure which is alreay quite lengthy and i dont have a time to modify the complete procedure. Please help me out ?

    Regards

    Arshad

  • PRINT can only print one thing, whether or not it is a string, a variable, or an expression. If you want to attach "FOR XML, etc" to your variable, you have to make it a constant and attach it to your variable something like this:

    declare @sample varchar(10)

    set @sample = 'Variable'

    print @sample + '/Constant' -- without the plus sign this will fail

    More importantly, PRINT will not modify your stored procedure. (Where did you get the idea that it could?) You have to rewrite it.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Conversation started over here:

    http://qa.sqlservercentral.com/Forums/Topic584256-149-1.aspx

    Try not to keep starting new threads - it's hard to keep a cohesive conversation going on a single topic if there are several threads.

    There's a followup or two to this question on the other thread.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Thanks, Matt. I'm outa here. Y'all finish it up in the other thread.

    By the way, I have always loved that Descartes joke.

    Bob

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

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

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