如何编写更新查询以设置存储过程中的角色和权限

时间:2014-12-05 11:30:20

标签: sql sql-server stored-procedures

我有一个存储过程,我必须更新Role表的值,然后根据Permission表的Permissionid,我必须更新第三个表Roledetail。但我尝试的是不工作,而不是更新表。这是我的存储过程。请帮忙.. 我有下表

作用

RoleId int,
RoleName varchar(25),
[Description] varchar(100),
Deleted bit,
CreatedOn datetime,
CreatedBy int,
LastUpdatedOn datetime,
LastUpdatedBy int

权限

PermissionId int,
PermissionName varchar(25)

RoleDetail

RoleDetailId int,
RoleId int,
PermissionId int
AddedOn datetime,
AddedBy int,
Deleted bit,
DeletedOn datetime,
DeletedBy int

查询

ALTER PROCEDURE usp_UpdateRole
@pRoleId int,
@pRoleName Varchar(25),
@pRoleDescription Varchar(100),
@pAttachedPermission varchar(50)
AS
BEGIN

    SET NOCOUNT ON;

   DECLARE @Permissions AS VARCHAR(50)
SET @Permissions = @pAttachedPermission
DECLARE @Query AS NVARCHAR (max)


UPDATE [Role]
SET

    [Role].[RoleName] = '@pRoleName',
    [Role].[Description] = '@pRoleDescription',
    [Role].[Deleted] = 0,
    [Role].[CreatedOn]= GetDate(),
    [Role].[CreatedBy] = 1,
     [Role].[LastUpdatedOn] = NULL,
    [Role].[LastUpdatedBy]=NULL 
    where [Role].RoleId = @pRoleId

DECLARE @RoleId AS INT
SET @RoleId = Scope_Identity()
SET @Query = 'SELECT '+
         CAST(@RoleId as Varchar(10))+' AS RoleId, 
        PermissionId AS PermissionId,
        GETDATE() AS AddedOn,
        1 As AddeBy,
        0 AS Deleted,
        NULL As DeletedOn,
        NULL AS DeletedBy 
    FROM
        [Permission]
    WHERE
        PermissionId in ('+@Permissions+')'
SET @Query = 'INSERT INTO [RoleDetail] '+ @Query
exec sp_ExecuteSQL @Query
END

2 个答案:

答案 0 :(得分:1)

您正在尝试将scope_identity与update语句一起使用。

您的RoleId将是您的范围中插入的最后一个ID,而不是更新记录的ID。 所以基本上你需要改变你获得角色ID的方式。

但总的来说,我会按如下方式重写你的过程

UPDATE [Role]
SET
....
    where [Role].RoleId = @pRoleId 

INSERT INTO [RoleDetail] (RoleId, PermissionId, AddedOn, AddedBy, Deleted )
SELECT  @pRoleId, PermissionId, getdate(), 1, 0
FROM  [Permission]
WHERE
PermissionId in (select id from fn_parseIntList(@Permissions))

其中parseIntList是将字符串列表转换为int的表数据的函数。有很多例子如何实现这个功能 例如 Converting String List into Int List in SQL

<强> UPD: 获取表结构后 - 您需要只插入2列,假设RoleDetailId是标识

INSERT INTO [RoleDetail] (RoleId, PermissionId)
    SELECT  @pRoleId, PermissionId
    FROM  [Permission]
    WHERE
    PermissionId in (select id from fn_parseIntList(@Permissions))

答案 1 :(得分:1)

@ fly_ua建议的一些更改 它的魅力所在。

ALTER PROCEDURE usp_UpdateRole
@pRoleId int,
@pRoleName Varchar(25),
@pRoleDescription Varchar(100),
@pAttachedPermission varchar(50)
AS
BEGIN

    SET NOCOUNT ON;

   DECLARE @Permissions AS VARCHAR(50)
SET @Permissions = @pAttachedPermission
DECLARE @Query AS NVARCHAR (max)


UPDATE [Role]
SET

   [Role].[RoleName] = @pRoleName,
    [Role].[Description] = @pRoleDescription,
    [Role].[Deleted] = 0,
    [Role].[CreatedOn]= GetDate(),
    [Role].[CreatedBy] = 1,
     [Role].[LastUpdatedOn] = NULL,
    [Role].[LastUpdatedBy]=NULL 
    where [Role].RoleId = @pRoleId

SET @Query = 'SELECT '+
         CAST(@pRoleId as Varchar(10))+' AS RoleId, 
        PermissionId AS PermissionId,
        GETDATE() AS AddedOn,
        1 As AddeBy,
        0 AS Deleted,
        NULL As DeletedOn,
        NULL AS DeletedBy 
    FROM
        [Permission]
    WHERE
        PermissionId in ('+@Permissions+')'
SET @Query = 'INSERT INTO [RoleDetail] '+ @Query
exec sp_ExecuteSQL @Query
END