l******9 发帖数: 579 | 1 need to copy a table into a new table on SQL server 2008. Also, add a new
column into the new table.
The values of the new column depends on the compare result between the new
table and another table.
Example,
Table1:
col1 col2 col3
abc 346 6546
hth 549 974
Table1_new:
col1 col2 col3 col4
abc 346 6546 1
hth 549 974 0
Table2:
col1
abc
sfsdf
If Table2's col1 appear in Table1 col1, mark col4 as 1 in Table1_new, else
mark as 0.
The code does not work
SELECT *,
(
SELECT 1 as col4
FROM Table2 as a
INNER JOIN Table1 as b
on b.col1 = a.col1
SELECT 0 as col4
FROM Table2 as a
INNER JOIN Table1 as b
on b.col1 <> a.col1 # I do not know how to do this !!!
)
INTO table1_new
FROM table1
Any help would be appreciated. | y*******7 发帖数: 219 | | s**********o 发帖数: 14359 | | a**d 发帖数: 4285 | 4 执行了一下,可行:
select t1.col1, t1.col2, t1.col3,
col4= case when t2.col1 is not null then 1
else 0
end
into table_new
from table1 t1 left join table2 t2 on t1.col1=t2.col1 | s**********o 发帖数: 14359 | | B*****g 发帖数: 34098 | 6 YHD
【在 s**********o 的大作中提到】 : OMG,LZ基本的SQL都没学会啊
|
|