sql

时间:2015-12-23 11:20:32

标签: sql sql-server-2008-r2

我将多个插入查询合并到一个查询中(比如combine1.sql

--insert query 1
--insert query 2
.
.
.
Declare @Tbl_Id INT
Select @Tbl_Id = Id from students where Code='123'
--null value comes in @Tbl_Id
insert into mappingtable values(@Tbl_Id, getdate())

.
.
.
--insert query n

当我执行@Tbl_Id时,我在combine1.sql中获得空值, 但是,当我执行

    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可能出现的问题。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

像这样检查@Tbl_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