【问题标题】:Declaring a variable and inserting into temporary table声明变量并插入临时表
【发布时间】:2019-05-23 11:14:19
【问题描述】:

为什么会出现错误?

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

有人知道这是什么原因吗?

【问题讨论】:

  • 从那里删除GO
  • 变量具有批处理范围。添加 GO 会创建一个新批次,因此该变量在那里不可用。

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


【解决方案1】:

消除变量声明和选择查询之间的 Go,因此 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多