I have a situation where I want to delete a member from Members
table if and only of the member has no transaction in MEMBERS_TRANSACTION
table.
I also want to know whether the member has any transaction. Currently I will return a column with the value of 1
to indicate at least one transaction exists for that member, or 0
to indicate that the member has no transaction and has been deleted upon the execution of this query. My query is as follows:
IF EXISTS
(
SELECT members_trans_id
FROM MEMBERS_TRANSACTION
WHERE member_link_id = 188
)
SELECT 1
ELSE
DELETE FROM Members WHERE member_link_id = 188;
SELECT 0
My concern is the two lines in ELSE
block. I can execute this query with no problem at SQL Server Management Studio, but I wonder if it is correct to write it that way.
I'm using SQL server 2008 R2.
Thank you.