ProcessingSequentiallyThroughaSetofRecords
来源:互联网 发布:网络单机游戏大全论坛 编辑:程序博客网 时间:2024/05/22 01:56
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
T-SQLProgrammingPart3-ProcessingSequentiallyThroughaSetofRecords
ByGregoryA.Larsen
Atsomepointyouwillhavesomebusinesslogicthatwillrequireyoutoprocesssequentiallythroughasetofrecordsonerecordatatime.Forexampleyoumayhavealistofdatabases,andforeachdatabaseyoumaywanttobuildacommandthatwillperformsomeprocessagainsteachdatabase.Oryoumighthaveasetofrecordswhereyouwanttoprocessthrougheachrecordoneatatime,soyoucanselectadditionalinformationfromanothertablebasedontheinformationcontainedineachrecord.Thisarticlewilldiscusstwodifferentwaystoprocessthroughasetofrecordsonerecordatatime.
UsingaCursor
ThefirstmethodIwilldiscussusesacursortoprocessthroughasetofrecordsonerecordatatime.Acursorisbasicallyasetofrowsthatyoudefinebasedonarecordsetreturnedfromaquery.Acursorallowsapplicationsamechanismtoprocessthrougharesultsetonerowatatime.Withacursoranapplicationisallowedtopositionitselftoaspecificrow,scrollbackandforth,andanumberofotherthings.Itwouldtakeaseriesofarticlestodescribeallthefunctionalityofacursor.ForthepurposeofthisarticleI'monlygoingtofocusonhowtousethedefaultscrollingfunctionalityofacursor.Thisdefaultfunctionalitywillonlyreadfromthefirstrowtothelastrowinacursor,onerowatatime.Iwillleaveadditionalcursortopicstoanotherarticleseries.
TodefineacursortheDECLARECURSORstatementisused.HereisthebasicformatforthesimplecursortopicIwillbediscussinginthisarticle.
DECLAREcursor_nameCURSORFORselect_statement
Thecursor_nameisthenameyouwanttoassociatewiththecursor.Theselect_statementisthequerythatwilldeterminetherowsthatmakeupthecursor.Notethereareotherparameters/optionsassociatedwiththeDECLARECURSORstatementthathelpdefinemorecomplicatedcursorprocessingthanIwillbecoveringinthisarticle.FortheseadditionaloptionspleasereadMicrosoftBooksOnline.
Let'sreviewafairlysimplecursorexample.Thisexamplewilldefineacursorthatcontainsthetop5Customer_Id'sintheCustomertableintheNorthwinddatabase.ItwillthenprocessthrougheachrecorddisplayingarownumberandtheCustomerIDforeach.Hereisthecodetodothis.
declare@CustIdnchar(5)declare@RowNumintdeclareCustListcursorforselecttop5CustomerIDfromNorthwind.dbo.CustomersOPENCustListFETCHNEXTFROMCustListINTO@CustIdset@RowNum=0WHILE@@FETCH_STATUS=0BEGINset@RowNum=@RowNum+1printcast(@RowNumaschar(1))+''+@CustIdFETCHNEXTFROMCustListINTO@CustIdENDCLOSECustListDEALLOCATECustList
HerearetheresultsthataregeneratedfromtheprintstatementwhenIrunitagainstmyNorthwindDatabase.
1ALFKI2ANATR3ANTON4AROUT5BERGS
Let'slookattheabovecodeinalittlemoredetail.Ifirstdeclaredacursorcalled"CustList".The"CustList"cursorispopulatedusingaSELECTstatementthatusestheTOPclausetoreturnonlythetop5CustomerId's.Nextthecursorisopened.Eachrecordinthe"CustList"cursorisretrieved,onerecordatatime,usingthe"FETCHNEXT"nextstatement.The"FETCHNEXT"statementpopulatesthelocalvariable@CustIDwiththeCustomerIDofthecurrentrecordbeingfetched.The@@FETCH_STATUSvariablecontrolswhethertheWHILEloopisexecuted.@@FETCH_STATUSissettozerowhenarecordissuccessfullyretrievedfromthecursor"CustList".InsidetheWHILEloopthe@RowNumvariableisincrementedby1foreachrecordprocessed.ThecalculatedRowNumberand@CustIdarethenprintedout.Lastly,a"FETCHNEXT"statementisusedtoretrievethenextrowbeforethenextcycleoftheWHILEloop.Thisprocesscontinuesonerecordatatimeuntilallrecordsincursor"CustList"havebeenprocessed.1<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
November19,2003T-SQLProgrammingPart3-ProcessingSequentiallyThroughaSetofRecords
ByGregoryA.Larsen
Atsomepointyouwillhavesomebusinesslogicthatwillrequireyoutoprocesssequentiallythroughasetofrecordsonerecordatatime.Forexampleyoumayhavealistofdatabases,andforeachdatabaseyoumaywanttobuildacommandthatwillperformsomeprocessagainsteachdatabase.Oryoumighthaveasetofrecordswhereyouwanttoprocessthrougheachrecordoneatatime,soyoucanselectadditionalinformationfromanothertablebasedontheinformationcontainedineachrecord.Thisarticlewilldiscusstwodifferentwaystoprocessthroughasetofrecordsonerecordatatime.
UsingaCursor
ThefirstmethodIwilldiscussusesacursortoprocessthroughasetofrecordsonerecordatatime.Acursorisbasicallyasetofrowsthatyoudefinebasedonarecordsetreturnedfromaquery.Acursorallowsapplicationsamechanismtoprocessthrougharesultsetonerowatatime.Withacursoranapplicationisallowedtopositionitselftoaspecificrow,scrollbackandforth,andanumberofotherthings.Itwouldtakeaseriesofarticlestodescribeallthefunctionalityofacursor.ForthepurposeofthisarticleI'monlygoingtofocusonhowtousethedefaultscrollingfunctionalityofacursor.Thisdefaultfunctionalitywillonlyreadfromthefirstrowtothelastrowinacursor,onerowatatime.Iwillleaveadditionalcursortopicstoanotherarticleseries.
TodefineacursortheDECLARECURSORstatementisused.HereisthebasicformatforthesimplecursortopicIwillbediscussinginthisarticle.
DECLAREcursor_nameCURSORFORselect_statement
Thecursor_nameisthenameyouwanttoassociatewiththecursor.Theselect_statementisthequerythatwilldeterminetherowsthatmakeupthecursor.Notethereareotherparameters/optionsassociatedwiththeDECLARECURSORstatementthathelpdefinemorecomplicatedcursorprocessingthanIwillbecoveringinthisarticle.FortheseadditionaloptionspleasereadMicrosoftBooksOnline.
Let'sreviewafairlysimplecursorexample.Thisexamplewilldefineacursorthatcontainsthetop5Customer_Id'sintheCustomertableintheNorthwinddatabase.ItwillthenprocessthrougheachrecorddisplayingarownumberandtheCustomerIDforeach.Hereisthecodetodothis.
declare@CustIdnchar(5)declare@RowNumintdeclareCustListcursorforselecttop5CustomerIDfromNorthwind.dbo.CustomersOPENCustListFETCHNEXTFROMCustListINTO@CustIdset@RowNum=0WHILE@@FETCH_STATUS=0BEGINset@RowNum=@RowNum+1printcast(@RowNumaschar(1))+''+@CustIdFETCHNEXTFROMCustListINTO@CustIdENDCLOSECustListDEALLOCATECustList
HerearetheresultsthataregeneratedfromtheprintstatementwhenIrunitagainstmyNorthwindDatabase.
1ALFKI2ANATR3ANTON4AROUT5BERGS
Let'slookattheabovecodeinalittlemoredetail.Ifirstdeclaredacursorcalled"CustList".The"CustList"cursorispopulatedusingaSELECTstatementthatusestheTOPclausetoreturnonlythetop5CustomerId's.Nextthecursorisopened.Eachrecordinthe"CustList"cursorisretrieved,onerecordatatime,usingthe"FETCHNEXT"nextstatement.The"FETCHNEXT"statementpopulatesthelocalvariable@CustIDwiththeCustomerIDofthecurrentrecordbeingfetched.The@@FETCH_STATUSvariablecontrolswhethertheWHILEloopisexecuted.@@FETCH_STATUSissettozerowhenarecordissuccessfullyretrievedfromthecursor"CustList".InsidetheWHILEloopthe@RowNumvariableisincrementedby1foreachrecordprocessed.ThecalculatedRowNumberand@CustIdarethenprintedout.Lastly,a"FETCHNEXT"statementisusedtoretrievethenextrowbeforethenextcycleoftheWHILEloop.Thisprocesscontinuesonerecordatatimeuntilallrecordsincursor"CustList"havebeenprocessed.1<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- ProcessingSequentiallyThroughaSetofRecords
- gridview 的技巧
- [收藏]使用ReportingServices中的窗体身份验证
- [收藏]利用ReportingServices从应用程序生成用户友好的报表
- 游戏界面缩放后屏幕抖动的问题
- Hello world
- ProcessingSequentiallyThroughaSetofRecords
- BuildingaT-SQLLoop
- Statspack之初体验
- --竖表变行表
- 关于scn的理解
- 如何将数据表导出备份到excel表格
- Foxmail 6.5 Beta 2 发布了
- 为什么用ROUND四舍五入总是无法得到正确数值?
- Wml的标准函数库