Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable not getting set by select

I have an issue where a variable is not getting set by a select statement.

The select joins a table variable @contracts (which is used to control a loop) and then joins to the real contract and contract line tables.

my select is:

select top 1 
        @contract_id = c.contract_id 
        , @account = ch.account
        , @service = cl.service
        , @model = cl.model
        , @serial = cl.serial
        , @contract = ch.contract
    from 
        @contracts c
        inner join contractline cl on c.contract_id = cl.contract_id
        inner join contractheader ch on cl.contract = ch.contract
    where
        cl.contract_id = @contract_id

But when I do a select @contract_id say i get a NULL back, as do all my variables.

I have done a simple select * from @contracts c inner join contractline cl on c.contract_id = cl.contract_id inner join contractheader ch on cl.contract = ch.contract and this returns exactly 1 line with the values in all the correct places, no nulls in the selected values.

What have I done wrong?

like image 367
themaninthesuitcase Avatar asked Jul 16 '26 18:07

themaninthesuitcase


1 Answers

Looks like your WHERE clause could be stopping the row from returning. You mentioned you've tested the same SELECT without setting variable, but the code you listed doesn't include this WHERE - so its not the same.

Test your SELECT with the WHERE in place, with dummy values (likely NULL) and review. It simply sounds like its not returning a result.

like image 168
Adam Houldsworth Avatar answered Jul 19 '26 12:07

Adam Houldsworth