I'm new to the forum. I'm having a problem with the return of a SQL query. I'm looking to get the result of a SELECT query and reuse it in an SELECT query. I've tried a lot of different scripts but I can't do it. Here's my script.I'm looking for a way to convert my ESSAI3 result into a value that the INSERT query could include
I use MSSQL in serve :
essai =system.dataset.toDataSet(system.db.runPrepQuery("SELECT DISTINCT CodeStockage FROM tStockageInfo WHERE CodeMatiere = (?)", [CodeMat] ))
essai2 = system.dataset.toDataSet(system.db.runQuery("SELECT DISTINCT CodeStockage FROM tStockage WHERE CodeGroupe=1 AND NOT CodeStockage IN (Select CodeStockage FROM tStockageInfo) "))
See Wiki - how to post code on this forum. There's a pencil icon below the post so you can edit. You can also use Python's triple quotes to wrap your SQL for readability.
CodeMat = system.tag.readBlocking(['[default]Cde_Matiere'])[0].value
essai =system.dataset.toDataSet(system.db.runPrepQuery("""
SELECT DISTINCT CodeStockage
FROM tStockageInfo
WHERE CodeMatiere = (?)""",
[CodeMat])
)
essai2 = system.dataset.toDataSet(system.db.runQuery("""
SELECT DISTINCT CodeStockage
FROM tStockage
WHERE CodeGroupe=1
AND NOT CodeStockage IN (
SELECT CodeStockage FROM tStockageInfo
)"""
)
)
essai3 = system.dataset.appendDataset(essai2, essai)
The code below won't work.
system.db.runPrepQuery("""
SELECT DISTINCT LibelleL1
FROM tStockage
WHERE CodeStockage IN ((?))",
[essai3]
)
The prepared query can't take a dataset or even a list.
I think the best solution would be to convert the dataset to a comma separated string using a join function. Then pass that into the IN(_).
SELECT DISTINCT s.LibelleL1
FROM tStockage s
LEFT JOIN tStockageInfo si
ON s.CodeStockage = si.CodeStockage
WHERE si.CodeMatiere = ? OR (s.CodeGroupe = 1 AND si.CodeStockage IS NULL)
OK, thanks a lot.
however if I want to retrieve a dataset from a SELECT query, how can I convert it so that in my INSERT query the language corresponds ?