/* 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