?
Solved

Dynamic sql in MS SQL

Posted on 2014-03-12
2
Medium Priority
?
258 Views
Last Modified: 2014-03-12
I have a stored procedure and I need to get a field value from a table based on a dynamic field name and a dynamic key value:

declare @FieldName varchar(255)
set @FieldName = 'myField' <-- in reality this is retrieved from a table

declare @KeyValue varchar(30)
set @KeyValue = 'key' <-- also retrieved from a table in the real world

declare @FieldValue varchar(30) <-- to return the results

select @FieldValue = @FieldName from myTable where keyField = @KeyValue

Then, of course, I have to use @FieldValue later in the stored procedure.

I know that this requires dynamic sql, but after looking at many examples and trying many things, I cannot figure it out.

Thanks in advance for whatever help you can provide
0
Comment
Question by:g_johnson
[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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39923922
table and column names cannot be given by variables, so you need to really do dynamic sql, like this:
declare @sql nvarchar(max)
set @sql =  'select @FieldValue = [' + @FieldName + '] from myTable where keyField = @KeyValue '
exec sp_executesql @sql, N'@fieldvalue varchar(30) OUTPUT, @Keyvalue varchar(30)', @fieldvalue OUTPUT, @keyvalue 

Open in new window

0
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 39924087
Perfect.  Thank you!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

771 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