0
votes

I'am creating a stored procedure to insert data into a table if the parameter matched.

Here's my stored procedure

    CREATE PROCEDURE [dbo].[spAutoRegistPosition]
        @Position varchar(255),
        @UserID int
    AS
    IF NOT EXISTS (SELECT * FROM dbo.UserXUserGroup WHERE UserID = @int)
    BEGIN

        -- INSERT HERE
        IF @Position = 'Manager'
            INSERT INTO dbo.UserXUserGroup(UserID, UserGroupID)  
            VALUES (@UserID, 4)

        ELSE IF @Position = 'GM'
            INSERT INTO dbo.UserXUserGroup(UserID, UserGroupID) 
            VALUES (@UserID, 3)

        ELSE IF @Position = 'Direktur'
            INSERT INTO dbo.UserXUserGroup(UserID, UserGroupID)  
            VALUES (@UserID, 3)

    END
    GO

But when I run that I got this error message

Msg 137, Level 15, State 2, Procedure spAutoRegistPosition, Line 8 [Batch Start Line 9]
Must declare the scalar variable "@int"."

I already tried to declare the variable after BEGIN and didn't work.

1

1 Answers

1
votes

You don't have a variable @INT. Seems you want to use @UserID.

    CREATE PROCEDURE [dbo].[spAutoRegistPosition]
        @Position varchar(255),
        @UserID int
    AS
    IF NOT EXISTS (SELECT * FROM dbo.UserXUserGroup WHERE UserID = @UserID)
    BEGIN
        -- INSERT HERE
        IF @Position = 'Manager'
        INSERT INTO dbo.UserXUserGroup(UserID, UserGroupID)  
        VALUES (@UserID, 4)

        ELSE IF @Position = 'GM'
        INSERT INTO dbo.UserXUserGroup(UserID, UserGroupID) 
        VALUES (@UserID, 3)

        ELSE IF @Position = 'Direktur'
        INSERT INTO dbo.UserXUserGroup(UserID, UserGroupID)  
        VALUES (@UserID, 3)

    END
    GO