Solved

Selecting the field name if a condition is met

Posted on 2009-05-06
3
479 Views
Last Modified: 2012-06-21
I'd like to be able to select the field name from a table if a boolean condition is met.  I've got a users table which has a userID field, plus a series of 1/0 tinyint fields to control the display of certain dashboard items.  If the field is set to 1, I'd like this query to return the field name.

The stored procedure that I have so far is:

SELECT userShowProcess, userShowLEN, userShowContractExp, userShowWO, userShowTSO, userShowOcc, userShowRev, userShowDelinq, userShowExp
FROM tblUsers
WHERE userID = @UserID;

That gives me 9 columns with each set to a 1 or 0.  While this works at the moment, my problem arises if I need to add options to the system -- there would be quite a bit of rewriting on the code side.  Plus, I currently have to iterate the recordset (which I've saved into an array) to weed out the zeroes.  I've looked into syscolumns and sysobjects but I really don't know what I'm looking at when I see examples of these. My ideal query would show something like this, assuming the user has "1" in four of the 9 fields:
itemName

--------

userShowProcess

userShowLEN

userShowWO

userShowRev

Open in new window

0
Comment
Question by:saoirse1916
3 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 24320149
Hi,

May I suggest that you create a new table for these controlcolumns instead of having them as columns in usertable?

That is

create table tblShowControl (controlID int identity(1,1) PRIMARY KEY CLUSTERED, name varchar(50) not null)
create table tblUserShow (userShowID int identity(1,1) PRIMARY KEY CLUSTERED, userID int not null REFERENCES tblUsers(userID), controlID int not null REFERENCES tblShowControl(controlID), value tinyint not null default 0)

Then you could have

SELECT sc.name
FROM tblUserShow us
INNER JOIN tblShowControl sc ON sc.controlID=us.controlID
WHERE us.userID = @UserID
AND us.value=1;

Also this way it's easy to add new controls.

/peter
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24320852


>> If the field is set to 1, I'd like this query to return the field name.<<
So do you want only the mentioned columns in the SELECT clause with the boolean value set to 1.
SELECT userShowProcess,userShowLEN,userShowWO,userShowRev
  FROM tblUsers
WHERE userID = @UserID AND BooleanField = 1
If my understanding is wrong then provide some sample set and what exactly you are looking for?
0
 
LVL 8

Author Closing Comment

by:saoirse1916
ID: 31578753
I had discounted that early on and decided to put the values in the users table, but now I have no idea why I did that.  This does make more sense.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now