我的存储过程有什么问题?请

时间:2016-05-03 21:58:00

标签: sql-server stored-procedures

这是我的存储过程:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[PROC_AgregarUsuario]
    @Email VARCHAR(100), 
    @SALT VARCHAR(100), 
    @PASS VARCHAR(100), 
    @PASSWORDECRYPT VARCHAR(100), 
    @Nuevo INT
AS
BEGIN
    DECLARE @USERID UNIQUEIDENTIFIER,
            @MensajeRegreso VARCHAR(8000),
            @CantidadContactosEmpresa INT,
            @EmpresaRFC char(13)

    SET @USERID = (SELECT [UserId] 
                   FROM [dbo].[aspnet_Users] 
                   WHERE [UserName] = @Email)

    IF @Nuevo IN (1, 2)
    BEGIN
        SET @CantidadContactosEmpresa = 
            (SELECT COUNT(*) 
             FROM [dbo].[EmpresaUser] 
             WHERE [EmpresaUser_Empresa] IN (SELECT [EmpresaUser_Empresa] 
                                             FROM [dbo].[EmpresaUser] 
                                             WHERE [EmpresaUser_Email] = @Email))
        IF @USERID IS NULL
        BEGIN
            INSERT INTO dbo.aspnet_Users(ApplicationId, UserId, UserName, LoweredUserName,
                                         MobileAlias, IsAnonymous, LastActivityDate)
                SELECT TOP 1 
                    ApplicationId, -- ApplicationId - uniqueidentifier
                    NEWID() , -- UserId - uniqueidentifier
                    @Email , -- UserName - nvarchar(256)
                    LOWER(@Email) , -- LoweredUserName - nvarchar(256)
                    NULL , -- MobileAlias - nvarchar(16)
                    0 , -- IsAnonymous - bit
                    GETDATE()  -- LastActivityDate - datetime
                FROM 
                    dbo.aspnet_Applications

            SET @USERID = (SELECT UserId FROM dbo.aspnet_Users 
                           WHERE UserName = @Email)

            INSERT INTO dbo.aspnet_Membership (ApplicationId, UserId, Password, PasswordFormat, PasswordSalt,
                                               MobilePIN, Email, LoweredEmail,
                                               PasswordQuestion, PasswordAnswer,
                                               IsApproved, IsLockedOut,
                                               CreateDate, LastLoginDate,
                                               LastPasswordChangedDate,
                                               LastLockoutDate,
                                               FailedPasswordAttemptCount ,
                                               FailedPasswordAttemptWindowStart,
                                               FailedPasswordAnswerAttemptCount,
                                               FailedPasswordAnswerAttemptWindowStart,
                                              Comment)
                SELECT 
                    ApplicationId , -- ApplicationId - uniqueidentifier
                    @USERID , -- UserId - uniqueidentifier
                    @PASSWORDECRYPT , -- Password - nvarchar(128)
                    1 , -- PasswordFormat - int
                    @SALT , -- PasswordSalt - nvarchar(128)
                    NULL , -- MobilePIN - nvarchar(16)
                    @Email , -- Email - nvarchar(256)
                    LOWER(@Email) , -- LoweredEmail - nvarchar(256)
                    'Pregunta' , -- PasswordQuestion - nvarchar(256)
                    @PASSWORDECRYPT , -- PasswordAnswer - nvarchar(128)
                    1 , -- IsApproved - bit
                    0 , -- IsLockedOut - bit
                    GETDATE() , -- CreateDate - datetime
                    GETDATE() , -- LastLoginDate - datetime
                    GETDATE() , -- LastPasswordChangedDate - datetime
                    '18990101' , -- LastLockoutDate - datetime
                    0 , -- FailedPasswordAttemptCount - int
                    '18990101' , -- FailedPasswordAttemptWindowStart - datetime
                    0 , -- FailedPasswordAnswerAttemptCount - int
                    '18990101', -- FailedPasswordAnswerAttemptWindowStart - datetime
                    @PASS  -- Comment - ntext
                FROM 
                    dbo.[aspnet_Applications]

        /*Rol Empresa*/

            INSERT INTO dbo.aspnet_UsersInRoles
                    ( UserId, RoleId )
            SELECT @USERID, -- UserId - uniqueidentifier
                      RoleId -- RoleId - uniqueidentifier
                  FROM dbo.aspnet_Roles WHERE RoleName IN ('Empresas')


        IF EXISTS (SELECT NULL FROM [dbo].[Empresa] WHERE [EMP_RFC] IN (SELECT [EmpresaUser_Empresa] FROM [dbo].[EmpresaUser] WHERE [EmpresaUser_Email] = @Email))
        BEGIN
            INSERT INTO dbo.aspnet_UsersInRoles
                    ( UserId, RoleId )
            SELECT @USERID, -- UserId - uniqueidentifier
                      RoleId -- RoleId - uniqueidentifier
                  FROM dbo.aspnet_Roles WHERE RoleName IN ('Empresas')
        END

            IF @CantidadContactosEmpresa = 1
            BEGIN
                INSERT INTO dbo.aspnet_UsersInRoles
                        ( UserId, RoleId )
                SELECT @USERID, -- UserId - uniqueidentifier
                          RoleId -- RoleId - uniqueidentifier
                      FROM dbo.aspnet_Roles WHERE RoleName IN ('Empresas')
            END
            SET @MensajeRegreso = 'Creo que funcionó ...'
    END
    ELSE
    BEGIN
        INSERT INTO dbo.aspnet_Users
                    ( ApplicationId ,
                      UserId ,
                      UserName ,
                      LoweredUserName ,
                      MobileAlias ,
                      IsAnonymous ,
                      LastActivityDate
                    )
            SELECT TOP 1 ApplicationId , -- ApplicationId - uniqueidentifier
                      NEWID() , -- UserId - uniqueidentifier
                      @Email , -- UserName - nvarchar(256)
                      LOWER(@Email) , -- LoweredUserName - nvarchar(256)
                      NULL , -- MobileAlias - nvarchar(16)
                      0 , -- IsAnonymous - bit
                      GETDATE()  -- LastActivityDate - datetime
                    FROM dbo.aspnet_Applications

            SET @USERID = (SELECT UserId FROM dbo.aspnet_Users WHERE UserName = @Email)

            INSERT INTO dbo.aspnet_Membership
                    ( ApplicationId ,
                      UserId ,
                      Password ,
                      PasswordFormat ,
                      PasswordSalt ,
                      MobilePIN ,
                      Email ,
                      LoweredEmail ,
                      PasswordQuestion ,
                      PasswordAnswer ,
                      IsApproved ,
                      IsLockedOut ,
                      CreateDate ,
                      LastLoginDate ,
                      LastPasswordChangedDate ,
                      LastLockoutDate ,
                      FailedPasswordAttemptCount ,
                      FailedPasswordAttemptWindowStart ,
                      FailedPasswordAnswerAttemptCount ,
                      FailedPasswordAnswerAttemptWindowStart ,
                      Comment
                    )
            SELECT ApplicationId , -- ApplicationId - uniqueidentifier
                      @USERID , -- UserId - uniqueidentifier
                      @PASSWORDECRYPT , -- Password - nvarchar(128)
                      1 , -- PasswordFormat - int
                      @SALT , -- PasswordSalt - nvarchar(128)
                      NULL , -- MobilePIN - nvarchar(16)
                      @Email , -- Email - nvarchar(256)
                      LOWER(@Email) , -- LoweredEmail - nvarchar(256)
                      'Pregunta' , -- PasswordQuestion - nvarchar(256)
                      @PASSWORDECRYPT , -- PasswordAnswer - nvarchar(128)
                      1 , -- IsApproved - bit
                      0 , -- IsLockedOut - bit
                      GETDATE() , -- CreateDate - datetime
                      GETDATE() , -- LastLoginDate - datetime
                      GETDATE() , -- LastPasswordChangedDate - datetime
                      '18990101' , -- LastLockoutDate - datetime
                      0 , -- FailedPasswordAttemptCount - int
                      '18990101' , -- FailedPasswordAttemptWindowStart - datetime
                      0 , -- FailedPasswordAnswerAttemptCount - int
                      '18990101', -- FailedPasswordAnswerAttemptWindowStart - datetime
                      @PASS  -- Comment - ntext
                    FROM dbo.[aspnet_Applications]

        /*Rol Empresa*/

            INSERT INTO dbo.aspnet_UsersInRoles
                    ( UserId, RoleId )
            SELECT @USERID, -- UserId - uniqueidentifier
                      RoleId -- RoleId - uniqueidentifier
                  FROM dbo.aspnet_Roles WHERE RoleName IN ('Empresas')


        IF EXISTS (SELECT NULL FROM [dbo].[Empresa] WHERE [EMP_RFC] IN (SELECT [EmpresaUser_Empresa] FROM [dbo].[EmpresaUser] WHERE [EmpresaUser_Email] = @Email))
        BEGIN
            INSERT INTO dbo.aspnet_UsersInRoles
                    ( UserId, RoleId )
            SELECT @USERID, -- UserId - uniqueidentifier
                      RoleId -- RoleId - uniqueidentifier
                  FROM dbo.aspnet_Roles WHERE RoleName IN ('Empresas')
        END

        SET @PASS = (SELECT TOP 1 [EmpresaUser_Password] FROM [dbo].[EmpresaUser] WHERE [EmpresaUser_Email] = @Email ORDER BY [EmpresaUser_Email])

        UPDATE [dbo].[EmpresaUser] SET [EmpresaUser_Password] = @PASS WHERE [EmpresaUser_Email] = @Email

        SET @MensajeRegreso = 'No creo que funcione ...'
    END

END
IF @Nuevo IN (0, 1)
BEGIN
    SELECT @MensajeRegreso AS Mensaje
END
END

每次插入电子邮件时,我都会收到此错误消息密码:

  

无法在具有唯一索引'aspnet_Users_Index'的对象'dbo.aspnet_Users'中插入重复的键行。重复键值为(f12020b5-6b40-488a-bc55-2bb4708986b2,)。违反PRIMARY KEY约束'PK__aspnet_M__1788CC4DA6E8A466'。无法在对象'dbo.aspnet_Membership'中插入重复键。重复键值为(556ba324-9720-4081-a4be-89fbb38902fc)。违反PRIMARY KEY约束'PK__aspnet_U__AF2760AD5013D67A'。无法在对象'dbo.aspnet_UsersInRoles'中插入重复键。重复键值是(556ba324-9720-4081-a4be-89fbb38902fc,084b9f2d-08d4-4e74-911e-ac5ae50dfaf9)。该语句已终止。该语句已终止。声明已经终止。

2 个答案:

答案 0 :(得分:0)

为select statment添加distinct

  SELECT DISTINCT  @USERID, -- UserId - uniqueidentifier
                   RoleId -- RoleId - uniqueidentifier
  FROM dbo.aspnet_Roles WHERE RoleName IN ('Empresas')

答案 1 :(得分:0)

您检查var mtl_loader = new THREE.MTLLoader(); mtl_loader.setBaseUrl(model_base_url); mtl_loader.setPath(model_base_url); mtl_loader.load(model_mtl_name, function(materials) { materials.preload(); var obj_loader = new THREE.OBJLoader(); obj_loader.setMaterials(materials); obj_loader.setPath(model_base_url); obj_loader.load(model_obj_name, function(object) { scene.add( object ); render(); }, onProgress, onError); }); 哪个好。但是在if @USERID IS NULL(即ELSE)部分,您尝试再次插入。这是你违反PK的行为。