Click here to Skip to main content
15,885,895 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table called CustomerAnalysis which has one record with ID 1, My stored proc does not go into the if since the record exist. What am I doing wrong

CusomerAnalysis:
| ID| 
|:--|
| 1 | 


What I have tried:

CREATE PROC proc_Insert
(
    @CustomerID INT = 1
)
AS
BEGIN
    IF EXISTS(SELECT ID FROM Client WHERE Client.ID = @CustomerID)

BEGIN
    UPDATE Client
    SET Client.ID = 2
    WHERE Client.ID = @CustomerID
END

ELSE
BEGIN
     INSERT INTO Client(ID) VALUES(@CustomerID)
END
END
Posted
Updated 16-Apr-21 1:06am
v4

1 solution

You have a table with a single column called ID.

That table contains a single record with ID = 1.

You are looking for a record in that table where the non-existent column CustomerID is equal to 1.

The only correct result is an error telling you that the column CustomerID does not exist.
 
Share this answer
 
Comments
Zainalds 16-Apr-21 7:05am    
CREATE PROC proc_Insert
(
@CustomerID INT = 1
)
AS
BEGIN
IF EXISTS(SELECT ID FROM Client WHERE Client.ID = @CustomerID)

BEGIN
UPDATE Client
SET Client.ID = 2
WHERE Client.ID = @CustomerID
END

ELSE
BEGIN
INSERT INTO Client(ID) VALUES(@CustomerID)
END
END
Richard Deeming 16-Apr-21 7:07am    
That's the same code you posted in your question. You are still referencing a column which does not exist according to your question.
Zainalds 16-Apr-21 7:09am    
I have updated the question, thank you.
Richard Deeming 16-Apr-21 7:16am    
You need to debug your code. Remember, the record won't exist after you've run the procedure once - the ID will now be 2 instead of 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900