...... N words are omitted here ...
DROP PROCEDURE Test_proc; CREATE PROCEDURE Test_proc as DECLARE @u_id INT;D eclare @u_name VARCHAR (255); DECLARE @age INT;--Declares the parameter in the cursor--select, must be the same as the variable name that the cursor takes out DECLARE mycursor cursor for SELECT u_id,u_name,age from U_us;--Open cursor O PEN MyCursor;--fetching data from a cursor, assigning to the 3 variables just defined fetch NEXT from mycursor into @u_id, @u_name, @age;--judging the state of the cursor--0 FETCH statement successful--1 fetch language A sentence fails or the row is not in the result set--2 fetched rows do not exist while (@ @fetch_status = 0) BEGIN
--print control output, be sure to turn to the same type print (' Number: ' +convert (varchar (one), @u_id) + ', name: ' [email protected]_name+ ', Age: ' + CONVERT (varchar (one), @age)); FETCH NEXT from MyCursor to @u_id, @u_name, @age; END; --Closing the cursor close mycursor; --Remove cursor deallocate mycursor;
...... N words are omitted here ...
--print control output, be sure to turn to the same type print (' Number: ' +convert (varchar (one), @u_id) + ', name: ' [email protected]_name+ ', Age: ' + CONVERT (varchar (one), @age));
SQL Server stored procedure cursor usage, print output problem