所以有两个表Table1和Table2。请参阅附带的屏幕截图以获取对Table1结构和值的参考。请注意,第5行具有所有NULL值。
我试图从Table1中获取值(仅在它不是空的情况下),将这些值替换为硬编码值' 覆盖'并将它们插入表2中。
理想情况下,Table2应该看起来像截图
附接。请注意,未插入第5行,因为它具有所有NULL值。
我已经编写了下面的查询,但无论值是什么,它都会为两个列插入硬编码值,而表格就像截图
附接。请参阅下面的查询和协助。
INSERT INTO Table2 (Row_Number, ValueA, ValueB)
SELECT Row_Number, ValueA= 'Override', ValueB= 'Override' FROM Table A
WHERE A.ValueA is not null
or ValueB is not null ;
答案 0 :(得分:2)
您可以使用case
表达式:
INSERT INTO Table2 (Row_Number, ValueA, ValueB)
SELECT Row_Number,
(case when ValueA is not null then 'Override' end) as ValueA,
(case when ValueB is not null then 'Override' end) as ValueB
FROM Table A
WHERE A.ValueA is not null or ValueB is not null;
答案 1 :(得分:0)
尝试以下方法:
INSERT
INTO Table_2
(Row_Number,ValueA, ValueB)
SELECT Row_Number,
CASE
WHEN A.valueA IS NOT NULL THEN
'Override'
END AS ValueA,
CASE
WHEN A.ValueB IS NOT NULL THEN
'Override'
END AS ValueB
FROM Table_1 A
WHERE A.ValueA IS NOT NULL
OR A.ValueB IS NOT NULL