tengiz,
tengiz wrote:vc, it's different. You're referring to a subquery as a table expression, which is perfectly valid. What I meant is the following:
select pk, a, b, (select c from tab2 where tab2.pk = tab1.pk) as c from tab1
Provided that the subquery returns one column rowset with no more that one row, this is valid for SQL Server; however, is not standard complaint. .........
I beg to differ (not being obnoxious, just willing to clarify ;) ):
From the '92 standard:
"Syntax Rules
1) The degree of a <scalar subquery> shall be 1
..................
"
and
"General Rules
1) If the cardinality of a <scalar subquery> or a <row subquery> is
greater than 1, then an exception condition is raised: cardinal-
ity violation.
"
and elsewhere in the standard:
"X3H2-92-154/DBL CBR-002
6.11 <value expression>
General Rules
.............................
2) If a <value expression primary> is a <scalar subquery> and the
result of the <subquery> is empty, then the result of the <value
expression primary> is the null value.
.........................
Leveling Rules
1) The following restrictions apply for Intermediate SQL:
None.
2) The following restrictions apply for Entry SQL in addition to
any Intermediate SQL restrictions:
........................................
e) A <value expression primary> shall not be a <scalar subquery>
except when the <value expression primary> is simply con-
tained in a <value expression> that is simply contained in
the second <row value constructor> of a <comparison predi-
cate>.
"
To sum up, a scalar subquery can be used anywhere a <value expression> is used, in particular in the select list.
Therefore, if the subquery satisfies the above rules, your query (
select pk, a, b, (select c from tab2 where tab2.pk = tab1.pk) as c from tab1 )
is ANSI compatible ('92 intermediate) because (select c from tab2 where tab2.pk = tab1.pk) is a <value expression>.
Rgds.