Performing an Update by Checking for Data in SQL Server

Hello everyone,

In this article, I will provide information about performing the update process by checking if there is data in SQL Server.

In SQL Server, in some cases, it is possible to check whether the data exists and to perform the update process.

You can easily do this with the help of the code below.

--Create TEMP Table

CREATE TABLE #MyTempTable
(
    ID INT PRIMARY KEY IDENTITY(1, 1),
    Names NVARCHAR(50)
);

--Adding Data to TEMP Table

INSERT INTO #MyTempTable
(
    Names
)
VALUES
(N'Yavuz Selim'),
(N'Hasan Ali'),
(N'Bilge Nuray');

--Checking TEMP Table

SELECT *
FROM #MyTempTable;

--Data update process by checking data in the table


IF EXISTS (SELECT * FROM #MyTempTable WHERE Names = 'Yavuz Selim')
BEGIN
    UPDATE #MyTempTable
    SET Names = 'Yavuz Selim Kart'
    WHERE Names = 'Yavuz Selim';
END;
ELSE
BEGIN
    PRINT 'There is no such record';
END;

When you run the code, you will get a result like the one below.

Performing an Update by Checking for Data in SQL Server

As you can see, the update process has been done by checking the data.

Good luck to everyone in business and life.

196 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!