So I am using an if statement for the first time in SQL Server. It is simple enough and it works fine. This is the statement.
IF EXISTS(SELECT Invoice_ID FROM #TEST WHERE Invoice_ID IS NULL)
SELECT Invoice_ID FROM #TEST WHERE Invoice_ID IS NULL
ELSE
SELECT 'No Values'
There is something about this that just doesn't seem efficient or logical. I understand that the if statement pulls all Invoice IDs that are Null and if it gets some it returns true so goes into the if block. It then actually selects all the invoice ID's that are NULL.
Now this could simply be because I have not done this before. But isn't there a way for me to use that data it pulled in the if statement to then use in the if block.
so something like (pseudocode)
IF EXISTS(select all invoice ids which have null value) do something with those invoice_ids
I mean I guess I could create a temporary table first then do an if statement on that if the count is greater than 0 then work with the table? But still maybe there is some nice sql command I don't know about?
It's just there is something wrong for me about pulling the data twice.
Aucun commentaire:
Enregistrer un commentaire