Results 1 to 4 of 4
Thread: Variable Column Name - MS SQL
-
04-08-2010, 07:52 PM #1
- 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%)
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
-
The Following User Says Thank You to manik For This Useful Post:
merabasera (10-11-2010)
-
10-21-2010, 03:48 AM #2
Junior
- Join Date
- Jul 2010
- Location
- india
- Posts
- 449
- Thanks
- 0
- Thanked 3 Times in 3 Posts
- Feedback Score
- 0
- @scheduleID int,
- @field varchar(50),
- @value varchar(50)
- As
- If (@value = 'ffffff')
- begin
- UPDATE ModaSchedule SET
- @field = '000066'
- WHERE scheduleID = @scheduleID
- end
- else
- begin
- UPDATE ModaSchedule SET
- @field = 'ffffff'
- WHERE scheduleID = @scheduleID
- end
-
The Following User Says Thank You to ursimrankhanna For This Useful Post:
manik (12-10-2010)
-
12-10-2010, 01:31 AM #3
Banned
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 103
- Thanks
- 0
- Thanked 4 Times in 4 Posts
- Feedback Score
- 0
i was searching answer this question but you posted ..
thank you..
-
12-13-2010, 01:05 PM #4
Freshman
- Join Date
- Dec 2010
- Location
- California
- Posts
- 5
- Thanks
- 0
- Thanked 0 Times in 0 Posts
- Feedback Score
- 0
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
-
Frame height problem
By hilda_mateiu in forum HTML / DHTMLReplies: 14Last Post: 05-16-2012, 03:15 AM -
MS SQL Code Snippest
By manik in forum DatabasesReplies: 5Last Post: 04-05-2011, 02:08 AM -
Released another website
By logic in forum ReviewsReplies: 8Last Post: 03-03-2010, 11:28 AM -
Data Import Export with Microsoft SQL DTS
By manik in forum DatabasesReplies: 1Last Post: 06-26-2009, 09:05 AM -
Variable Top Select Microsoft SQL
By manik in forum DatabasesReplies: 3Last Post: 12-24-2008, 10:06 AM


Reply With Quote

