0

The following query refuses to work on Sql server whereas it runs fine on Oracle 10gR2.

SELECT COUNT(*)
FROM (
   SELECT count(*)
   FROM MYTABLE
   WHERE id IS NOT NULL
   GROUP BY id
)

It ends up with this message :

Microsoft OLE DB Provider for SQL Server error '80040e14'

Ligne 7 : syntaxe incorrecte vers ')'. 

How can I make it compatible for both DBMS ?

4
  • Umm... TABLE is a reserved word in both DBMSs. Are you sure that exact query gives this error? Commented Apr 27, 2012 at 16:33
  • @MarkByers TABLE was just a place holder for the real name Commented Apr 27, 2012 at 16:36
  • Are there any other changes you made to the query before posting it? Can we see the original query? Commented Apr 27, 2012 at 16:37
  • @MarkByers the query performed is exactly like the one in the post. The table name changes only. Commented Apr 27, 2012 at 16:43

4 Answers 4

4

SQL Server requires an alias on derived tables, so...

SELECT COUNT(*) 
FROM ( 
   SELECT count(*) 
   FROM TABLE 
   WHERE id IS NOT NULL 
   GROUP BY id 
)  a

I'm not sure how to do the alias in Oracle. Also, you'll need to provide a column name for count(*) in the derived table.

Sign up to request clarification or add additional context in comments.

Comments

1

This should work in both:

SELECT COUNT(DISTINCT id)
FROM yourtable
WHERE id IS NOT NULL

1 Comment

If I multiple fields, let's say id,foo and bar fields, how can I do ?
1

If I understand what you are trying to accomplish, I think you could simplify this to

SELECT COUNT( DISTINCT id )
  FROM someTable
 WHERE id IS NOT NULL

which should work on both databases.

1 Comment

If I multiple fields, let's say id,foo and bar fields, how can I do ?
0

In Sql Server this should work:

SELECT count(id)
FROM TABLE
WHERE id IS NOT NULL
GROUP BY id

Not sure if it will work in Oracle though

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.