Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql中select语句中变量的空值_Sql_Sql Server 2008 R2 - Fatal编程技术网

sql中select语句中变量的空值

sql中select语句中变量的空值,sql,sql-server-2008-r2,Sql,Sql Server 2008 R2,我将多个插入查询组合成一个查询(比如combine1.sql) 当我执行combine1.sql时,我在@Tbl\u Id中得到空值, 然而,当我执行 Declare @Tbl_Id INT Select @Tbl_Id = Id from students where Code='123' --5 comes in @Tbl_Id insert into mappingtable values(@Tbl_Id, getdate()) combine1.sql中

我将多个插入查询组合成一个查询(比如
combine1.sql

当我执行
combine1.sql
时,我在
@Tbl\u Id
中得到空值, 然而,当我执行

    Declare @Tbl_Id INT
    Select @Tbl_Id = Id from students where Code='123'
    --5 comes in @Tbl_Id
    insert into mappingtable values(@Tbl_Id, getdate())

combine1.sql中可能出了什么问题。非常感谢您的帮助。

有一件事需要检查@Tbl\u Id的值,如下所示

 Declare @Tbl_Id INT
    Select @Tbl_Id = Id from students where Code='123'
    --5 comes in @Tbl_Id
    if(@Tbl_Id is not null)
    begin
          insert into mappingtable values(@Tbl_Id, getdate())
    end

显示插入查询1的代码块如果没有更多代码,则无法识别。我怀疑您目前误诊了这个问题,但当您向我们显示的唯一代码在两个块之间是相同的时,我们不太可能发现故障。尝试创建一个你可以与我们共享的值。我的问题是为什么我第一次得到空值,而不是我应该如何避免在Code='123'处插入空值此条件可能是第一次为false,因此它是将空值分配给@Tbl_IdI'm lot循环,因此第一次不存在为false的情况
 Declare @Tbl_Id INT
    Select @Tbl_Id = Id from students where Code='123'
    --5 comes in @Tbl_Id
    if(@Tbl_Id is not null)
    begin
          insert into mappingtable values(@Tbl_Id, getdate())
    end