简体   繁体   English

如何创建存储过程

[英]How to create a stored procedure

I have two tables, table1 and table2 我有两个表, table1table2

table1 columns table1

product_name 
product_info

table2 columns table2

product_name 
product_info 
Product_address 
etc

I need to insert into table1 if any new product name is inserted in table2 我需要插入table1是否有新的产品名称插入table2

I tried below code but its not inserting. 我尝试了下面的代码,但未插入。 Where did I made a mistake? 我在哪里弄错了?

as
begin
    insert into  table2  (product_name product_info Product_address etc) values   (...);

    INSERT INTO table1(Product_Name)
         SELECT Product_Name 
         FROM tb_new_purchase
         WHERE NOT EXISTS (SELECT Product_Name FROM tb_new_product_Name_id )
 end

You need a correlated subquery. 您需要一个相关的子查询。 I find your table names inconsistent. 我发现您的表名称不一致。 I think this is what you want: 我认为这是您想要的:

INSERT INTO table1(Product_Name)
     SELECT Product_Name 
     FROM table2
     WHERE NOT EXISTS (SELECT Product_Name
                       FROM table1
                       where table1.Product_Name = Table2.Product_Name
                      );

Your query never inserts a new name, because there is at least one row in the query. 您的查询永远不会插入新名称,因为查询中至少有一行。 By the way, you can also express this as: 顺便说一句,您也可以这样表示:

INSERT INTO table1(Product_Name)
     SELECT Product_Name 
     FROM table2
     WHERE Product_Name not in (SELECT Product_Name
                                FROM table1
                               );

似乎只会在tb_new_product_name_id为空的地方插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM