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.
As you can see, the update process has been done by checking the data.
Good luck to everyone in business and life.