SQL stored procedure loop while

Source: Internet
Author: User
Createprocedure [owner.] name [; program number] [(parameter #1, & hellip; parameter #1024)] [with {recompile | encryption | recompile, encryption}] [forreplication] as program line where the name of the stored procedure cannot exceed 128 words. In each stored procedure, a maximum of 1024 parameters can be set: create procedure [owner.] Name [; program number] [(parameter #1 ,... Parameter #1024)] [with {recompile | encryption | recompile, encryption}] [for replication] as program line where the stored procedure name cannot exceed 128 words. A maximum of 1024 parameters can be set in each stored procedure (SQL server 7.0 or later). The parameter usage is as follows: @ Parameter Name Data Type [varying] [= internal value] [output] each parameter name must have a "@" symbol before it. The parameters of each stored procedure are only used inside the program, the parameter type is applicable to data types supported by SQL server except image.

Declare @ mycounter int
Set @ mycounter = 0/* set variable */
While (@ mycounter <2)/* set the number of cycles */
Begin
Waitfor delay '000: 00: 10'/* delay Time: 10 seconds */
Insert into time_by_day
(Time_id, the_date, the_year, month_of_year, quarter, day_of_month)
Select top 1 time_id + 1 as time_id, the_date + 1 as the_date, year (the_date + 1)
As the_year, month (the_date + 1) as month_of_year, {fn quarter (the_date + 1)
} As quarter, day (the_date + 1) as day_of_month
From time_by_day
Order by time_id desc

Set @ mycounter = @ mycounter + 1
End

Use a cursor

Declare @ a int, @ B int
Declare ccc cursor for select * from test where id> @ id
Open ccc
Fetch next from ccc into @ a, @ B
While (@ fetch_status = 0)
Begin
.......
End
Close ccc
Declare ccc)

This is probably the case. I don't know if the syntax is correct. I haven't used sqlserver recently. I can see how to use cursor by looking at the system stored procedures.

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.