Solved

Selecting the field name if a condition is met

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

840 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