使用cursor递归遍历sqlserver的相应表

use rc 
GO

DECLARE @nnn varchar(100)DECLARE cur1 cursor for
select [name] from sys.tables
where name LIKE 'index_%'

drop table #Tmp   --删除临时表#Tmp
create table #Tmp --创建临时表#Tmp
(
   kkk uniqueidentifier
);

INSERT #Tmp VALUES('595C4D7D-B9E9-4E29-85F5-05C414CA961E')
INSERT #Tmp VALUES('92DFF3AF-7B65-43EF-AA64-069AF84896AA')
INSERT #Tmp VALUES('A3638B49-4CF0-4046-8349-0AB12560CDE6')
INSERT #Tmp VALUES('BC1D4C15-8BDB-4E80-9853-0ADBFDE7BDE7')
INSERT #Tmp VALUES('5D5F2A56-98C2-407F-B416-0E19FC25505A')
INSERT #Tmp VALUES('764C582C-6BC2-47E6-8CDD-1AA65B7FE643')
INSERT #Tmp VALUES('E64E7241-4806-4B31-9C71-1E21B70FC8C3')
INSERT #Tmp VALUES('720502B5-7964-4D9F-969E-26D2415624AF')
INSERT #Tmp VALUES('D1E49B78-554B-4B74-9A33-2B1638471237')
INSERT #Tmp VALUES('010F9480-7583-4447-8392-2CE8414969C5')
INSERT #Tmp VALUES('5B1D8BBA-1E5F-4946-8750-2FCD2E73F7F6')
INSERT #Tmp VALUES('A9B554AB-92CD-409B-886F-3058DA87B9E5')
INSERT #Tmp VALUES('372E1FAF-25BB-4013-8204-32E86F18D6DB')
INSERT #Tmp VALUES('5F150296-DA16-4FB4-A80D-39F9A56BF1F3')
INSERT #Tmp VALUES('D58CEEA6-8ABE-494F-901C-3A91E2E779B0')
INSERT #Tmp VALUES('BD68E246-CA61-4F5B-9E81-3E788035D575')
INSERT #Tmp VALUES('47579C76-93A3-43BB-88E5-40372C075BEA')
INSERT #Tmp VALUES('AEEC89E6-1DF7-4C5F-B9C4-473540D762F7')
INSERT #Tmp VALUES('A3B81A59-4932-48F6-BFCB-4979AA21B39F')
INSERT #Tmp VALUES('61019FC9-AAC8-4AA1-99FD-4A96878D65E9')
INSERT #Tmp VALUES('66E83B6B-64A2-4ED2-8680-4B27C18FAA26')
INSERT #Tmp VALUES('0AC18832-75C8-4FE5-929E-555A9861B2A0')
INSERT #Tmp VALUES('E05BEFE5-8099-4F3D-9BA6-57CEDF0F82DE')
INSERT #Tmp VALUES('26264247-0CCD-4481-A3ED-58061DE0FD11')
INSERT #Tmp VALUES('6833500B-3B0D-4485-A1C0-61AA3406314D')
INSERT #Tmp VALUES('7775B6CE-B646-460C-9B44-62BC48974029')
INSERT #Tmp VALUES('8D694E5F-3116-4DE3-A786-62F87503019F')
INSERT #Tmp VALUES('40A2A655-C5D8-42A2-9DCE-63AACF4388CF')
INSERT #Tmp VALUES('2F4F5153-C59D-4765-B37D-66F9714A79FF')
INSERT #Tmp VALUES('7BBAC415-0CFC-4D96-94A4-68A6CE4085A3')
INSERT #Tmp VALUES('66666467-074F-4A86-9394-6DABA1264088')
INSERT #Tmp VALUES('9EDDCA09-92FA-4152-B9A0-6FEA47E8C7DF')
INSERT #Tmp VALUES('25F47E5F-5D0A-41FE-9E16-748A5E0EF413')
INSERT #Tmp VALUES('3CD38D86-E1B7-4FDA-AAF6-78B9154D65B5')
INSERT #Tmp VALUES('53F6B2DB-C17D-49C6-908F-7AA0A4810042')
INSERT #Tmp VALUES('C682A0D0-BF86-4A11-88C0-7BB9FA19571C')
INSERT #Tmp VALUES('272A03A5-88C2-4198-949E-7F118BC33C20')
INSERT #Tmp VALUES('D7E2F1EC-FF11-4808-A494-8392AD7E6C93')
INSERT #Tmp VALUES('E8D1CA90-CC1B-4E02-A0FF-87F992A526D8')
INSERT #Tmp VALUES('34C07CE3-C6E5-44EA-9744-89B9A11383ED')
INSERT #Tmp VALUES('68B14974-29AF-49DA-8124-8A922AE646D1')
INSERT #Tmp VALUES('5C0DE49C-5A26-4655-A9AD-8C70B1EB35E4')
INSERT #Tmp VALUES('9B915B28-5CB1-444B-808F-936ADEE38173')
INSERT #Tmp VALUES('A2999667-F04F-4721-AD65-94DDCEEC6694')
INSERT #Tmp VALUES('8358AC2F-23F6-4DBD-8639-9BED73549AE3')
INSERT #Tmp VALUES('92102880-FAC4-42A5-A034-9D5A67600BE9')
INSERT #Tmp VALUES('97E8CC4A-8D39-426C-B206-9DB7B6235BCD')
INSERT #Tmp VALUES('4073C546-0FF1-4F1E-A573-9F570117D146')
INSERT #Tmp VALUES('CE403006-4BFD-4E0B-855A-A65476E5D3BE')
INSERT #Tmp VALUES('0AD4088C-F428-4836-A1C4-A7E40C836EA3')
INSERT #Tmp VALUES('CC6BA3F9-20F6-451D-A4C1-B0E606AD1C31')
INSERT #Tmp VALUES('2DB472C0-A265-4B85-9508-B6A54759DE55')
INSERT #Tmp VALUES('7449A438-BEEC-480B-9730-B8DF3B16D222')
INSERT #Tmp VALUES('90622821-A261-4FE2-A87A-BE34C38652A7')
INSERT #Tmp VALUES('FAFE0B93-C1FC-4C81-BACF-BFCA4DF41F98')
INSERT #Tmp VALUES('7EBBEB48-5650-47DD-BF6E-C5124EF81AF4')
INSERT #Tmp VALUES('8C7DD83B-BA67-4F99-A8F8-CA92540BC65F')
INSERT #Tmp VALUES('3BB693B2-6BFE-4ABD-8CE5-CED3B657C181')
INSERT #Tmp VALUES('B190229D-55FD-4847-9E2C-CF4F66512361')
INSERT #Tmp VALUES('C02E2FAE-3BF6-4D68-B0B9-DD788EC60856')
INSERT #Tmp VALUES('CF090A1A-98D3-43B7-A5CF-DE48B545123C')
INSERT #Tmp VALUES('D99B7C1C-C2E0-40A2-80D4-EA5F5B241C6E')
INSERT #Tmp VALUES('F7A26643-8CA7-41CB-8C65-ED5D6E458221')
INSERT #Tmp VALUES('52626FAB-5492-441D-B01A-EF052823FA42')
INSERT #Tmp VALUES('8437948F-00A3-4578-8172-F2CB38409E3C')
INSERT #Tmp VALUES('0E16ACD9-745B-4298-BADB-F398A7E3FF68')
INSERT #Tmp VALUES('B0A10238-3ACE-4260-AFBA-F6BFF76F0D28')
INSERT #Tmp VALUES('159963DE-DE40-4FB6-8FA0-FAA23DC0536C')

OPEN cur1

fetch next from cur1 into @nnn 

while @@fetch_status=0
begin

exec('select * from ' +@nnn+
' where md_key in (
    select [kkk] from #Tmp
)')

fetch next from cur1 into @nnn 
end

close cur1
DEALLOCATE cur1

 

posted @ 2015-08-03 12:03  三台  阅读(501)  评论(0编辑  收藏  举报