我需要编写一个这样的存储过程:我通过表中的值,编写这个存储过程,首先我需要检查数据Access和RoleId存在于数据库中; 如果没有,需要将其插入表中。
我写了这个:
ALTER PROCEDURE [dbo].[InsertAndUpdateAccessLevel]
(@AccessLevel AS AccessLevel READONLY)
AS
BEGIN
DECLARE @AC AccessLevel;
SET @AC = @AccessLevel;
IF NOT EXISTS (SELECT Id
FROM RoleAccess
WHERE RoleAccess.RoleId = @AC.RoleId
AND RoleAccess.Access = @AC.Access)
INSERT INTO RoleAccess (RoleId, Access, IsDelete)
SELECT * FROM @AccessLevel
END
得到这些问题: Msg 137, Level 16, State 1, Procedure InsertAndUpdateAccessLevel, Line 8 [Batch Start Line 7] Must declare the scalar variable "@AccessLevel".
Msg 137, Level 16, State 1, Procedure InsertAndUpdateAccessLevel, Line 8 [Batch Start Line 7] Must declare the scalar variable "@AC".
Msg 137, Level 16, State 1, Procedure InsertAndUpdateAccessLevel, Line 15 [Batch Start Line 7] Must declare the scalar variable "@AC".
Msg 137, Level 16, State 1, Procedure InsertAndUpdateAccessLevel, Line 16 [Batch Start Line 7] Must declare the scalar variable "@AC".
我该如何解决?
请尝试以下更改:
ALTER PROCEDURE [dbo].[InsertAndUpdateAccessLevel]
(
@AccessLevel AccessLevel READONLY
)
AS
BEGIN
IF NOT EXISTS
(
SELECT
Id
FROM RoleAccess
WHERE exists (select 1 from @AccessLevel as x where
RoleAccess.RoleId=x.RoleId
AND RoleAccess.Access=x.Access)
)
INSERT INTO RoleAccess (RoleId, Access, IsDelete )
SELECT * FROM @AccessLevel
END
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。