日期:2014-05-17  浏览次数:20445 次

exec sp_executesql @sql1+@sql2...
难道是我的SQLSERVER版本的问题吗?我的版本是sqlserver2000.
我的语句如下所示:
declare @param nvarchar(100)
declare @roadx int
set @param = '@roadx int'
declare @sSql1 nvarchar(4000)
declare @sSql2 nvarchar(4000)
set @sSql1 = 'insert into t1 '
set @sSql2 = ' select * from t1'
exec sp_executesql (@sSql1 + @sSql2) ,@param,@roadx 


------解决方案--------------------
SQL code

create table t1(a int) insert into t1 select 1 union select 2
declare @param nvarchar(100)
declare @roadx int
set @roadx = 1
set @param = '@roadx int'
declare @sSql1 nvarchar(4000)
declare @sSql2 nvarchar(4000)
declare @sSql3 nvarchar(4000)
--set @sSql1 = 'insert into t1  select * from t1 where a = @roadx '
set @sSql1 = 'insert into t1 '
set @sSql2 = ' select * from t1 where a = @roadx'
set @sSql3 = @sSql1 + @sSql2
exec sp_executesql @sSql3,@param,@roadx

------解决方案--------------------
SQL code

declare @param nvarchar(100)
declare @roadx int
set @roadx = 1
set @param = '@roadx int'
declare @sSql1 varchar(4000)
declare @sSql2 varchar(4000)

--set @sSql1 = 'insert into t1 select * from t1 where a = @roadx '
set @sSql1 = 'insert into t1 '
set @sSql2 = ' select * from t1 where a='
exec ('declare @sSql1 varchar(4000), @sSql2 varchar(4000)'+@sSql1+@sSql2+@roadx)