SQL INSERT triggers BULK INSERT Cursors

Source: Internet
Author: User
Tags bulk insert

I am currently working in a business-to-consumer site, today in the work encountered a problem, is the need to have millions of data of the order form table1 data to another to provide to the merchant to view the table table2, When inserting into the Table2 table, the newly inserted data needs to be parsed into the Table3 table and the Table4 table, respectively, and the data in the Table2 table is retained. If the query out of the data Ctrl + C to table2 trigger will have effect, but Bai data let me go sticky, today can rest!

When we want to use INSERT INTO table1 select * from Table2, the trigger executes only the first row in the Table2. At this point, we're going to use cursors to control the rhythm of inserting data (lol is much more fun ...). ). The following is the result of my study this morning, have not touched the cursor before, there may be some local comments are not correct, understanding!

1 ALTER TRIGGER [dbo].[Tri_table2]2  on [dbo].[table2] --on which table to build3instead of INSERT --Trigger before insertion4  as5 BEGIN6     --defining variables for passing values7     DECLARE @MchId VARCHAR(Ten)8     DECLARE @MchNo VARCHAR( -)9     DECLARE @ProductCode VARCHAR( -)Ten     DECLARE @SuccGetCardTime DATETIME One     DECLARE @BuyNo VARCHAR( -) A     DECLARE @BuyCall VARCHAR( -) -     DECLARE @SectionNo VARCHAR( -) -     --Creating Cursors the     DECLAREYbCURSOR  for     -     SELECT [Mchno],[Mchid],[ProductCode],[Succgetcardtime],[Buyno],[Buycall],[Sectionno] fromINSERTED--cursor reads row information -     OPENYb--Open Cursor -     FETCH NEXT  fromYb into @MchNo,@MchId,@ProductCode,@SuccGetCardTime,@BuyNo,@BuyCall,@SectionNo--assigns the first line of information that the cursor reads to the variable +      while @ @FETCH_STATUS = 0 --[indicates whether read to data row]0 operation succeeded, -1 FETCH statement failed or this row is not in result set,-2 fetched row does not exist -     BEGIN +         --determine if the order exists in the table A         IF  not EXISTS(SELECT 1  fromTable3WHEREMchno=@MchNo  andMchid=@MchId) at         BEGIN -             --data migration to Table3 table -             INSERT  intoTable3 -             VALUES(@MchNo,@MchId,@ProductCode,@SuccGetCardTime,@BuyNo,@BuyCall,@SectionNo) -              -             IF(@MchId='BJFW' OR @MchId='BJF2') in             BEGIN -             --data migration to Table4 table to                 INSERT  intotable4 +                 VALUES(@MchNo,@MchId,@ProductCode,@SuccGetCardTime,@BuyNo,@BuyCall,@SectionNo) -             END the         END *         --Complete Work_tradeinfo table data entry $         INSERT  intotable2Panax Notoginseng         VALUES(@MchNo,@MchId,@ProductCode,@SuccGetCardTime,@BuyNo,@BuyCall,@SectionNo) -         --cursor jumps to the next line to continue looping the         FETCH NEXT  fromYb into @MchNo,@MchId,@ProductCode,@SuccGetCardTime,@BuyNo,@BuyCall,@SectionNo +     END A     CLOSEYb--Close Cursors the     deallocateYb--Releasing Cursors + END

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.