In sql, the cursor can not be achieved sometimes all cyclic operation, such as when the double loop, the inner dynamic cursor is defined repeatedly allowed sql syntax.
The following describes a method of using a temporary substitution for the cursor.
sql Code
use
Test
Go - Assume a Table, the TA, the TB Create Table the TA ( ID UNIQUEIDENTIFIER Primary Key , name VARCHAR ( 10 ), Age int ) Go - inserting some test data INSERT the TA (ID, name, Age) values ( ' E5DC-4688--675BEB41-Standard Specification for CA0BD5A58961 ' , ' John Doe ' , 20 is ) INSERT the TA (ID, name, Age) values ( '
94D7-38507782C8DE-0658-488B-EF6358BE ' , ' John Doe ' , 15 )
Go Create Table the TB ( ID UNIQUEIDENTIFIER Primary Key , TA_ID UNIQUEIDENTIFIER Foreign Key References the TA (ID), the Claim decimal ( 18 is , 2 ), claim_date datetime Not null default ( getdate ()) ) Go - insert some of these people test data reimbursement
insert TB (id, TA_id, claim, claim_date) values ( newid (), ' 675BEB41-E5DC-4688-B317-CA0BD5A58961 ' , 300 , ' 2010-03-01 ' )
insert TB (id, TA_id, claim, claim_date) values ( newid (), ' 675BEB41-E5DC-4688-B317-CA0BD5A58961 ' , 150 , ' 2010-04-05 ' )
insert TB (id, TA_id, claim, claim_date) values ( newid (), ' EF6358BE-0658-488B-94D7-38507782C8DE ' , 50 , ' 2010-02-23 ' )
INSERT the TB (ID, TA_ID, the Claim, claim_date) values ( NEWID (), ' EF6358BE-0658-488B-94D7-38507782C8DE ' , 350 , ' 2010-03-15 ' )
INSERT the TB (ID, TA_ID, the Claim, claim_date) values ( NEWID (), ' EF6358BE-0658-488B-94D7-38507782C8DE ' , 412 , ' 2010-04-10 ' )
- now to copy all age> 10 is Schedule data and its data. select
* INTO #ta_temp from the TA - assuming a condition: WHERE Age > 10 Go - recording in each #ta_temp generate a new primary key ALTER Table #ta_temp the Add new_key UNIQUEIDENTIFIER Not null default ( NEWID ()) Go - test: - SELECT * from #ta_temp - now inserted to copy the master table record iNSERT the TA (ID, name, Age) SELECT new_key, name + ' copy ' , Age from #ta_temp
- now #ta_temp each primary table record corresponding to the record in the schedule found,
- and corresponds to the new master key is inserted from the table INSERT the TB (ID, TA_ID, the Claim, claim_date) SELECT NEWID (), b.new_key , a.claim, a.claim_date from TB A Inner the Join #ta_temp b ON a.TA_id = b.id Go - clean up the site drop the Table #ta_temp Go - test results: the SELECT * from TA the SELECT * from TB Go
Go - Assume a Table, the TA, the TB Create Table the TA ( ID UNIQUEIDENTIFIER Primary Key , name VARCHAR ( 10 ), Age int ) Go - inserting some test data INSERT the TA (ID, name, Age) values ( ' E5DC-4688--675BEB41-Standard Specification for CA0BD5A58961 ' , ' John Doe ' , 20 is ) INSERT the TA (ID, name, Age) values ( '
94D7-38507782C8DE-0658-488B-EF6358BE ' , ' John Doe ' , 15 )
Go Create Table the TB ( ID UNIQUEIDENTIFIER Primary Key , TA_ID UNIQUEIDENTIFIER Foreign Key References the TA (ID), the Claim decimal ( 18 is , 2 ), claim_date datetime Not null default ( getdate ()) ) Go - insert some of these people test data reimbursement
insert TB (id, TA_id, claim, claim_date) values ( newid (), ' 675BEB41-E5DC-4688-B317-CA0BD5A58961 ' , 300 , ' 2010-03-01 ' )
insert TB (id, TA_id, claim, claim_date) values ( newid (), ' 675BEB41-E5DC-4688-B317-CA0BD5A58961 ' , 150 , ' 2010-04-05 ' )
insert TB (id, TA_id, claim, claim_date) values ( newid (), ' EF6358BE-0658-488B-94D7-38507782C8DE ' , 50 , ' 2010-02-23 ' )
INSERT the TB (ID, TA_ID, the Claim, claim_date) values ( NEWID (), ' EF6358BE-0658-488B-94D7-38507782C8DE ' , 350 , ' 2010-03-15 ' )
INSERT the TB (ID, TA_ID, the Claim, claim_date) values ( NEWID (), ' EF6358BE-0658-488B-94D7-38507782C8DE ' , 412 , ' 2010-04-10 ' )
- now to copy all age> 10 is Schedule data and its data. select
* INTO #ta_temp from the TA - assuming a condition: WHERE Age > 10 Go - recording in each #ta_temp generate a new primary key ALTER Table #ta_temp the Add new_key UNIQUEIDENTIFIER Not null default ( NEWID ()) Go - test: - SELECT * from #ta_temp - now inserted to copy the master table record iNSERT the TA (ID, name, Age) SELECT new_key, name + ' copy ' , Age from #ta_temp
- now #ta_temp each primary table record corresponding to the record in the schedule found,
- and corresponds to the new master key is inserted from the table INSERT the TB (ID, TA_ID, the Claim, claim_date) SELECT NEWID (), b.new_key , a.claim, a.claim_date from TB A Inner the Join #ta_temp b ON a.TA_id = b.id Go - clean up the site drop the Table #ta_temp Go - test results: the SELECT * from TA the SELECT * from TB Go
Reproduced in: https: //www.cnblogs.com/davidgu/archive/2010/04/23/1718755.html