Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Intermediate
Oct 26, 2004

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 INT
AS
BEGIN

--Validation 
-- you can also add code to see if the @FirstNumber is less than @LastNumber

--Create the temporary table
CREATE TABLE #TEMP_SEQUENCE(DisplayLabel VARCHAR(10), Value INT)

--Insert all the values between specified range..
WHILE @LastNumber >= @FirstNumber
BEGIN
	INSERT INTO #TEMP_SEQUENCE VALUES (CAST(@FirstNumber AS VARCHAR), @FirstNumber)

	SELECT @FirstNumber = @FirstNumber + 1
END

--return the values from temporary table
SELECT DisplayLabel, VALUE FROM #TEMP_SEQUENCE

END
GO
Raj Chidipudi
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap