How to built string from the table values

  • drop table #temp

    Create table #temp

    (

    Pk_Id Int,

    Name Varchar(100),

    value Varchar(1000)

    )

    Insert Into #temp

    values (1,'@para','value'),

    (2,'@para1','value1'),

    (3,'@para2','value2'),

    (4,'@para3','value3')

    Select * from #temp

    /*** Desired Output data **/

    exec sp_name @para = 'value',@para1 = 'value1',@para2 = 'value2',@para3 = 'value3'

    Please help me.

    Thanks in Advance!!

  • DECLARE @param varchar(1000), @param1 varchar(1000), @param2 varchar(1000), @param3 varchar(1000);

    SELECT @param = value FROM #temp WHERE Name = '@para';

    SELECT @param1 = value FROM #temp WHERE Name = '@para1';

    SELECT @param2 = value FROM #temp WHERE Name = '@para2';

    SELECT @param3 = value FROM #temp WHERE Name = '@para3';

    exec sp_name @para = @param,@para1 = @param1,@para2 = @param2,@para3 = @param3;

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Hi,

    I want to create this string dyanamically,

    based on the data in the table.

  • Maybe you should mention this in your original question.

    You'll need to write some dynamic SQL: create a string variable large enough to hold the different SELECT statements that I wrote earlier.

    Construct these using a query over your source table and store the result in this string variable. At the end of this generated statement, make sure you select the parameters that you want to use.

    Execute this string variable using sp_executesql. This stored procedure has the capability to retrieve values and store them in output parameters. Use these parameters as input parameters in your final stored procedure.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

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

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