Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax near 'end' [closed]

For the following stored procedure, I get an error

Incorrect syntax near 'end'

To the best of my knowledge I have the right begin and end tags. I'm not sure Where the error is. I have checked the previous questions as well but couldn't rectify the error. Thank you for any help!

USE CONTACT
GO

SET ANSI_NULLS ON
GO  
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[Pref] 
    (@userName VARCHAR(50),
     @ComputerName VARCHAR(50),
     @PrinterDescription VARCHAR(255),
     @PrinterLocation VARCHAR(255),
     @Print_DuplexYN TINYINT,
     @DateRecChanged datetime
    )
AS
BEGIN
    DECLARE @userID INT;

    SELECT @userID = User_ID
    FROM tblUser
    WHERE Login_ID = @userName

    DECLARE @MyCount INT

    SELECT @MyCount = COUNT(UserName) 
    FROM PrinterPrefs 
    WHERE UserName = @UserName AND ComputerName = @ComputerName

    IF @MyCount = 0 
    BEGIN
        INSERT INTO PrinterPrefs (UserName, ComputerName, PrinterDescription, PrinterLocation, Print_DuplexYN, DateRecChanged)
        VALUES (@UserName, @ComputerName, @PrinterDescription, @PrinterLocation, @Print_DuplexYN, getdate(), @UserName)
    END
    ELSE
    BEGIN
        UPDATE PrinterPrefs
        SET PrinterDescription = @PrinterDescription,
            PrinterLocation = @PrinterLocation,
            Print_DuplexYN = @Print_DuplexYN,
            DateRecChanged = getdate(),
            UserName = @UserName
        WHERE
            UserName = @UserName AND
            ComputerName = @ComputerName
    END
GO
like image 927
ERR Avatar asked Sep 02 '26 02:09

ERR


1 Answers

You need another end before go.

USE CONTACT
    GO
    SET ANSI_NULLS ON
    GO  
    SET QUOTED_IDENTIFIER ON
    GO
CREATE PROCEDURE [dbo].[Pref] (
    @userName VARCHAR(50)
    ,@ComputerName VARCHAR(50)
    ,@PrinterDescription VARCHAR(255)
    ,@PrinterLocation VARCHAR(255)
    ,@Print_DuplexYN TINYINT
    ,@DateRecChanged datetime
    )
AS
BEGIN
    DECLARE @userID INT;
    SELECT @userID = User_ID
    FROM tblUser
    WHERE Login_ID = @userName;

    declare @MyCount int;
    select @MyCount = count(UserName) 
    from PrinterPrefs 
    where UserName = @UserName 
      and ComputerName = @ComputerName;

    if @MyCount = 0 
    begin
        INSERT into PrinterPrefs
        (UserName,
         ComputerName,
         PrinterDescription,
         PrinterLocation,
         Print_DuplexYN,
         DateRecChanged)
        VALUES
        (@UserName,
         @ComputerName,
         @PrinterDescription,
         @PrinterLocation,
         @Print_DuplexYN,
         getdate(),
         @UserName)
    end
    else
    begin
        UPDATE PrinterPrefs
        SET
            PrinterDescription = @PrinterDescription,
            PrinterLocation = @PrinterLocation,
            Print_DuplexYN =@Print_DuplexYN,
            DateRecChanged = getdate(),
            UserName = @UserName
        WHERE
            UserName = @UserName and
            ComputerName = @ComputerName;
    end
end --<-- add this
GO
like image 200
SqlZim Avatar answered Sep 03 '26 19:09

SqlZim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!