Get column header from a table value

  • Does anyone know how to set the column name from another table's value?

    I tried something like:

    Select Col1 as (Select Coltitle from TableB where Bid = somevalue) From TableA

     

    I get the following error

    Server: Msg 170, Level 15, State 1, Line 1

    Line 1: Incorrect syntax near '('.

    Server: Msg 156, Level 15, State 1, Line 1

    Incorrect syntax near the keyword 'From'.

     

    Does anyone know how to achieve the above?

  • I have two tables

    Cst (for customers) and

    Prd (for Products):

    I will display the values of Product Code (PrdCode) with column title the 'CstFullName' that I get from Cst (Customers):

    ===================================================================

    declare @S1 varchar(512)

    declare @S2 varchar(255)

    Select @s1 = c.name

    from sysobjects o, syscolumns c

    where o.id = c.id and o.type='U' and o.name='Cst' and c.name='CstFullName'

    select @s2= 'select prdCode as '+ @s1+' from prd'

    exec(@s2)

    ========================================================

    The above is a workaround that I hope it will help you!

    AJV

    PS: Kali sinexeia, elpizw na min 8es na ksegelaseis kanena me ayto to query... 😉

    ------------
    When you 've got a hammer, everything starts to look like a nail...

  •  

    I think your Question is not properly justified,

    It may be happen but i have no words to explain it ??

     

    sorry

     

  • Amit you are right.

    Andreas solution has the effect of returning the column name to the query string, when I was looking for a value in the table.

    However, his approach, that is, to build up a querystring is what I was looking for. So I merely adjusted the code to fetch the relevant field from the table rather than the actual column name.

    It works fine.

    Thanks Andreas (poli orea )

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

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