How do I use TADOQuery.Parameters with integer parameter types that have to be put in two or more places in a query?

StackOverflow https://stackoverflow.com/questions/7613832

  •  05-02-2021
  •  | 
  •  

Pergunta

I have a complex query that contains more than one place where the same primary key value must be substituted. It looks like this:

select  Foo.Id,
        Foo.BearBaitId,
        Foo.LinkType,
        Foo.BugId,
        Foo.GooNum,
        Foo.WorkOrderId,
        (case when Goo.ZenID is null or Goo.ZenID=0 then
            IsNull(dbo.EmptyToNull(Bar.FanName),dbo.EmptyToNull(Bar.BazName))+' '+Bar.Strength else
            '@'+BarZen.Description end) as Description,
        Foo.Init,
        Foo.DateCreated,
        Foo.DateChanged,
        Bug.LastName,
        Bug.FirstName,
        Goo.BarID,
        (case when Goo.ZenID is null or Goo.ZenID=0 then
             IsNull(dbo.EmptyToNull(Bar.BazName),dbo.EmptyToNull(Bar.FanName))+' '+Bar.Strength else
             '@'+BarZen.Description end) as BazName,
        GooTracking.Status as GooTrackingStatus
  from
    Foo
  inner join Bug on (Foo.BugId=Bug.Id)
  inner join Goo on (Foo.GooNum=Goo.GooNum)
  left join Bar on (Bar.Id=Goo.BarID)
  left join BarZen on (Goo.ZenID=BarZen.ID)
  inner join  GooTracking on(Goo.GooNum=GooTracking.GooNum )
 where (BearBaitId = :aBaitid) 
UNION
 select Foo.Id,
        Foo.BearBaitId,
        Foo.LinkType,
        Foo.BugId,
        Foo.GooNum,
        Foo.WorkOrderId,
        Foo.Description,
        Foo.Init,
        Foo.DateCreated,
        Foo.DateChanged,
        Bug.LastName,
        Bug.FirstName,
        0,
        NULL,
        0
 from Foo
 inner join Bug on (Foo.BugId=Bug.Id)
  where (LinkType=0)  and (BearBaitId= :aBaitid ) 
order by BearBaitId,LinkType desc, GooNum

When I try to use an integer parameter on this non-trivial query, it seems impossible to me. I get this error:

Error

Incorrect syntax near ':'.

The query works fine if I take out the :aBaitid and substitute a literal 1.

Is there something else I can do to this query above? When I test with simple tests like this:

select * from foo where id = :anid

These simple cases work fine. The component is TADOQuery, and it works fine until you add any :parameters to the SQL string.

Update: when I use the following code at runtime, the parameter substitutions are actually done (some glitch in the ADO components is worked around) and a different error surfaces:

adoFooContentQuery.Parameters.FindParam('aBaitId').Value := 1;
adoFooContentQuery.Active := true;

Now the error changes to:

Incorrect syntax near the keyword 'inner''.

Note again, that this error goes away if I simply stop using the parameter substitution feature.

Update2: The accepted answer suggests I have to find two different copies of the parameter with the same name, which bothered me so I reworked the query like this:

 DECLARE @aVar int;
 SET @aVar = :aBaitid;
 SELECT ....(long query here)

Then I used @aVar throughout the script where needed, to avoid the repeated use of :aBaitId. (If the number of times the parameter value is used changes, I don't want to have to find all parameters matching a name, and replace them).

I suppose a helper-function like this would be fine too: SetAllParamsNamed(aQuery:TAdoQuery; aName:String;aValue:Variant)

Foi útil?

Solução

FindParam only finds one parameter, while you have two with the same name. Delphi dataset adds each parameter as a separate one to its collection of parameters.

It should work if you loop through all parameters, check if the name matches, and set the value of each one that matches, although I normally choose to give each same parameter a follow-up number to distingish between them.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top