declare @ID int = 0;
select ID,Title into #temp from A where ID = 4
select * from #temp
select @ID = ID from #temp
select * from B where AID = @ID
drop table #temp;用临时表,这么写是可以运行成功的。用表变量,怎么写执行都会出错。declare @temp Table
(
ID int,
Title nvarchar(50)
)
insert into @temp (ID,Title) values (4,'123')
select ID,Title into @temp from A where ID = 4
select * from @temp这样写报@temp附近有错误ddeclare @temp Table
select ID,Title,Memo into @temp from QA_Question where ID = 4
select * from @temp这样模仿临时表的方式会报两个错误请按这个简单的例子写个表变量的用法0.0另外还有个疑问,临时表那样,不事先声明,直接select xx,xxx into #temp 就OK了。
表变量必须这样先定义每个字段类型吗?
declare @temp Table
(
ID int,
Title nvarchar(50)
)