Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Exists return type

What does the SQL Server Exists function return as a type?

Does Exists have a conceptual return type?

SELECT * 
FROM tableName 
WHERE EXISTS (SELECT * 
              FROM tableName 
              WHERE columnName LIKE 'theValue%') = 1

Why does this not work?

like image 344
AmirNorouzpour Avatar asked May 12 '18 13:05

AmirNorouzpour


People also ask

What is the return data type of exists in SQL?

The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.

How do you check if an entry exists in SQL?

To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.

How do you use exists instead of in in SQL Server?

“IN” clause is preferred when there is a small list of static values or the inner query returns a very less number of rows. “EXISTS” clause is preferred when there is a need to check the existence of values in another table or when there is a need to check against more than one column.

When exists return false in SQL?

If EXISTS (subquery) returns at least 1 row, the result is TRUE. If EXISTS (subquery) returns no rows, the result is FALSE. If NOT EXISTS (subquery) returns at least 1 row, the result is FALSE.


1 Answers

As documented in BOL

Result Types

Boolean

It has a return type of boolean but this is just an internal datatype currently you can't declare columns or variables of that datatype.

Or use it in comparisons - even against another boolean.

where exists(select 1) = exists(select 1)

Also fails.

SQL Server doesn't implement the SQL Standard boolean datatype yet - the closest equivalent is bit but this is not a true boolean.

like image 112
Martin Smith Avatar answered Oct 02 '22 13:10

Martin Smith