SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get value from exec(@sql)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

albertkohl
Aged Yak Warrior

USA
723 Posts

Posted - 04/30/2012 :  17:26:30  Show Profile  Visit albertkohl's Homepage  Reply with Quote
okay, so i know i'm totally just not thinking of something here, but i have a stored proc i'm trying to have work with a dynamic table name. i'm trying to get how many rows are in the table, how to a capture that from the Exec(@sql) command?


declare @tablename varchar(255) = '#temp'
declare @sql varchar(max)
declare @maxid varchar(30)
set @sql = 'select max(rec_no) maxid from ' + @tablename
exec(@sql)



any help would be great! thanks in advance!

tkizer
Almighty SQL Goddess

USA
35007 Posts

Posted - 04/30/2012 :  18:50:12  Show Profile  Visit tkizer's Homepage  Reply with Quote
Use sp_executesql to get output from dynamic SQL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
35007 Posts

Posted - 04/30/2012 :  18:50:25  Show Profile  Visit tkizer's Homepage  Reply with Quote
Examples are in sp_executesql topic in BOL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 05/01/2012 :  07:00:05  Show Profile  Reply with Quote
Yes. You'll have to use sp_executesql. Check the link:

http://hspinfo.wordpress.com/2011/05/02/how-do-i-get-result-of-dynamic-sql-into-a-variable/

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000