Database link not working for count(1) but works for count(*) [message #321254] |
Mon, 19 May 2008 11:57  |
OraKaran
Messages: 183 Registered: March 2008 Location: United Kingdom
|
Senior Member |
|
|
Hi,
I am using oracle 10.2.0.4.0 on RHEL
I have 2 databases : db1 and db2
I have user1 on both the databases (with dba privileges)
I have created database link from db1 to db2
create public database link conn_db2.myco.com connect to user1 identified by user1_pwd using 'db2';
I can execute
Select count(*) from process_log@conn_db2.myco.com
But while I execute
Select count(1) from process_log@conn_db2.myco.com;
I get an error
"ora-01008: not all variables are bound
ora-02063: preceding line from conn_db2"
What could be the reason?
Thanks and Regards,
OraKaran
|
|
|
|
|
|
|
|
|
|
|
Re: Database link not working for count(1) but works for count(*) [message #321537 is a reply to message #321254] |
Tue, 20 May 2008 11:59   |
drewsmith70
Messages: 22 Registered: April 2008 Location: New Hampshire
|
Junior Member |
|
|
I just tried count(1) over a db link on 10.2, and it seems to work OK. Why is it a bug? I agree that you should always use count(*) or count(indexed_column), but I think count(1) is internally converted to count(*) anyway (it just means "count rows with non-null value 1" - doesn't mean anything to me).
But as of Oracle 11, here's something that should convince you to use count(*) vs. count(1):
asmith@DEV> create table t1 (c1 number);
Table created.
asmith@DEV> create view v1 as
2 select count(1) count from t1;
View created.
asmith@DEV> create view v2 as
2 select count(*) count from t1;
View created.
asmith@DEV> select object_name, object_type, status
2 from user_objects
3 where (object_name in ('V1','V2'))
4 and object_type = 'VIEW';
OBJECT_NAME
----------------------------
OBJECT_TYPE STATUS
------------------- -------
V1
VIEW VALID
V2
VIEW VALID
asmith@DEV> alter table t1 add (c2 number);
Table altered.
asmith@DEV> select object_name, object_type, status
2 from user_objects
3 where (object_name in ('V1','V2'))
4 and object_type = 'VIEW';
OBJECT_NAME
----------------------------
OBJECT_TYPE STATUS
------------------- -------
V1
VIEW INVALID
V2
VIEW VALID
A view using count(*) on a table will remain valid when columns are added to the table.
See:
http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/dependencies.htm#insertedID4
|
|
|
|
Re: Database link not working for count(1) but works for count(*) [message #321562 is a reply to message #321540] |
Tue, 20 May 2008 13:50   |
andrew again
Messages: 2577 Registered: March 2000
|
Senior Member |
|
|
I'd never consider count(1) to be any kind of "bug" unless Oracle says it is. AskTom doesn't seem to think so:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1156159920245
I'd never reject someone's code during a review beacuse they used count(1). From my perspective it's a style issue. Personally I always use count(*).
Regarding the view becoming invalid above: In general, invalid views will always automatically recompile when you access them, so if you can select from the invalid view without getting an error, I wouldn't call it an error just because it behaves differently to another example. Interesting though - I haven't seen that test case before.
Most of the metalink hits regarding ora-01008 I saw are related to cursor sharing. I found nothing related specifically to count(1). The cursor sharing would tie in with Michel's comment about internally re-writing as count(*).
|
|
|
Re: Database link not working for count(1) but works for count(*) [message #321673 is a reply to message #321562] |
Wed, 21 May 2008 00:34   |
Dipali Vithalani
Messages: 278 Registered: March 2007 Location: India
|
Senior Member |
|
|
Even i personally prefer to user count(*), but i cannot get why the count(1) is considered as bug by Michel, though it is converted by oracle internally to count(*)? His openion is important for me..
Interesting testcase by Mr. Smith.
Any other such testcase, anyone came to face where count(*) and count(1) behave differenty?
This is important to know for us because may of the developer used to use count(1) instade of count(*).
|
|
|
|
|