简体   繁体   中英

How to use Loop Stored procedure in SQL Server?

I have a classes table in which I need to run stored procedure (SQL Server) in loop of class ie, it should run 5 times here because maximum class in 5th standard in table. On every loop status field should update as pass or fail.

Kind of table is mentioned below.

Table Name: classes

在此处输入图片说明

Stored procedure:

DECLARE @counter INT = 1;

SELECT count(class)
FROM classes
GROUP BY class;

WHILE @counter <= * * [here I need count of distinct class] * *;//i.e, here 5 
BEGIN
    PRINT @counter;

    UPDATE classes
    SET STATUS = "Pass"
    WHERE class = * * [here I need class value] * *;

    SET @counter = @counter + 1;
END

Please help me to run this stored procedure.

Are you sure you don't just want an UPDATE statement?

UPDATE classes
SET [Status] = CASE WHEN [Class] = 'I' THEN 'Pass'
                    WHEN [Class] = 'II' THEN 'Fail'
.... etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM