使用存储过程

时间:2017-02-15 17:50:36

标签: c# asp.net sql-server

我已经实现了一个过程,我为每个记录插入传递了用户自己的唯一ID。但问题是,当同时发生多次命中时,将向所有用户分配唯一ID。

我的程序代码是

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[name]
    (
     --@reg_id int,
     @name nvarchar(50),    
     @designation int,
     @deptmntid int,
     --@officeid int,
     @disid int,
     @bentype_id int,
     @benificry_type nvarchar(50),
     @dob datetime,
     @basic_pay int,
     @home_address nvarchar(200),
     @mobile nvarchar(50),
     @email nvarchar(50),
     @aadhar nvarchar(50),
     @applicant_image image,
     @ip nvarchar(50),
     @udate datetime,
     @ddoid int,
     @depend_id int,
     @relation int,
     @flag int,
     @officename nvarchar(max),
     @deptother nvarchar(max),
     @desigother nvarchar(max),
     @regno bigint output,
     @flg int output)
AS
    SET NOCOUNT ON;

    DECLARE @regnc char(6);
    DECLARE @numm varchar(7);
    DECLARE @regnc_dcd varchar(11);
    DECLARE @final_appno varchar(11);
    DECLARE @bentype int;
    DECLARE @ddotocode int;
    DECLARE @ddochk int;
    DECLARE @regidchk bigint;   

    BEGIN TRANSACTION

        SELECT @ddochk = ddoid 
        FROM tablename 
        WHERE ddoid = @ddoid;

        IF (@ddochk IS NULL)
        BEGIN
            SET @numm = '1';
            SET @regnc = STUFF(@numm, 1, 0, REPLICATE('0', 6 - LEN(@numm)));
            SET @regnc_dcd = cast(@ddoid as varchar(4)) + cast(@bentype_id as varchar(1)) + cast(@regnc as varchar(6)); 
            SET @final_appno = @regnc_dcd
        END
        ELSE
        BEGIN
            SELECT 
                @numm = MAX(ISNULL(auto_inc, 0) + 1),
                @ddotocode = ddoid 
            FROM 
                tablename 
            WHERE
                ddoid = @ddoid;

            SET @regnc = STUFF(@numm, 1, 0, REPLICATE('0', 6 - LEN(@numm)));
            SET @regnc_dcd=cast(@ddotocode as varchar(4)) + cast(@bentype_id as varchar(1)) + cast(@regnc as varchar(6)); 
            SET @final_appno = @regnc_dcd
        END

        BEGIN
            INSERT INTO tablename (reg_id, auto_inc, name, desigid, deptid, distid, bentype_id, benificry_type, dob, basic_pay, home_address, mobile, email, aadhar, ip, udate, ddoid, applicant_image, flag, officename, deptother, desigother) 
            VALUES (@final_appno, @regnc, @name, @designation, @deptmntid, @disid, @bentype_id, @benificry_type, @dob, @basic_pay, @home_address, @mobile, @email, @aadhar, @ip, @udate, @ddoid, @applicant_image, @flag, @officename, @deptother, @desigother)

            SET @regno = @final_appno;
        END

        IF @@ERROR <> 0
        BEGIN
            ROLLBACK
            -- Raise an error and return
            RAISERROR ('Error in Inserting record from temp.....', 16, 1)
            RETURN
        END

        BEGIN   
            INSERT INTO SHCbeneficiary(card_id, depend_id, pic, name, dob, relation, ip, aadhar, udate) 
            VALUES (@final_appno, @depend_id, @applicant_image, @name, @dob, @relation, @ip, @aadhar, @udate)

            IF @@ERROR <> 0
            BEGIN
               -- Rollback the transaction
               ROLLBACK

               -- Raise an error and return
               RAISERROR ('Error in updating record in persoaldetails....!!!', 16, 1)
               RETURN
            END

            SET @flg = 1;
        END

        COMMIT TRANSACTION

我想检查重复数据,直到在表中找不到唯一ID。 这意味着我想在程序中使用递归。

此外,我还使用了CTE,标量值函数来表示唯一性,但效果不佳。请帮忙

1 个答案:

答案 0 :(得分:0)

如果您使用的是SQL Server 2012或更高版本,则可以使用SEQUENCES,这是一个生成一系列数字的新用户定义对象。例如:

CREATE SEQUENCE dbo.MySequence
    AS int
    START WITH 1
    INCREMENT BY 1;

您可以使用查询检索顺序值,如下所示:

SELECT NEXT VALUE FOR dbo.MySequence AS [Value];

以下是有关更多信息的技术文章:

https://technet.microsoft.com/en-us/library/ff878058(v=sql.110).aspx