Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 28th, 2005, 04:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need incrementing number in query

I need to have a field in my query that is an icremental number like 1, 2, 3, 4 etc, but that I can start at a specific value, say 236 so it would number the returned set of records as 237, 238, 239 etc

Any code examples?
__________________
Mitch
 
Old February 28th, 2005, 05:00 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Can you explain in more detail what it is exactly you want to do?

Thanks,
Jim
 
Old February 28th, 2005, 05:14 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

may be identity with seed equal to specific value you want to begin with.

 
Old March 1st, 2005, 04:22 AM
Authorized User
 
Join Date: Feb 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aaddressing to your problem considering the SQL server 2000 database

Create two functions: one for creating global variable object and other to get the running serial number....and use running serial number function in any query.

CREATE FUNCTION dbo.CreateGlobalObject()
RETURNS integer AS
BEGIN
    DECLARE @objReturn integer
    EXECUTE sp_OACreate 'VBScript.RegExp', @objReturn OUTPUT
    EXECUTE sp_OASetProperty @objReturn, 'Pattern', ''
    RETURN @objReturn
END
GO

CREATE FUNCTION dbo.RunningSerialNo(@ObjGlobal integer,@StartNumber integer)
RETURNS INTEGER
AS
BEGIN
    DECLARE @ReturnNumber INTEGER, @ObjTemp varchar(1000)
    IF @StartNumber IS NULL
        SET @StartNumber = 1
    EXECUTE sp_OAGetProperty @ObjGlobal, 'Pattern', @ObjTemp output
    IF @ObjTemp = 0 AND @StartNumber <> 0
        SET @ReturnNumber = @StartNumber
    ELSE
        SET @ReturnNumber = convert(numeric, '0'+@ObjTemp) + 1
    SET @ObjTemp = convert(varchar(1000), @ReturnNumber)
    EXECUTE sp_OASetProperty @ObjGlobal, 'Pattern', @ObjTemp
    RETURN @ReturnNumber
END
GO

/************************************************** *********/
DECLARE @a INT
SET @a = dbo.CreateGlobalObject()
SELECT dbo.RunningSerialNo(@a,236) SerialNo,* FROM <TABLE_NAME>
/************************************************** *********/

Cheers,
Pooja Falor





Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Number Function Query rsm42 ASP.NET 1.0 and 1.1 Basics 7 May 16th, 2007 05:04 AM
Update query: Editing phone number fields? monacle Access 2 February 27th, 2007 08:55 AM
Auto Number query again Brendan Bartley Access 4 August 26th, 2006 06:09 AM
Query database with large number of records andyj00 ASP.NET 1.0 and 1.1 Professional 6 June 27th, 2005 08:47 PM
How do you query for number of occurences? karjagis SQL Language 1 January 27th, 2005 10:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.