Solved

Selecting the field name if a condition is met

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert null in sql server 12 33
Query group by data in SQL Server - cursor? 3 31
SQL View nearest date 5 36
Tsql query 6 21
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 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