The process of automatically generating a number in SQL Server for multiple users

Source: Internet
Author: User
Tags integer rtrim
server|sqlserver| process

The process of automatically generating a number in SQL Server for multiple users
If not EXISTS (SELECT * from dbo.sysobjects WHERE id = object_id (n ' [indextable] ') and OBJECTPROPERTY (ID, n ' isusertable ') = 1)
CREATE TABLE indextable (Ex char (), num integer)

Go

CREATE PROCEDURE Setindex @Ex char (), @result char output, @Fmt integer
As
Declare @num char (10)
SET NOCOUNT on
If not EXISTS (select num from indextable where ex= @ex)
INSERT into indextable values (@ex, 1)
Else
Update indextable set num=num+1 where ex= @ex
Select @num =cast (num as char) from indextable where ex= @ex
Select @num =space (@fmt-len (@num)) + @num
Select @num =replace (@num, ', ' 0 ')
Select @result =rtrim (@ex) +rtrim (@num)
SET NOCOUNT off
Go

--------

Call in Delphi

Procedure Tform1.button1click (Sender:tobject);
Begin
Storedproc1.parambyname (' @Ex '). asstring:= ' User ';
Storedproc1.parambyname (' @fmt '). asinteger:=3;
Storedproc1.execproc;
ShowMessage (Storedproc1.parambyname (' @result '). Value)
End

-----------
The parameter @ex represents the prefix, @fmt represents the number length, @result represents the return data
Back to User001



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.