views:

125

answers:

3

Could someone please explain to me why the following query is invalid? I'm running this query against an Oracle 10g database.

select count(test.*) from my_table test;

I get the following error: ORA-01747: invalid user.table.column, table.column, or column specification

however, the following two queries are valid.

select count(test.column) from my_table test;

select test.* from my_table test;
+1  A: 

Hi contactmatt,

COUNT(expression) will count all rows where expression is not null. COUNT(*) is an exception, it returns the number of rows: * is not an alias for my_table.*.

Vincent Malgrat
+1  A: 

This syntax only works in PostgreSQL and only because it has a record datatype (for which test.* is a meaningful expression).

Just use COUNT(*).

This query:

select count(test.column) from my_table test;

will return you the number of records for which test.column is not NULL.

This query:

select test.* from my_table test;

will just return you all records from my_table.

COUNT as such is probably the only aggregate that makes sense without parameters, and using an expression like COUNT(*) is just a way to call a function without providing any actual parameters to it.

Quassnoi
There's a major difference between COUNT(*) and COUNT(column): the former counts all rows; the latter counts the rows where the column value is NOT NULL. They are not interchangeable.
Jonathan Leffler
+2  A: 

As far as I know, Count(Table.*) is not officially supported in the SQL specification. Only Count(*) (count all rows returned) and Count(Table.ColumnName) (count all non-null values in the given column). So, even if the DBMS supported it, I would recommend against using it.`

Thomas
For oracle, the 'reason' is that count() requires an expression, whereas [table.]* is not an expression, but a part of syntax offered as part of a SELECT as an alternative to an expression.
Gary