Trouble translating a SQL Server query to Firebird
-
03-10-2019 - |
Pergunta
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?
Solução
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.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow