[英]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.