Help with using a variable as IN clause in SQL

I was able to execute a SQLquery (in_arg with type string) whenever I pass in the IDs directly into the in_arg as below

"Select * from abc
where  abc.colname in (12,12,344)

in_arg declaration.

For business reasons, I do need to be able to pass the values of ‘abc.colname’ as a variable, so the query then looks like this below:

“Select * from abc
where abc.colname in +stringAcctIDs”

stringAcctIDs was declared as below:

When I passed in the variable into the SQL query as shown below

I got the error below:
sqlerror

I have declared the variable stringAcctIDs as String, an array of String , but it has not worked.
How can I make the SQL work with using the stringAcctIDs ?

The query should be like:

"Select * from abc where abc.colname in "+stringAcctIDs+ "

The plus should be out of double quotes. Your variable is being passed as a normal string strArray and not like a variable.

You can also do it like
Create a query variable of string type:
assign the above expression first to that query variable and then pass it on in your activity.

Thanks

Happy Automation!

@adiijaiin
Thanks but that did not work. Actually my IN clause is within other qualifications like below

"
select * …
WHERE t1.c = ‘a’ AND t2.c2 = 44522223 AND table.col IN +stringAcctIDs and t4.c4 = 1
"

hi @Yomi_Oluwadara

The only way is to use a variable first, you can call it varQuery or anything

Construct the query over there and then review it by placing a breakpoint and debugging the file.

Thanks

1 Like

The Query was initially stored in the in _arg

the in_arg was updated with the var ( holding the IDS) in the assign activity before executing the updated query

image

Don’t break your query across multiple lines.

1 Like

@postwick , thanks for the input.
What if the query is like 100 lines?

@Yomi_Oluwadara

Please try like this

In the query variable use value as

"Select * from abc where abc.colname in (<IDVALUE>)"

In the id values variable use value as you already did as an array

Now in the run query command give in_querytoExecute.Replace("<IDVALUE>",String.Join(",",StringAccountIDS.Select(function(x) x.ToString)))

Hope this helps

Cheers

@Anil_G
Thanks, I updated my qiery variable as shown below
image

I also updated the run query as shown below

However, I ended up with this error:
image

Not sure if its a spacing issue

it worked when I changed the type from String to String array

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.