Solved

Selecting the field name if a condition is met

Posted on 2009-05-06
3
482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 41

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Throw Error 7 35
SQL syntax question 6 45
SQL - Error "The Multi-Part Identifier could not be found" 2 21
mysql vs miscrosoft sql server 6 20
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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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