query projection - dynamically generating

  • Hi,

    is it possible to dynamically generate a query projection. That is: I would like to dynamically decide what columns of a table I would select.

    I tried something like:

    ---

    declare @p char

    set @p = '*'

    select @p

    from FOOBAR

    ---

    and got :

    *

    *

    *

    *

    *

    *

    ...

    TIA,

    João Macaíba.

  • You require to use EXEC(...)

    DECLARE @col varchar(10)

    SET @col = 'UR_COLUMN'

    EXEC ( 'SELECT ' + @col + ' FROM FOOBAR' )

    Alternatively,

    DECLARE @sql varchar(50), @col varchar(10)

    SET @col = 'UR_COLUMN'

    SET @sql = 'SELECT ' + @col + ' FROM FOOBAR'

    EXEC ( @sql )

    Hope this helps you...

    IF U DON'T SEEK PERFECTION, U CAN NEVER REACH EXCELLENCE!!!

    SD


    Regards,
    Sachin Dedhia

  • Or use sp_executesql N'select ' + @FieldList + N' from table' on 2000

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

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