Hi nabilabolo,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
SQL
DECLARE @first INT,@last INT
SET @first = 1
SET @last = 5
DECLARE @Counter INT
SET @Counter = @first
WHILE (@Counter <= @last)
BEGIN
IF EXISTS(SELECT CustomerId FROM Customers WHERE CustomerId = @Counter)
-- Update
UPDATE Customers SET Country = '' WHERE CustomerId = @Counter
ELSE
-- Insert
INSERT INTO Customers VALUES ('test','test')
SET @Counter = @Counter + 1
CONTINUE;
END
SELECT * FROM Customers
Output
CustomerId |
Name |
Country |
1 |
John Hammond |
|
2 |
Mudassar Khan |
|
3 |
Suzanne Mathews |
|
4 |
Robert Schidner |
|
5 |
test |
test |