下面有错误
declare @table table(id int,name nvarchar(30))
insert into @table
select 1,'a' union all
select 2,'b' union all
select 3,'c'
-----------------------
declare @strSQL nvarchar(100)
set @strSQL='select * from '+@table
exec(@strSQL)
go
declare @table table(id int,name nvarchar(30))
insert into @table
select 1,'a' union all
select 2,'b' union all
select 3,'c'
-----------------------
declare @strSQL nvarchar(100)
set @strSQL='select * from '+@table
exec(@strSQL)
go
insert into @table
select 1,'a' union all
select 2,'b' union all
select 3,'c'select * from @table
这样不就可以了嘛
set @strSQL='
declare @table table(id int,name nvarchar(30))
insert into @table
select 1,''a'' union all
select 2,''b'' union all
select 3,''c''select * from @table'
exec(@strSQL)
go
需要首先生成一个表变量,然后再动态构造sql语句执行.
set @strSQL=''
declare @table table(id int,name nvarchar(30))
insert into @table
select 1,'a' union all
select 2,'b' union all
select 3,'c'select * from @table
exec(@strSQL)
go
select * from @table为何要用动态sql语句呢?
如果必用动态sql的话,要写成这样:
set @str='select * from @table'
SET @table='SELECT * FROM .........'
EXEC SP_EXECUTESQL @table
@table必须是 NVARCHAR TEXT NTEXT 类型
要是不好使称把命给你