Results 1 to 4 of 4
  1. #1
    manik's Avatar
    manik is online now Om Shanti!
    Join Date
    Apr 2008
    Location
    Boston, USA
    Posts
    13,175
    Thanks
    744
    Thanked 699 Times in 550 Posts
    Blog Entries
    4
    Feedback Score
    4 (100%)

    Default Variable Column Name - MS SQL

    How do you use a variable as a column name in a MS SQL statement?

    Answer is you cant do that directly. You have to build and run the query dynamically. Like this:

    Code:
    declare @sql nvarchar(2000)
    set @sql = 'select ' + @columnName + ' from myTable'
    EXEC(@sql)
    Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK


  2. The Following User Says Thank You to manik For This Useful Post:

    merabasera (10-11-2010)

  3. #2
    Join Date
    Jul 2010
    Location
    india
    Posts
    449
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Feedback Score
    0

    Default

    1. @scheduleID int,
    2. @field varchar(50),
    3. @value varchar(50)
    4. As
    5. If (@value = 'ffffff')
    6. begin
    7. UPDATE ModaSchedule SET
    8. @field = '000066'
    9. WHERE scheduleID = @scheduleID
    10. end
    11. else
    12. begin
    13. UPDATE ModaSchedule SET
    14. @field = 'ffffff'
    15. WHERE scheduleID = @scheduleID
    16. end

  4. The Following User Says Thank You to ursimrankhanna For This Useful Post:

    manik (12-10-2010)

  5. #3
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    103
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Feedback Score
    0

    Default

    i was searching answer this question but you posted ..
    thank you..

  6. #4
    DCSS is offline Freshman
    Join Date
    Dec 2010
    Location
    California
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    I think you may have answered my question but am not quite sure. Here is my issue.

    I have a table with one record per certificate a user has. So, user SMITH can have multiple records, one for DMV another for CPT and another for ICD. SMITH has three certificates but can have up to 20.

    The desire is to have a second table with one record per user with columns for each certificate indicating with Y or N whether the user has that certificate. The column names are the same as the certificate name (eg. DMV, CPT, ICD,...)

    So, is there a way to read in the first table, use the data in the certificate field (DMV, CPT, ICD, etc) to the reference the column in the second table and update the respective column to a "Y" to indicate the user has that certificate? For example: if table2:ColumnName(DMV)=table1:Certificate("DMV") then update table2:Column(DMV)="Y"
    The input file we use to create table 1 is in the one record per certificate per user design. We do not control that.

    The intent is to avoid long Case statements. If this works, we have another similar application with a possible 130 roles a user can have. I've Google this multiple was and read lots of possibilities but none seem to indicate this can be done or I just don't understand it.

    Can I do this? If so, how? Your answer above seems to indicate I can but I don't quite understand how. Any help will be much appreciated. Thank you!

Similar Threads

  1. Frame height problem
    By hilda_mateiu in forum HTML / DHTML
    Replies: 14
    Last Post: 05-16-2012, 03:15 AM
  2. MS SQL Code Snippest
    By manik in forum Databases
    Replies: 5
    Last Post: 04-05-2011, 02:08 AM
  3. Released another website
    By logic in forum Reviews
    Replies: 8
    Last Post: 03-03-2010, 11:28 AM
  4. Data Import Export with Microsoft SQL DTS
    By manik in forum Databases
    Replies: 1
    Last Post: 06-26-2009, 09:05 AM
  5. Variable Top Select Microsoft SQL
    By manik in forum Databases
    Replies: 3
    Last Post: 12-24-2008, 10:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Partners: BOSTON WEB DEVELOPER, LLC   |   WEBCOSMO CLASSIFIEDS