Query Tweak

  • create table #tfield

    ( id int ,

    Fields nvarchar(200))

    insert into #tfield

    select 1, 'name'

    union select 2, 'Class'

    create table #Student

    (

    name varchar(100),

    Class varchar(20))

    insert into #Student

    select 'Jack' ,'B'

    union select 'Max' , 'dd'

    union select 'Sujan', 'X'

    OUTPUT :

    SEt @field = 'name'

    Result would be : Jack,Max,Sujan

    if @field = 'Class'

    Result : B,DD, X

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • declare @field varchar(5);

    set @field = 'class';

    declare @sqlcmd varchar(max);

    SET @sqlcmd = '

    SELECT ' + @field + ' = STUFF((SELECT '','' + ' + @field + '

    FROM #Student

    FOR XML PATH(''''),TYPE).value(''.'',''varchar(max)''),1,1,'''')';

    exec (@sqlcmd);

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • thanks Wayne, it worked for me

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

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

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