Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Record ID generator (need your opinion)
Message
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Titre:
Record ID generator (need your opinion)
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01093716
Message ID:
01093716
Vues:
90
I have developed this SP based in suggestions seen at this forum.
This code runs with any table and column of D.B.
I have done it to avoid to use the IDENTITY
In some conditions, I can not use IDENTITY.
I have tried it, and it works fine, but anyway, I would like to have your option and know that I can improve it.
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[Nextcode]
        @myTable varchar(50),
        @myColumn varchar(50)
AS

SET NOCOUNT ON

DECLARE 
    @return_val int,
    @sqlcmd nvarchar(100)

BEGIN TRAN t1
SET @sqlcmd = 'update ' + @myTable + ' set ' + @myColumn + ' = ' + @myColumn + ' + 1'
EXEC sp_executesql @sqlcmd
SET @sqlcmd = 'select ' + @myColumn + ' as myID from ' + @myTable
EXEC sp_executesql @sqlcmd, N'@return_val int output', @return_val output
if @@ERROR > 0
	BEGIN
		ROLLBACK TRAN t1
		SET @return_val = -1
		RETURN @return_val
	END
ELSE
	BEGIN
		COMMIT TRAN t1
		RETURN @return_val
	END
Thanks in advance
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform