Solved

execute dinamic query

Posted on 2011-02-14
4
314 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 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

Industry Leaders: 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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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