any (T) SQL heads in the house?

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,925
mkay, I'm working on a trigger for le school stuffs and I am literally going insane:

Code:
SELECT COUNT(*) FROM inserted <--- is OK value = 1

SELECT COUNT(*) FROM inserted <--- is OK value = 1
    INNER JOIN Vlucht v
    ON inserted.vluchtnummer = v.vluchtnummer 

SELECT COUNT(*) FROM inserted <--- is OK value = 1
    INNER JOIN Vlucht v
    ON inserted.vluchtnummer = v.vluchtnummer
    INNER JOIN IncheckenVoorBestemming ivb
    ON v.luchthavencode = ivb.luchthavencode 

SELECT COUNT(*) FROM inserted <--- is OK value = 1
    INNER JOIN Vlucht v
    ON inserted.vluchtnummer = v.vluchtnummer
        INNER JOIN IncheckenVoorBestemming ivb
    ON v.luchthavencode = ivb.luchthavencode
    INNER JOIN IncheckenBijMaatschappij ibm
    ON inserted.balienummer = ibm.balienummer 

IF (SELECT COUNT(*) FROM inserted  <--- is NOT OK value = 0
    INNER JOIN Vlucht v
    ON inserted.vluchtnummer = v.vluchtnummer
    INNER JOIN IncheckenVoorBestemming ivb
    ON v.luchthavencode = ivb.luchthavencode
    INNER JOIN IncheckenBijMaatschappij ibm
    ON inserted.balienummer = ibm.balienummer) = 0

<some error stuff here>

Ok I am freaking out a bit, because when I run an INSERT on the table this trigger lives in, then I get 4 1's printed and then the error handling. That means that the final IF() evaluated to zero, which is simply not true given the code above saying otherwise. I also tried with SELECT that goes with the IF() surrounded by ISNULL(<the select>,0) and that ofc is just desperation given that count will always return a number.

So...anyone know what the heck is going on? :(:(:(
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
What's the database software? You probably want an "IF EXISTS"
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
You could also do:

SELECT COUNT(*) INTO row_count inserts INNER ...
IF row_count = 0 ...
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,925
it's MS-SQL server. and mate I tried IF EXISTS, all that jazz. If I look for the stuff separately I find it, but as soon as I put it in the condition I get nothing. it's doing my head in. any help much appreciated.

also, be aware I put everything into the database that's needed for this trigger to go off, it's not some random thing.
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,925
solved it:

Code:
IF (SELECT COUNT(*) FROM inserted i
INNER JOIN Vlucht v ON v.vluchtnummer = i.vluchtnummer
INNER JOIN IncheckenBijMaatschappij ibm ON v.maatschappijcode = ibm.maatschappijcode and i.balienummer = ibm.balienummer
INNER JOIN Luchthaven l ON v.luchthavencode = l.luchthavencode
INNER JOIN IncheckenVoorBestemming ivb ON l.luchthavencode = ivb.luchthavencode and i.balienummer = ivb.balienummer
) <> (select count(*) from inserted)
 

Users who are viewing this thread

Top Bottom