Friday, February 24, 2012

EXISTS (SELECT ... FROM) optional?

Alex
It is enough to use an asterisk rather including column as
IF EXISTS (SELECT * FROM Table WHERE blabalab)
Actually SQL Server checks for non empty set and internally will return
TRUE or NOT TRUE and stop continue checking subquery.
"Axel Dahmen" <NO_SPAM@.NoOneKnows.invalid> wrote in message
news:uKzl7fWAGHA.4004@.TK2MSFTNGP15.phx.gbl...
> Hi,
> in SQL there is a term that's bothering me:
> EXISTS (SELECT ... FROM
> I guess generations of SQL programmers put some brains into thinking of
> some
> expression to put after the "SELECT" term. I usually use a NULL, like
> EXISTS (SELECT NULL FROM
> I tend to believe this term is as redundant as DELETE * FROM. Wouldn't it
> be
> good to have this part optional, like with the DELETE statement? Like:
> EXISTS ([SELECT <column>[,<column>] FROM ] <tablename> ...)
> so it would be a valid term to write:
> EXISTS (MyTable WHERE MyIdColumn = 1234)
> RFC,
> Axel Dahmen
>I'm not sure if SQL Server actually allocates memory to hold the result set
of the sub-query. If that were true, using the asterisk would be rather
non-scalable I believe.
Regards,
Axel
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:esuwJtWAGHA.2036@.TK2MSFTNGP14.phx.gbl...
> Alex
> It is enough to use an asterisk rather including column as
> IF EXISTS (SELECT * FROM Table WHERE blabalab)
> Actually SQL Server checks for non empty set and internally will return
> TRUE or NOT TRUE and stop continue checking subquery.
>
>
> "Axel Dahmen" <NO_SPAM@.NoOneKnows.invalid> wrote in message
> news:uKzl7fWAGHA.4004@.TK2MSFTNGP15.phx.gbl...
it
>|||> I'm not sure if SQL Server actually allocates memory to hold the result set
> of the sub-query. If that were true, using the asterisk would be rather
> non-scalable I believe.
At execution time, there is zero difference between SELECT *, SELECT 1, or a
nything else. SQL Server
know what an existence check is and will not allocate memory to hold any res
ult from the subquery
inside the EXISTS.
During compile time, SELECT * will have a wee bit higher cost compared to SE
LECT 1. I doubt we can
measure any difference, even with a *very* wide table.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Axel Dahmen" <NO_SPAM@.NoOneKnows.invalid> wrote in message
news:OuPH0%23YAGHA.3928@.tk2msftngp13.phx.gbl...
> I'm not sure if SQL Server actually allocates memory to hold the result se
t
> of the sub-query. If that were true, using the asterisk would be rather
> non-scalable I believe.
> Regards,
> Axel
>
> --
> "Uri Dimant" <urid@.iscar.co.il> wrote in message
> news:esuwJtWAGHA.2036@.TK2MSFTNGP14.phx.gbl...
> it
>

No comments:

Post a Comment