您的位置:首页 > 数据库

使用sql变量sql执行结果

2011-10-11 21:56 274 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐