Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
SQL
USE [RealEstate]
GO
/****** Object: StoredProcedure [dbo].[SP_AutoNumber] Script Date: 17/04/2014 12:00:31 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 

 
ALTER PROCEDURE [dbo].[SP_AutoNumber]
@code int,
@MaxValue int,
@LastValue int ,
@strGUID uniqueidentifier
 
AS
 
(select @MaxValue = max(RealEstateNumber) from RlRegistrationRealEstate) 
 
if @MaxValue is null set @MaxValue = '0001' set @LastValue = right(@MaxValue,3) + 1 return right('000' + convert(varchar(4),@lastValue),4) set @MaxValue= @MaxValue + @LastValue
 

UPDATE [dbo].RlRegistrationRealEstate SET RealEstateNumber=@MaxValue WHERE GUID = @strGUID
Posted
Updated 18-Apr-14 5:50am
v2
Comments
PIEBALDconsult 18-Apr-14 12:10pm    
SR0016: Avoid using sp_ as a prefix for stored procedures
http://msdn.microsoft.com/en-us/library/dd172115(v=vs.100).aspx
http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix
http://www.dotnetspider.com/resources/2445-Don-t-prefix-user-defined-stored-procedure-wi-S.aspx
[no name] 18-Apr-14 12:13pm    
http://www.codeproject.com/Questions/760607/Auto-Generate-Number-In-Sql-I-No-Display-Like-This

1 solution

It looks like you are confused about when to use integer values and varchar values.

An integer will contain the number. The normal presentation of the number doesn't include leading zeros. Setting @MaxValue = '0001' will give @MaxValue the value of 1. The leading zeros are irrelevant for integers.

If you wish to display an integer with leading zeros, then it needs to be converted to a string somewhere. Normally, this would be done in the UI presentation (ie the web page) rather than the database.

If you wish to store the data with the leading zeros in the database, then the datatypes need to be varchar rather than integer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900