使用存储过程从用户定义的表类型将记录插入SQL Server数据库

时间:2017-11-17 19:10:41

标签: sql sql-server tsql stored-procedures

方案

我有这个数据表,当用户将角色分配给其他用户时我会从中获取:

enter image description here

在上表中,我为用户mushtaq分配了一个新角色,并通过以下过程将其插入到我的数据库中。

ALTER PROCEDURE [dbo].[SP_ROLE_ASSIGNMENT_ASSIGN_ROLES_TO_USER]
    @RoleAssignmentDT _ROLEASSIGNMENTSCHEMA ReadOnly
AS
BEGIN TRY
BEGIN TRANSACTION
    IF EXISTS (SELECT Role_ID 
               FROM Role_Assignment RA 
               INNER JOIN @RoleAssignmentDT t ON t._Role_ID = Ra.Role_ID 
                                              AND RA.Username = t._Username)
    BEGIN
        UPDATE Role_Assignment
        SET Role_Assign = 0, 
            Modified_By_ID = t._Modified_By_ID, 
            Modified_Date = GETDATE()
        FROM @RoleAssignmentDT t
        WHERE Role_Assignment.Role_ID <> t._Role_ID 
          AND Role_Assignment.Username = t._Username
          AND (t._Role_Assign = 1 AND Role_Assignment.Role_Assign = 1)

        UPDATE Role_Assignment
        SET Role_Assign = t._Role_Assign, 
            Modified_By_ID = t._Modified_By_ID, 
            Modified_Date = GETDATE()
        FROM @RoleAssignmentDT t
        WHERE Role_Assignment.Role_ID = t._Role_ID 
          AND Role_Assignment.Username = t._Username
    END
    ELSE
    BEGIN
        INSERT INTO Role_Assignment (Role_ID, Username, Role_Assign, Prepared_By_ID, Prepared_Date)
            SELECT
                t._Role_ID, t._Username, t._Role_Assign, t._Prepared_By_ID, GETDATE()  
            FROM 
                @RoleAssignmentDT t
    END

    COMMIT TRAN -- Transaction Success!
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRAN --RollBack in case of Error

-- you can Raise ERROR with RAISEERROR() Statement including the details of the exception
--RAISERROR('No Record Inserted, Contact to administrator',16,1)
    DECLARE @Msg NVARCHAR(MAX)  
    SELECT @Msg=ERROR_MESSAGE() 
    RAISERROR('Error Occured: %s', 20, 101,@msg) WITH LOG
END CATCH

下面是我的SQL Role_Assignment表的架构

enter image description here

现在,当我将新角色分配给上述其他用户时,只会更新用户ahmer。因为当我的程序获取数据表时,IF条件运行并找到用户ahmer并更新它。

我想要什么

我希望存储过程只检查和更新SQL Server表Role_Assignment中存在的那些用户;如果它们不存在,那么先插入它们。

我想我提供了有关我当前问题的简要信息。请帮助我在我的商店程序中需要做哪些修改,以便首先检查天气用户是否存在。那些之前不存在的人插入它们并更新其他存在的人。

1 个答案:

答案 0 :(得分:0)

在存储过程

中尝试以下代替t-sql代码
 MERGE Role_Assignment ra
    USING @RoleAssignmentDT t
    ON Role_Assignment.Username = t._Username
    WHEN MATCHED AND
       Role_Assignment.Role_ID <> t._Role_ID 
    and (t._Role_Assign = 1 and Ra.Role_Assign = 1) THEN
       UPDATE
        Set Role_Assign = 0, Modified_By_ID = t._Modified_By_ID, Modified_Date = GETDATE()
        From @RoleAssignmentDT
    WHEN MATCHED AND
    Role_Assignment.Role_ID = t._Role_ID  THEN
      UPDATE
        Set Role_Assign = t._Role_Assign, Modified_By_ID = t._Modified_By_ID, Modified_Date = GETDATE()
        From @RoleAssignmentDT
    WHEN NOT MATCHED BY TARGET THEN
      Insert (Role_ID,Username,Role_Assign,Prepared_By_ID,Prepared_Date)
        VALUES (t._Role_ID, t._Username, t._Role_Assign, t._Prepared_By_ID, GETDATE());