Solved

Selecting the field name if a condition is met

Posted on 2009-05-06
3
478 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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 …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

747 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

11 Experts available now in Live!

Get 1:1 Help Now