2015-02-24 18 views
0

我想创建一个过程,该过程应检查两个表之间的列,并根据比较将值插入另一个表中。如何比较两个表的列并根据SQL Server中存储过程中的比较将值插入新表中

表1:

create table table1 
(
ID int not null primary key, 
) 

表2:

Create table table2 
(
ItemID int not null primary key, 
ID int FOREIGN KEY REFERENCES Orders(OrderID) , 
Descp Text 
) 

表3:

create table table3 
(
ID int, 
ItemCheck char 
) 

值的表3的列应该与表1的ID列和 相同,如果table1表的ID列存在于表2中,则表3的值列ItemCheck应该是'true'oterwise'false'。 请给我一些想法,并让我知道如果你有任何疑问。提前致谢。

回答

0

听起来像你想要这样的东西?

TRUNCATE table3; 
INSERT INTO table3 (ID, ItemCheck) 
    SELECT ID, 
     CASE WHEN EXISTS (SELECT 1 FROM table2 t2 WHERE ID = t.ID) 
       THEN 'T' 
       ELSE 'F' 
     END 
    FROM table1 t 
+0

It Works。非常感谢你 – Ats 2015-02-24 18:16:51

0
Declare @col1 varchar(10) 
Declare @col2 varchar(10) 

SET @col1 = Select column1 from table1 where id =1 
SET @col1 = Select column1 from table1 where id =2 

IF(@col1 == @col2) 
BEGIN 
    // insert statement goes here 
END 
+0

感谢您的回复 – Ats 2015-02-24 18:17:12

相关问题