?
Solved

execute dinamic query

Posted on 2011-02-14
4
Medium Priority
?
316 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
[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
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

762 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