1

I m trying to pass multiple parameters to sql query but it is throwing me with error,

here is the query

cbd_id=[1,10,19,31,37,42,48,57,63,64,65]


cursor.execute('''select t1.id as pid,pxval,pyval from <tbl1> t1 left join (select * from <tbl2> where clubid=? t2 on t1.id=t2.projectid where t1.cityid in (SELECT cityid FROM <tbl3> WHERE cbdid =? group by cityid) and t1.pxval>0 and t2.distance is null order by projectid)''',(cbd_id[i],cbd_id[i]))

And it give me error

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 't2'. (102) (SQLExecDirectW)")

But I checked couldn't identify the problem,

Any suggestion on this will be helpful.

Thanks

1
  • 1
    You're likely missing a right parenthesis somewhere. One example is the one missing right before the t2 alias. Should be select t1.id as pid,pxval,pyval from <tbl1> t1 left join (select * from <tbl2> where clubid=?) t2. Commented Nov 7, 2017 at 13:25

1 Answer 1

1

As the error message suggests, your SQL command text is malformed. Reformatted for clarity, your query is

select t1.id as pid,pxval,pyval 
from 
    <tbl1> t1 
    left join 
    (
        select * 
        from <tbl2> 
        where clubid=? 
    t2 
        on t1.id=t2.projectid 
where 
    t1.cityid in (
            SELECT cityid 
            FROM <tbl3> 
            WHERE cbdid =? 
            group by cityid) 
    and 
    t1.pxval>0 
    and 
    t2.distance is null 
order by projectid)

Notice that the <tbl2> subquery does not have its closing parenthesis. Your query probably should be

select t1.id as pid,pxval,pyval 
from 
    <tbl1> t1 
    left join 
    (
        select * 
        from <tbl2> 
        where clubid=? 
    ) t2 
        on t1.id=t2.projectid 
where 
    t1.cityid in (
            SELECT cityid 
            FROM <tbl3> 
            WHERE cbdid =? 
            group by cityid) 
    and 
    t1.pxval>0 
    and 
    t2.distance is null 
order by projectid
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.