简体   繁体   English

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

[英]Insert records into SQL Server database from user defined table type using stored procedure

Scenario 情境

I have this Datatable which I'm getting from when user assign role to other user: 我有这个数据表,当用户将角色分配给其他用户时,我将从中获得该数据表:

在此处输入图片说明

In the above table, I assign a new role to user mushtaq and through below procedure I want to insert it in my database. 在上表中,我为用户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

Below is my SQL Role_Assignment table's schema 以下是我的SQL Role_Assignment表的架构

在此处输入图片说明

Now, when I assign new roles to other user above procedure only updates user ahmer . 现在,当我向上述其他用户分配新角色时,过程只会更新ahmer用户。 Because when my procedure gets the datatable my IF condition run and it finds user ahmer and updates it. 因为当我的过程获取数据表时,我的IF条件就会运行并找到用户ahmer并对其进行更新。

What I want 我想要的是

I want the stored procedure to check and update only those users who exist in the SQL Server table Role_Assignment ; 我希望存储过程仅检查和更新SQL Server表Role_Assignment存在的那些用户; if they don't exist, then insert them first. 如果它们不存在,请先插入它们。

I think I provide brief information about my current problem. 我想提供有关当前问题的简短信息。 Please help me what modification I need to do in my store procedure so it first check weather user exists or not. 请帮助我在存储过程中进行哪些修改,以便它首先检查天气用户是否存在。 Those who not exists before then insert them and update other those who exists. 之前不存在的对象然后将其插入并更新其他存在的对象。

Try the following in place of your t-sql code in the stored procedure 在存储过程中尝试以下方法代替您的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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将用户定义的表类型与 SQL Server 中的临时存储过程一起使用 - Using a User defined table type with a temporary stored procedure in SQL Server 在SQL Server存储过程中循环用户定义的表类型 - Looping user defined table type in SQL Server stored procedure 如何使用存储过程 SQL Server 从表中复制记录并插入到同一个表中 - How to copy records from table and insert into same table using stored procedure SQL Server 在存储过程中插入或更新用户定义的表类型 - Insert or Update user-defined table type in a stored procedure 执行存储过程以将记录插入表中-SQL Server 2000 - Executing a stored procedure to insert records into a table - SQL Server 2000 如何从存储过程插入表(用户定义类型)并传递给C# - How insert into table(User-defined type) from Stored procedure and passing to c# SQL Server 2008是否在CLR存储过程中接收用户定义的表类型? - SQL Server 2008 Receive an user-defined table type in a CLR Stored Procedure? 如何使用存储过程将datetime插入SQL Server数据库表中? - How to insert datetime into a SQL Server database table with a stored procedure? 在 SQL 存储过程中使用用户定义的表 - 无效数据类型 - using user defined tables in SQL Stored Procedure - invalid data type 将日期值从用户定义的表传递到SQL Server 2008存储过程 - Passing Date value from user defined table to a SQL Server 2008 stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM