SQL Procedure to Display First Nine Countries from Table
Here's a procedure that uses a cursor to retrieve the country ID and names from the 'country' table and displays the first nine records:
CREATE PROCEDURE display_first_nine_countries
AS
BEGIN
DECLARE @country_id INT
DECLARE @country_name VARCHAR(50)
DECLARE country_cursor CURSOR FOR
SELECT country_id, country_name
FROM country
OPEN country_cursor
FETCH NEXT FROM country_cursor INTO @country_id, @country_name
PRINT 'Country ID' + CHAR(9) + 'Country Name'
PRINT '----------' + CHAR(9) + '------------'
DECLARE @counter INT = 1
WHILE @@FETCH_STATUS = 0 AND @counter <= 9
BEGIN
PRINT CAST(@country_id AS VARCHAR(10)) + CHAR(9) + @country_name
FETCH NEXT FROM country_cursor INTO @country_id, @country_name
SET @counter = @counter + 1
END
CLOSE country_cursor
DEALLOCATE country_cursor
END
To execute the procedure, simply call it like any other stored procedure:
EXEC display_first_nine_countries
This will display the country ID and name for the first nine records from the 'country' table. Note that you can modify the procedure to display a different number of records by changing the @counter limit in the WHILE loop.
原文地址: https://www.cveoy.top/t/topic/ohoz 著作权归作者所有。请勿转载和采集!