main logo
Subject: sp_newID()
Author: Bob Archer
Posted: 2002/10/23 18:47:30
 
View Entire Thread
New Search


There never was one supplied with Codebook, but I will be happy to share mine with ya:

CREATE proc dbo.sp_NewID
@basetablename varchar(30)
AS
Declare @returnNewID int

SET XACT_ABORT ON
SET NOCOUNT ON
BEGIN TRANSACTION

SELECT @basetablename = LOWER( AT basetablename)

IF NOT EXISTS (SELECT *
FROM sy_uniqueid
WHERE sy_uniqueid DO.T tablename = @basetablename)
BEGIN
INSERT INTO sy_uniqueid
VALUES (@basetablename,0)
END

UPDATE sy_uniqueid
SET last_id = last_id + 1
, AT returnNewID = last_id
WHERE sy_uniqueid DO.T tablename = @basetablename

select AT returnNewID

COMMIT TRANSACTION

RETURN

*********
I kept getting that this was a duplicate message when I tried to reply to the message below DO.T

This requires a table, here is a script for it:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sy_uniqueid]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[sy_uniqueid]
GO

CREATE TABLE [dbo].[sy_uniqueid] (
[tablename] [varchar] (30) NOT NULL ,
[last_id] [int] NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[sy_uniqueid] WITH NOCHECK ADD
CONSTRAINT [PK_sy_uniqueid] PRIMARY KEY CLUSTERED
(
[tablename]
) ON [PRIMARY]
GO

 
©2002 Bob Archer
<-- Prior Message New Search Next Message -->