SQL Server-Stored procedure sample

Source: Internet
Author: User

/*    material Equipment Table: Id code name etc     inventory Inventory table:id  MATERIAL_ID MATERIAL_CODE    1, inventory list associated equipment table, only associated with Material_code, no associated material_id (NULL)     2, now according to Material_code in the equipment table material_id, and then assigned to the inventory table */declare  @maid  varchar (100), @macode  varchar (100)--Define two variables (variables must be given length values) declare  #cursor1  CURSOR FOR SELECT  material_id,material_code from inventory--defines a cursor (similar to a collection), queries out the inventory table data open  #cursor1--Opens the cursor fetch  next from  #cursor1  INTO  @maid, @macode-assigns the next value of the cursor to the variable while @ @FETCH_STATUS  =  0--while Cycle begin    select  @maid  = id from material where  code =  @macode-Obtain the device ID according to code and assign it to the @maid variable     print  @maid      update inventory set material_id =  @maid  where material_code  =  @macode--Update the variable @maid to the inventory table     fetch next from  #cursor1  INTO  @maid, @macode--re-assigned, Go to the next loop endclose  #cursor1--Close the cursor deallocate  #cursor1--Destroy the cursor


This article is from the "Zero Technology Road" blog, please be sure to keep this source http://zerohou.blog.51cto.com/3019528/1615327

SQL Server-Stored procedure sample

Related Article

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.