Solved

SQL Query - Dynamic Field

Posted on 2012-03-19
4
175 Views
Last Modified: 2012-03-19
I need help with a query.  I would like to add in a parameter that allow user to select a field dynamically.

Here's the query.

DECLARE @ID VARCHAR(2)
DECLARE @ITEM_ID VARCHAR(30)

SET @ID = '1'
SET @ITEM_ID = '09901'

SELECT ITEM_ID, DESCRIPTION, USER_+@USER+
FROM  ITEM_MASTER
WHERE ITEM_ID  = @ITEM_ID

I would like for user to be able o toggle from 1 to 10 with the user field.
0
Comment
Question by:holemania
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37738923
DECLARE @ID VARCHAR(2)
DECLARE @ITEM_ID VARCHAR(30)
declare @sql varchar(max)

SET @ID = '1'
SET @ITEM_ID = '09901'

set @sql = 'SELECT ITEM_ID, DESCRIPTION, USER_ ' +@ID+  ' FROM  ITEM_MASTER WHERE ITEM_ID  = ' + @ITEM_ID

execute (@sql)
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 37739007
have you tried making a stored procedure
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 37739034
Just noticed a slight error.  You need ' ' around the item id.

set @sql = 'SELECT ITEM_ID, DESCRIPTION, USER_ ' +@ID+  ' FROM  ITEM_MASTER WHERE ITEM_ID  = ' + @ITEM_ID

should be

set @sql = 'SELECT ITEM_ID, DESCRIPTION, USER_ ' +@ID+  ' FROM  ITEM_MASTER WHERE ITEM_ID  = ''' + @ITEM_ID + ''''


A stored proc wouldn't help with the dynamic portion of this, but would help in terms of keeping the parameters straight.
0
 

Author Closing Comment

by:holemania
ID: 37739281
Thank you.  That's exactly what I need.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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