1,获取返回表
declare @sql varchar(1000)
set @sql=N'select * from Student'
--exec(@sql)
create table #S
(
#SStudentID int--#S表结构和Student一致
)
insert into #S exec(@sql)--把返回的结果报错到临时表里
select * from #S--对临时表操作
drop table #S
2,获取单个变量
declare @sql varchar(1000),@var int
set @sql=N'select * from Student'
--exec(@sql)
If Object_Id( 'Tempdb.dbo.#S') Is Not NULL
--#Test 为临时表名
begin
Print 'Exists Table'
drop table #S
end
Else begin
Print 'Not Exists Table'
create table #S
(
#SStudentID int
)
end
insert into #S exec(@sql)--把返回的结果报错到临时表里
select * from #S--对临时表操作
set @var=(select top 1 #SStudentID from #S)
select @var
drop table #S
注:判断表是否存在:
IF EXISTS (Select * From sysObjects Where Name ='Student' And Type In ('S','U'))
PRINT 'exists'
ELSE
PRINT 'not exists'
GO