Automatic Generation of numbers by multiple users in sqlserver

Source: Internet
Author: User
Tags rtrim

Automatic Generation of numbers by multiple users in sqlserver
If not exists (select * From DBO. sysobjects where id = object_id (n' [indextable] ') and objectproperty (ID, n'isusertable') = 1)
Create Table indextable (ex char (20), num integer)


Create procedure setindex @ ex char (20), @ result char (30) output, @ FMT integer
Declare @ num char (10)
Set nocount on
If not exists (select num from indextable where ex = @ ex)
Insert into indextable values (@ ex, 1)
Update indextable set num = num + 1 where ex = @ ex
Select @ num = cast (Num as char (10) 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


Call in Delphi

Procedure tform1.button1click (Sender: tobject );
Storedproc1.parambyname ('@ Ex'). asstring: = 'user ';
Storedproc1.parambyname ('@ FMT'). asinteger: = 3;
Showmessage (storedproc1.parambyname ('@ result'). value)

The @ ex parameter indicates the prefix, @ FMT indicates the number length, and @ result indicates the returned data.
Returns user001

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: 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.