我想从一个临时表中选择另一个临时表。每个记录都将按原样复制,除非ID = 17.在这种情况下,我希望第二个表中的ID为18。我该怎么做?
答案 0 :(得分:2)
如果两个表都已存在,请使用以下查询:
SET IDENTITY_INSERT #t2 ON -- only needed if ID is a Primary Key
INSERT INTO #t2 (
ID
,Column1
,Column2
)
SELECT
CASE
WHEN #t1.ID = 17 THEN 18
ELSE #t1.ID
END AS ID
,#t1.Column1
,#t1.Column2
FROM #t1
SET IDENTITY_INSERT #t2 OFF -- only needed if ID is a Primary Key
答案 1 :(得分:0)
SELECT *
,CASE WHEN ID=17 THEN 18
ELSE ID
END ID
INTO #temp2
FROM #temp1