Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 execute an execute dynamically

Author  Topic 

mksdf
Starting Member

26 Posts

Posted - 2014-10-02 : 05:30:52
can you execute an execute statement? I am getting an error. What is the way around this?

For example

DECLARE	@SQL nvarchar(MAX)
DECLARE @DB nvarchar(100) = 'DB_TEST'
, @ID int = 1
SET @SQL = 'EXEC ' + @DB + '.dbo.usp_test ' + CAST(@ID AS varchar(4))

EXEC @SQL


Thanks

MichaelJSQL
Constraint Violating Yak Guru

252 Posts

Posted - 2014-10-02 : 07:21:16
Try the following :
DECLARE @SQL nvarchar(MAX)
DECLARE @DB nvarchar(100) = 'DB_TEST'
, @ID int = 1
SET @SQL = 'EXEC ' + @DB + '.dbo.usp_test ' + CAST(@ID AS varchar(4))

EXEC SP_EXECUTESQL @SQL

I ran this to confirm no issues because I don't have your proc
DECLARE @SQL nvarchar(MAX)
DECLARE @DB nvarchar(100) = 'master'
, @ID int = 1
SET @SQL = 'EXEC ' + @DB + '.dbo.sp_helpfile '


EXEC SP_EXECUTESQL @SQL




Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2014-10-02 : 19:35:57
exec (@SQL)



No amount of belief makes something a fact. -James Randi
Go to Top of Page
   

- Advertisement -