声明变量并插入临时表

时间:2018-12-24 08:20:43

标签: sql sql-server sql-server-2012 temp-tables

为什么会显示错误?

declare @start_date date, @end_date date

set @start_date = '2018-06-01'
set @end_date = '2018-11-30' 
go

select * 
into mytable_TB 
from Other_Table 
where mis_date >= @start_date 
  and mis_date <= @end/_date

错误:

  

必须声明标量变量@start_date

有人知道原因吗?

1 个答案:

答案 0 :(得分:3)

消除了在变量声明和选择查询之间的转入,因此,GO是一个批处理分隔符,用于将整个脚本分成批处理

declare @start_date date, @end_date date

set @start_date = '2018-06-01'
set @end_date = '2018-11-30' 


select * into mytable_TB from Other_Table where mis_date >= @start_date 
and mis_date <= @end_date