Search code examples
sqlcrystal-reportssqlcommandcrystal-reports-xi

Nested SQL command in Crystal Reports


Is it possible to use the result from a command on another command in Crystal Reports?

As an example, I have a command

SELECT foo FROM bar WHERE foo2 IN (list)

saved as command_1
Now I want

SELECT xfoo FROM xbar WHERE xfoo2 IN (command_1.foo)

In a simple case I realise I could just combine them to

SELECT xfoo FROM xbar WHERE xfoo2 IN (SELECT foo FROM bar WHERE foo2 IN (list))

but in my real world case that gives me errors due to too many nested SELECTs.

My real world example if someone wants to give a specific and not just a general answer:

Existing command saved as pnr:

SELECT DISTINCT rem.pop_pid
FROM a_remiss rem
INNER JOIN a_bestall best ON rem.arem_id = best.arem_id
JOIN a_prov prov ON best.apro_id = prov.apro_id
WHERE prov.pro_provdat >= {ts '2006-01-01 00:00:00'}
AND prov.pro_provdat <= {ts '2011-12-31 23:59:59'}
AND best.ana_analyskod IN ('PKREAT', 'PKREA', 'PKREAN')
AND (
rem.pop_pid LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] 1[89]'
OR rem.pop_pid LIKE '[01][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] 20'
)

Wanted additional new command:

SELECT arem_id, apro_id, ana_analyskod
FROM a_bestall
WHERE ana_analyskod IN('ABSBIK', 'BCRPS')
AND arem_id IN (
SELECT arem_id
FROM a_remiss
WHERE rem_ank_dttm >= {ts '2006-01-01 00:00:00'}
AND rem_ank_dttm <= {ts '2012-01-31 23:59:59'}
AND pop_pid IN pnr.pop_pid
)

EDIT: Fixed a code error that now makes the nested SELECTs work, but it would still be nice to be able to nest commands.


Solution

  • The short answer is no, Commands can not be nested.

    If you can get the syntax to work in a query tool, it should work in a Command.

    If you are still getting syntax errors, consider using a different database driver. I've noticed that the 'native' drivers do a better job than ODBC.