So, the situation is this: I need to check if there is a desired value ("abcd") in the table.
If there is no value, you need to receive a message (using SELECT), for example, this: "value abcd is missing".
How can this be done using SQL?
So, the situation is this: I need to check if there is a desired value ("abcd") in the table.
If there is no value, you need to receive a message (using SELECT), for example, this: "value abcd is missing".
How can this be done using SQL?
Two 100% working options:
True implementation proposed by a colleague with my comments:
SELECT CASE WHEN EXISTS (SELECT TOP (1) 1 FROM [Table] WHERE [field] = 'abcd') THEN 'EXISTS' ELSE 'value abcd is missing' END The second option:
IF (SELECT COUNT(1) FROM [Table] where [field]='abcd')>0 BEGIN SELECT 'EXISTS' END ELSE BEGIN SELECT 'value abcd is missing' END IF Through EXISTS :
IF EXISTS (SELECT TOP (1) 1 FROM [Table] where field='abcd') BEGIN SELECT 'EXISTS' END ELSE BEGIN SELECT 'value abcd is missing' END This, for example, should work in MS SQL:
SELECT CASE WHEN EXISTS(SELECT TOP 1 1 FROM Table where field='abcd')=1 THEN 'EXISTS' ELSE 'value abcd is missing' END In theory, in many other DBMS this should work.
You can still do the same through the IF ELSE.
Source: https://ru.stackoverflow.com/questions/637459/
All Articles