Hello. There were problems in compiling the correct SQL - Query. How to call a sql query and pass a parameter to the input from another table?

I have a complicated sql query, written as query with the name qryARAT2B_EXT and receiving the parameter PARAM.

SELECT * FROM ( SELECT * FROM ( SELECT *, firstStudy, ABS(DATEDIFF('d', firstStudy, Check_Date)) as diff FROM ( SELECT *, ( SELECT TOP 1 Check_Date FROM qryARAT2B WHERE PATNR = [PАРАМ] ORDER BY Check_Date ) AS firstStudy FROM ( SELECT * FROM qryARAT2B WHERE PATNR = [PАРАМ] ) AS myPatientsWithStudy ) AS myPatientsFirstStudy ) WHERE diff = 0 ) AS T1 LEFT JOIN ( SELECT * FROM ( SELECT *, firstStudy, ABS(DATEDIFF('d', firstStudy, Check_Date)) as diff FROM ( SELECT *, ( SELECT TOP 1 Check_Date FROM qryARAT2B WHERE PATNR = [PАРАМ] ORDER BY Check_Date ) AS firstStudy FROM ( SELECT * FROM qryARAT2B WHERE PATNR = [PАРАМ] ) AS myPatientsWithStudy ) AS myPatientsFirstStudy ) WHERE diff = 4 ) As T2 ON T1.PATNR = T2.PATNR 

When I open it from ms-access, it asks me for the value of [PAPAM] and returns the result.

I need to write sql in which I could pass the parameter PAPAM.

 SELECT * FROM qryARAT2B_EXT2 WHERE PAPAM = 1 

But ms-access still requests it, maybe there is some special syntax?

  • You have it in many places. if requested, it means that somewhere in the request it remains. - Mike
  • one
    Your initial query syntax requires you to enter a parameter. Trying to pass a parameter from the outside into a subquery, you actually want to use it as correlated, but MS Access does not understand such constructions in principle. So correct the text of your "complex sql query". - Akina

0