I need to write a stored procedure for this: I pass the table value parameters to this stored procedure, first I need to check data Access
and RoleId
exist in the database; if not, I insert it into table.
I wrote this :
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
but I get these errors:
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".
What's the problem? How can I solve it?
Edit
CREATE TYPE [dbo].[AccessLevel] AS TABLE
(
[RoleId] [INT] NULL,
[Access] [NVARCHAR](MAX) NULL,
[IsDelete] [BIT] NULL
)
SET
it's value, you perform DDL operations (INSERT
,UPDATE
,DELETE
, etc) statement against it.SET @AC=@AccessLevel;
makes no sense; how do youSET
a table to be another table? It would be like running the statementSET dbo.MyTable = dbo.YourTable;
– Larnu