Click here to Skip to main content
15,903,175 members
Home / Discussions / Database
   

Database

 
GeneralRe: Extracting a blob data file from sqlite Pin
crain19818-Jan-11 5:27
crain19818-Jan-11 5:27 
QuestionIndex Fragmentation in Microsoft SQL Server [modified] Pin
Member 285321221-Dec-10 8:12
Member 285321221-Dec-10 8:12 
AnswerRe: Index Fragmentation in Microsoft SQL Server Pin
ScottM122-Dec-10 23:09
ScottM122-Dec-10 23:09 
Questionwrapper program Pin
kabir_2128920-Dec-10 19:11
kabir_2128920-Dec-10 19:11 
AnswerRe: wrapper program Pin
WoutL20-Dec-10 21:28
WoutL20-Dec-10 21:28 
AnswerRe: wrapper program Pin
Jörgen Andersson21-Dec-10 0:13
professionalJörgen Andersson21-Dec-10 0:13 
AnswerRe: wrapper program Pin
ScottM122-Dec-10 23:13
ScottM122-Dec-10 23:13 
QuestionMSSQL - How can I reference a column to update using a variable?? Pin
JTRizos20-Dec-10 9:01
JTRizos20-Dec-10 9:01 
I have a table with one record per security role a user has. So, user SMITH can have multiple records, one for DMV another for CPT and another for ICD. In this example, SMITH has three security roles but can have up to 100.

The desire is to have a second table with one record per user with columns for each security role indicating with Y or N whether the user has that role. The column names are the same as the security role name (eg. DMV, CPT, ICD,...) Smith would have 3 columns with a Y and all the rest with an N.

So, is there a way to read in the first table, use the data in the security role field (DMV, CPT, ICD, etc) to then reference the column in the second table and update the respective column to a "Y" to indicate the user has that role? For example: if table2:ColumnName(DMV)=table1:Security Role("DMV") then update table2:Column(DMV)="Y". The key to both tables is the Employee ID.

The input file we use to create table 1 is in the one record per Security Role per user design. We do not control that.

The intent is to avoid long Case statements. I've Googled this multiple ways and read lots of possibilities but none seem to have a workable solution but most likely I just don't understand it. Below is one way I hoped would worked and a couple of forums indicated it would but I get 'Yes' in the @colname variable.

Declare @colname varchar(200), @Eid varchar(50),@message varchar(80),@command varchar(200)
Declare my_cursor CURSOR
For Select replace(replace(replace(SecurityRole,' ',''),'/',''),'-','') as SecRole,EmployeeID
from EmergencyContact.dbo.CSEEmployeeRoles
where EmployeeID='38'
order by EmployeeID, SecRole
open my_cursor
fetch next from my_cursor into @colname,@Eid 
while @@fetch_status = 0
begin
select @message = @colname+' '+@Eid
print @message
select @command= 'update EmergencyContact.dbo.CSERolesRolledUp set '+@colname+' = "Yes"
where EmployeeID = '+@Eid
exec (@command)
fetch next from my_cursor into @colname,@Eid
end
close my_cursor
deallocate my_cursor


Any help will much appreciated. Seems so simple. Confused | :confused:
AnswerRe: MSSQL - How can I reference a column to update using a variable?? Pin
PIEBALDconsult20-Dec-10 9:34
mvePIEBALDconsult20-Dec-10 9:34 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
JTRizos20-Dec-10 11:12
JTRizos20-Dec-10 11:12 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
PIEBALDconsult20-Dec-10 13:39
mvePIEBALDconsult20-Dec-10 13:39 
AnswerRe: MSSQL - How can I reference a column to update using a variable?? Pin
Jörgen Andersson20-Dec-10 9:39
professionalJörgen Andersson20-Dec-10 9:39 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
JTRizos20-Dec-10 11:19
JTRizos20-Dec-10 11:19 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
Jörgen Andersson20-Dec-10 11:34
professionalJörgen Andersson20-Dec-10 11:34 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
JTRizos20-Dec-10 11:45
JTRizos20-Dec-10 11:45 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
Jörgen Andersson20-Dec-10 12:30
professionalJörgen Andersson20-Dec-10 12:30 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
JTRizos20-Dec-10 12:41
JTRizos20-Dec-10 12:41 
AnswerRe: MSSQL - How can I reference a column to update using a variable?? Pin
Mycroft Holmes20-Dec-10 16:08
professionalMycroft Holmes20-Dec-10 16:08 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
Jörgen Andersson21-Dec-10 0:07
professionalJörgen Andersson21-Dec-10 0:07 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? [modified] Pin
JTRizos21-Dec-10 6:04
JTRizos21-Dec-10 6:04 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
Mycroft Holmes21-Dec-10 11:17
professionalMycroft Holmes21-Dec-10 11:17 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
JTRizos21-Dec-10 11:40
JTRizos21-Dec-10 11:40 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
Mycroft Holmes21-Dec-10 13:19
professionalMycroft Holmes21-Dec-10 13:19 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
JTRizos22-Dec-10 6:26
JTRizos22-Dec-10 6:26 
GeneralRe: MSSQL - How can I reference a column to update using a variable?? Pin
Mycroft Holmes22-Dec-10 11:13
professionalMycroft Holmes22-Dec-10 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.