SQL stored procedure Loop while

Source: Internet
Author: User

CREATE PROCEDURE [owner.] stored procedure name [; program number] [Parameter # #,... Parameter #1024] [with {recompile | encryption | recompile, encryption}] [the FOR replication] as program row where stored procedure name cannot be more than 128 words. Up to 1024 parameters per stored procedure (SQL Server 7.0 version above) parameters are used as follows: @ parameter name data type [varying] [= default] [output] before each parameter name, you must have an "@" symbol, and each stored procedure parameter is only the Within the program, the type of the parameter is available in addition to image, and is supported by other SQL Server data types.

DECLARE @mycounter int
Set @mycounter = 0/* Setting variable * *
while (@mycounter < 2)/* Set the number of cycles *
Begin
WAITFOR DELAY ' 000:00:10 ' * Delay Time 10 sec * *
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
FET CH Next from CCC to @a,@b
while (@ @fetch_status =0)
Begin
....
End
Close CCC
Declare CCC (this operation to see how other system stored procedures are written is not too clear, each time it is copy)

Probably just like this. Don't know if the grammar is wrong. Look at the system stored procedures to understand how to use the cursor the

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.