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 2005 Forums
 Transact-SQL (2005)
 Can sp_executesql in insert statement?

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2009-11-04 : 14:22:00
I want to use sp_executesql in insert statement but got an error:

declare @sql nvarchar(2000)
set @sql =
insert into tOrder (f1, f2...f20)
select f1, f2, ... f20
from tOrderOld
exec sp_executesql @sql

What is wrong?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-04 : 14:58:44
Your example has syntax problems. Where's the single quotes for @sql?

It would be best if you posted your actual code and the exact error message.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Pradip
Starting Member

32 Posts

Posted - 2009-11-05 : 03:27:11
declare @sql nvarchar(2000)
set @sql ='insert into t1(a) select a from t2'
exec sp_executesql @sql
it works fine, please check your code.

pradipjain
Go to Top of Page
   

- Advertisement -