Jump to content
dormky

How do I make a "WHERE id IN (param)" with MyDAC ?

Recommended Posts

I'd like to run this query :

 

SELECT * FROM users WHERE id IN (:param)

 

To get a list of the users I need. But I can't figure out how to pass an array of values to a TMyQuery object. How is this supposed to be done ?

 

Thanks !

Share this post


Link to post

MyDac can use macro https://docs.devart.com/mydac/work_macros.htm

Something like this code (sorry, I don't use Mydac but Firedac)

Query1.SQL.Text := 'SELECT * FROM user Where ID IN (&list)';
Query1.MacroByName('list').asString := '1,2,3';
Query1.Open;

 

Otherwise, you can use a format string

sqlstring:='SELECT * FROM users WHERE id IN (%s)';
inlist := '1,2,3'
query.sql.text:=Format(sqlstring,[inlist]);

Ok, it's really a very poor code !

  • Like 1

Share this post


Link to post

I'm not sure these answers will handle SQL sanitization properly, is there data on that ?

And in any case who builds an sql accessor without giving array types lol

Share this post


Link to post
2 hours ago, dormky said:

I'm not sure these answers will handle SQL sanitization properly, is there data on that ?

I had MyDac since 2009 and it is documented and shown in the demos, same goes for UniDac.

 

q.SQL.Add('SELECT * FROM users WHERE id IN :param');
.....
    q.ParamByName('param').AsString := 'Iam doing here what ever i want, including a useless try to inject SQL with select * from user or even delete * from users; that will not be ran as SQL !!!';

As shown in my screenshot from that link.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×