Stored Procedure example-temporary table instead of cursor

Source: Internet
Author: User
Tags stored procedure example
  /*  
Mobile phone activity statistics
*/
Create Proc Usp_actioncount
@ Planid Int ,
@ Updatetype Varchar ( 50 )
As
-- ------------------------------------------------------------------
Create Table # Tempa (
ID Int Identity ( 1 , 1 ) Not Null ,
Mobile Varchar ( 20 ) Not Null ,
Updatetype Varchar ( 50 )
)
Create Table # Tempb (
ID Int Identity ( 1 , 1 ) Not Null ,
Mobile Varchar ( 20 ) Not Null ,
Updatetype Varchar ( 20 )
)
Insert Into # Tempa (mobile, updatetype) Select Distinct Msisdn, '' From Tb_planmsisdn Where Plan_id = @ Planid
Declare @ ID Int , @ Mobile Varchar ( 20 )
Select @ ID = Count ( 1 ) From # Tempa Ta
While @ ID > 0
Begin
Select @ Mobile = Mobile From # Tempa Ta Where Ta. ID = @ ID
Truncate Table # Tempb
Insert Into # Tempb (mobile, updatetype) Select Distinct Msisdn, updatetype From Tb_planmsisdn TPM Where TPM. msisdn = @ Mobile And TPM. plan_id = @ Planid
Declare @ Innerid Int , @ Utype Varchar ( 20 ), @ Innermobile Varchar ( 20 )
Select @ Innerid = Count ( 1 ) From # Tempb TB
While @ Innerid > 0
Begin
Select @ Utype = Updatetype, @ Innermobile = TB. Mobile From # Tempb TB Where TB. ID = @ Innerid
If Charindex ( @ Utype , @ Updatetype ) > 0
Begin
Update # Tempa Set Updatetype = Updatetype + Case @ Utype
When ' 1 ' Then ' Periodic update '
When ' 2 ' Then ' Shutdown '
When ' 3 ' Then ' Start '
When ' 4 ' Then ' Enter the residential area '
When ' 5 ' Then ' Paging response '
When ' 6 ' Then ' Text message '
When ' 7 ' Then ' Caller '
When ' 8 ' Then ' SMS receiving '
Else ''
End Where Mobile = @ Innermobile

End
Set @ Innerid = @ Innerid - 1
End
Set @ ID = @ ID - 1
End
Select * From # Tempa Ta
Drop Table # Tempb
Drop Table # Tempa
Go
 
 

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.