Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 June 21st, 2006, 09:13 PM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to auto increment ?

Hi,I'm having a problem auto incrementing my reference number. I have created a user request form, every time a user clicks save, the data entered by the user will be stored in the database and the reference number will auto increment.
The problem here is that my reference number is char(9), for example, 2006-0001, 2006-0002, etc until it reaches 2006-9999. I have created a stored procedure.How am I going to auto increment my reference number.Please help, i'm useing VS 2005 VB.

 I do I make use of this code,
intTemp = cint(right(strMyString,4))
intTemp=intTemp + 1

strMyNewString = "2006-" & cstr(intTemp)


Below is the stored procedure.
CREATE PROCEDURE [dbo].[My_SSRF_Insert]
    @RequestNo char(9),
    @System char(30),
    @Module char(9),
    @Description char(255),
    @Priority char(6),
    @Approver char(30),
    @Status char(6)

AS

---//DECLARE @RequestNo char(9)
SELECT MAX(RequestNo) FROM My_SSRF


-----///SET NOCOUNT ON

INSERT INTO [My_SSRF]
(
            [RequestNo],
            [System],
            [Module],
            [Description],
            [Priority],
            [Approver],
                            [Status]

)
VALUES
(
            @RequestNo,
            @System,
            @Module,
            @Description,
            @Priority,
            @Approver,
            @Status

)
GO

 
Old June 22nd, 2006, 02:52 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

-- prepare test data
declare @test table (t varchar(50))

insert @test
select '2006-0001' union all
select '2006-0049'

-- show data before inserting new values
select * from @test

declare @number varchar(50)

select @number = '2006-' + right('0000' + convert(varchar, t.n + 1), 4)
from (
        select max(convert(int, substring(t, 6, 50))) n
        from @test
    ) t

-- show new number
select @number

 
Old June 22nd, 2006, 03:00 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or more generic, use this code!

-- prepare test data
declare @test table (t varchar(50))

insert @test
select '2006-0001' union all
select '2007-0012' union all
select '2005-4004' union all
select '2005-0123' union all
select '2007-0002' union all
select '2006-0049'

-- show data before inserting new values
select * from @test

declare @number varchar(50),
    @Year smallint

select @year = 2005

select @number = cast(@year as char(4)) + '-' + right('0000' + convert(varchar, t.n + 1), 4)
from (
        select max(convert(int, substring(t, 6, 50))) n
        from @test
    ) t

-- show new number
select @year 'Year', @number 'Next serial'





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding auto increment of id Rajesh225 ASP.NET 1.0 and 1.1 Basics 1 June 27th, 2007 05:35 PM
Auto increment prad_a MySQL 3 April 7th, 2007 05:47 AM
How to auto increment? Shawn Mohan ASP.NET 2.0 Basics 6 June 20th, 2006 10:36 PM
Auto-increment using VBScript CraigKornacki VB How-To 1 April 6th, 2004 02:17 PM
auto increment? hosefo81 PHP Databases 1 February 2nd, 2004 03:56 PM





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