views:

28

answers:

1

I'm new to Firebird, and I'm having particular difficulty translating this T-SQL to Firebird SQL. This code is stored outside of the database, not in a stored procedure.

DECLARE @NumTotal int
DECLARE @NumUsed int

SELECT @NumTotal = COUNT(*)
    FROM "some_Table"
    WHERE "CreatedOn"=@CreatedOn

SELECT @NumUsed = COUNT(*)
    FROM "some_Table"
    WHERE "CreatedOn"=@CreatedOn AND "UserID" IS NOT NULL

SELECT @NumUsed AS "NumUsed", @NumTotal AS "NumTotal"

I guess from the errors and my experimentation that I'm basically forced to put this into a stored procedure somehow. Is there a way I can do this while still keeping the code out of the database?

+1  A: 

Your code can be simplified to a single query:

SELECT COUNT(*) AS numTotal,
       (SELECT COUNT(*)
          FROM YOUR_TABLE
         WHERE userid IS NOT NULL
           AND createdon = @createdon) AS numUsed
  FROM YOUR_TABLE
 WHERE createdon = @createdon

Using double quotes is ANSI for escaping unusual characters, none of which I see in the example.

OMG Ponies
Missing the `WHERE CreatedOn = @CreatedOn` on your outer query?
Joe Stefanelli
@Joe Stefanelli: Fixed, thx.
OMG Ponies
Firebird uppercases table and column names if you don't quote them.
pATCheS