I use Microsoft SQL Server 2012. I need to check if table named SiteObjects in database has at least one record where column named SiteRegionId's value is equal to 22.
If it is it has to imlpement some logic if not it have to implement another logic.
Here my implementation:
IF EXISTS (SELECT * FROM SiteObjects WHERE SiteRegionId = 22)
BEGIN
----do what you need if exists
RETURN '0'; --not deleted
END
ELSE
BEGIN
--do what needs to be done if not
DELETE FROM SiteRegions
WHERE Id = 22;
RETURN '1';
END
I think that this row:
SELECT * FROM SiteObjects WHERE SiteRegionId = 22
is not effective because as I understand it runs on all table rows and selects all rows where SiteRegionId is equal to 22. Is there any way to make it more effective? And to check if any of the row meets conditions if it is, proceed.