我有2个表Table A和TableB。基于将表A和表B连接起来的某些条件,我将值插入表C。
在表C中,我通过使用insert插入select查询中来插入值。 假设表C有4列,我的第二列值取决于第一列值,即如果列1的值小于0,则将列2的值插入为'F'或'T'。 因此,如何通过计算同一表中第1列的值将值插入第2列。
模式:
INSERT INTO #TableC
SELECT
B.STORE AS WS_AUTH_STR,
(CASE
WHEN (B.DATE /17) = substring(cast([A_EXT_AUTH_CD] as varchar(10)),1, 4) THEN (B.DATE /17)
WHEN (B.DATE /17) = substring(cast([A_EXT_AUTH_CD] as varchar(10)),2, 5) THEN (B.DATE /17)
WHEN (B.DATE /17) = substring(cast([A_EXT_AUTH_CD] as varchar(10)),3, 6) THEN (B.DATE /17)
WHEN (B.DATE /23) = substring(cast([A_EXT_AUTH_CD] as varchar(10)),1, 4) THEN (B.DATE /23)
WHEN (B.DATE /23) = substring(cast([A_EXT_AUTH_CD] as varchar(10)),2, 5) THEN (B.DATE /23)
WHEN (B.DATE /23) = substring(cast([A_EXT_AUTH_CD] as varchar(10)),3, 6) THEN (B.DATE /23)
ELSE A_AUTH_CD
END) AS WS_AUTH_AUTH_CODE,
COLUMN N
FROM TableA A
INNER JOIN TableB B ON A.STORE = B.STORE;
为了插入N列,首先我需要检查WS_AUTH_AUTH_CODE的值。如果是(B.DATE / 17),则在此列中插入1,否则插入2。
答案 0 :(得分:0)
您可以使用CASE满足您的要求。 select语句如下-
INSERT INTO....
....
SELECT A.Some_column AS Column_1,
CASE
WHEN A.Some_column = 0 THEN 'F'
ELSE 'T'
END AS Column_2
FROM Some_table A