如何使我的代码一直运行?

时间:2019-02-08 14:42:05

标签: sql alter

我需要做什么来使我的代码一直运行?它在我添加列的ALTER TABLE部分中断了,因为它说它们还不存在-我是否必须在此处的代码中添加某种中断?我是新手用户,但可能是因为执行每个步骤的顺序?

其中一段代码中断的示例-当前,仅当我运行所有更新(此处显示的更新超过此内容),然后再单独运行Alter Table部分时,它才有效。

update TABLE_2
set MEASURE = '-9'
where RESPONSE = '1' 
and QUESTION = '22d' and MEASURE not in (1,2)

update TABLE_2
set MEASURE = '-9'
where RESPONSE = '1' 
and QUESTION = '22e' and MEASURE not in (1,2)

update TABLE_2
set MEASURE = '-9'
where RESPONSE = '1' 
and QUESTION = '22f' and MEASURE not in (1,2)



--Create a ‘CONC’ column of La Code, Stratum and Question:

alter table TABLE_2
add [CONC] nvarchar(10)

update TABLE_2
set [CONC] = [LA_CODE] + [STRATUM_1] + [question]
where [CONC] is NULL

1 个答案:

答案 0 :(得分:0)

答案是go,如评论中所述。我想指出的是,前三个语句可以简化为:

update TABLE_2
    set MEASURE = -9
where RESPONSE = 1 and
      QUESTION in ('22d', '22e', '22f')
      MEASURE not in (1, 2);

然后使用go

以这种方式组合逻辑可以使代码更具可维护性,并且不易出错。假设RESPONSEMEAUSRE的常数是某种数字,我删除了它们的单引号。如果不是,则对所有比较和分配使用单引号。