Solved

execute dinamic query

Posted on 2011-02-14
4
312 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
ID: 34887380
0
 
LVL 11

Expert Comment

by:rajvja
ID: 34887433
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
ID: 34887483
this is what i get

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

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 34887954
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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 …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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