devxlogo

Generating a Sequence of Numbers in SQL Server

You can use this method to fill the dropdown lists on a Web page, Combo box, or List box in a Windows application.

First, make the lists data-bound to a query or stored procedure.

CREATE PROCEDURE GetSequenceOfNumbers	@FirstNumber INT, @LastNumber INTASBEGIN--Validation -- you can also add code to see if the @FirstNumber is less than @LastNumber--Create the temporary tableCREATE TABLE #TEMP_SEQUENCE(DisplayLabel VARCHAR(10), Value INT)--Insert all the values between specified range..WHILE @LastNumber >= @FirstNumberBEGIN	INSERT INTO #TEMP_SEQUENCE VALUES (CAST(@FirstNumber AS VARCHAR), @FirstNumber)	SELECT @FirstNumber = @FirstNumber + 1END--return the values from temporary tableSELECT DisplayLabel, VALUE FROM #TEMP_SEQUENCEENDGO

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.