Solved

execute dinamic query

Posted on 2011-02-14
4
310 Views
Last Modified: 2012-05-11
Im trying to get a value from a table passed as argument a long with its ID

SET @ClientID = '(SELECT ClientID FROM ' + @tablename + ' WHERE ID = ' + @JobID + ')'

Any ideas how i can retrieve the clientID in the example above?
0
Comment
Question by:arcross
  • 2
4 Comments
 
LVL 11

Expert Comment

by:rajvja
Comment Utility
0
 
LVL 11

Expert Comment

by:rajvja
Comment Utility
DECLARE @sql nvarchar(500)
SET @sql = N'SELECT @ClientId = ClientID FROM ' + @tablename + ' WHERE ID = ' + @JobID
EXEC sp_executesql @sql
0
 
LVL 8

Author Comment

by:arcross
Comment Utility
this is what i get

Must declare the scalar variable "@ClientID".
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
Comment Utility
This should be handled as the way demonstrated here
http://support.microsoft.com/kb/262499

I think, it would be like this
DECLARE @sql nvarchar(500)
DECLARE @ClientId INT
SET @ParmDefinition = N'@ClientId int'
SET @sql = N'SELECT @ClientIdOUT = ClientID FROM ' + @tablename + ' WHERE ID = ' + @JobID
EXEC sp_executesql @sql, @ParmDefinition, @ClientIdOUT=@ClientId OUTPUT
SELECT @ClientId

Open in new window


If any error please refer that link's instructions

Raj
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now