Results 1 to 9 of 9

Thread: Column Names From a Variable Without Execute Statement

  1. #1
    Join Date
    Sep 2002
    Location
    Eastbourne
    Posts
    7

    Column Names From a Variable Without Execute Statement

    Is there anyway anyone knows of that i can select columns using variable names without building an execute statement??

    ie.

    DECLARE @col varchar(10)

    SELECT @col = "AuditID"

    SELECT @Col FROM tblAudit

    ??

    Anyhelp a bonus

    Thanks

    Daniel/

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    No, you can't.

  3. #3
    Join Date
    Oct 2002
    Posts
    1

    Wink Yes you can

    DECLARE @Id varchar(20), @Table varchar(20), @Field varchar(20)

    @SQL varchar(200)

    SET @Field = 'MyField'
    SET @Id = 'Id'
    SET @Table = 'MyTable'

    exec('SELECT ' + @Field + ' FROM ' + @Table + ' where id = ''' + @id + '''')

    That is all

  4. #4
    Join Date
    Sep 2002
    Posts
    5,938
    But original question is do this without building an execute statement.

  5. #5
    Join Date
    Sep 2002
    Location
    Eastbourne
    Posts
    7

    Summary

    So from what ppl are telling me.
    even though we are now in version 2000

    SQL Does not support (from transact)

    Evaluated column names similar to the javascript Eval statement

    Variable arrays of any dimensions ??

    is this correct ?

    Daniel G

  6. #6
    Join Date
    Sep 2002
    Posts
    5,938
    You may try UDF in sql2k.

  7. #7
    Join Date
    Sep 2002
    Location
    Eastbourne
    Posts
    7
    what is UDF ???

  8. #8
    Join Date
    Sep 2002
    Posts
    5,938
    User defined functions.

  9. #9
    Join Date
    Sep 2002
    Location
    Eastbourne
    Posts
    7

    udf

    i think i may get away with a UDF that does a case statement on the column name variable passed in ..

    thanks for everyones help

    Danielg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •