please helppmee

Esra Köse 1 Reputation point
2020-11-12T21:28:26.947+00:00

CREATE PROC ContactCreateOrUpdate
@ContactID int,
@DeezNutz varchar(50),
@TVT varchar(50),
@Address varchar(50),
@EmailID varchar(50),
AS
BEGIN
IF(@ContactID=0)
INSERT INTO Contact(Name,Mobile,Address,[Email ID])
VALUES(@DeezNutz ,@TVT ,@Address,@EmailID)

END
ELSE
BEGIN
UPDATE Contact
SET
Name = @DeezNutz ,
Mobile = @TVT ,
Address = @Address
EmailID = @EmailID

	  WHERE ContactID= @ContactID  

END
END

Msg 156, Level 15, State 1, Procedure ContactCreateOrUpdate, Line 8
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Procedure ContactCreateOrUpdate, Line 15
Incorrect syntax near the keyword 'ELSE'.
Msg 102, Level 15, State 1, Procedure ContactCreateOrUpdate, Line 22
Incorrect syntax near 'EmailID'.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,688 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 25,851 Reputation points
    2020-11-12T22:13:37.53+00:00

    Please try the following.

    SQL

    CREATE PROC ContactCreateOrUpdate
        @ContactID INT,
        @Name VARCHAR(50),
        @Mobile VARCHAR(50),
        @Address VARCHAR(50),
        @EmailID VARCHAR(50)
    AS
    BEGIN
        IF (@ContactID = 0)
        BEGIN
            INSERT INTO Contact
            (
                Name,
                Mobile,
                Address,
                [Email ID]
            )
            VALUES
            (@Name, @Mobile, @Address, @EmailID);
        END;
        ELSE
        BEGIN
            UPDATE Contact
            SET Name = @Name,
                Mobile = @Mobile,
                Address = @Address,
                EmailID = @EmailID
            WHERE ContactID = @ContactID;
        END;
    END;
    
    1 person found this answer helpful.
    0 comments No comments

  2. Guoxiong 8,206 Reputation points
    2020-11-12T22:34:39.84+00:00

    There are two issues in your stored procedure. The first one is that in the last parameter @EmailID varchar(50) you do not need to put a comma (,). The second issue is that there are two BEGIN statements and three END statements. You can add the BEGIN statement in the IF statement:

    IF (@ContactID=0) 
    BEGIN
        INSERT INTO Contact(Name,Mobile,Address,[Email ID])
        VALUES(@Name,@Mobile,@Address,@EmailID)
    END
    

    or remove the END statement in the IF statement:

    IF (@ContactID=0) 
            INSERT INTO Contact(Name,Mobile,Address,[Email ID])
            VALUES(@Name,@Mobile,@Address,@EmailID)
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.