Multi database SQL query

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
jjeffman
Posts: 84
Joined: Tue 09 Nov 2004 12:22
Location: Porto Alegre-Rio Grande do Sul - Brazil

Multi database SQL query

Post by jjeffman » Fri 09 Nov 2018 18:46

Hello,

Someone has told me that UniDac supports querying different database systems with the same SQL text.

How can I do that? For example SQL Server uses
[datafield]
Oracle do not.

Is there any way of enable parts of a complex SQL query to make it compatible to each database system the application is connected ?

Thank you very much.

Jayme Jeffman

frickler
Posts: 37
Joined: Wed 04 Apr 2018 08:30

Re: Multi database SQL query

Post by frickler » Mon 12 Nov 2018 07:56


jjeffman
Posts: 84
Joined: Tue 09 Nov 2004 12:22
Location: Porto Alegre-Rio Grande do Sul - Brazil

Re: Multi database SQL query

Post by jjeffman » Mon 12 Nov 2018 14:31

Thank you very much for answering me.

I have attemted your sugestion but I am getting "empty macro error" when running the code bellow

Code: Select all

HST2_OCOR->SQL->BEGINUPDATE();
HST2_OCOR->SQL->CLEAR();
HST2_OCOR->SQL->ADD("SELECT OCORRENCIA_ID        \
			,INICIO                  \
			,ALARMEIN                \
			,TERMINO                 \
			,DURACAO                 \
			,ENVIOABERTURA           \
			,ENVIOFECHAMENTO         \
			,INTERVBUSCAINI          \
			,INTERVBUSCAFIM          \
			,BUSCAFALINF             \
			,BUSCAFALSUP             \
			,TRATAR                  \
			,ENVIAREMAIL             \
			,MODULO_ID               \
	 		{ IF ORACLE }               \
			 FROM HST2_OCORRENCIA        \
			 {ELSE}                      \
				 {IF DBSTO }               \
					 FROM [DBSTO].[DBO].[HST2_OCORRENCIA] \
				 {ELSE}                                   \
					 FROM [DBO].[HST2_OCORRENCIA]           \
				 {ENDIF}                                  \
			 {ENDIF}                                    \
			 WHERE TRATAR = 'S'");
HST2_OCOR->MACROBYNAME("ORACLE")->CLEAR();
HST2_OCOR->MACROBYNAME("DBSTO")->CLEAR();
IF( SERVERSTO == L"ORACLE" )
	HST2_OCOR->MACROBYNAME("ORACLE")->VALUE = "ORACLE";
ELSE
{
	IF(! EMPRESA.SQL_DATABASESTO.ISEMPTY() )
		HST2_OCOR->MACROBYNAME("DBSTO")->VALUE = EMPRESA.SQL_DATABASESTO ;
}
HST2_OCOR->SQL->ENDUPDATE();
// This line raises exception
I have tried using &Oracle and &DBSTO and it also fails.

Can you please point me an example of its usage a little bit more complex then the ones placed in the documentation.

Thank you very much.

Jayme Jeffman

Stellar
Devart Team
Posts: 496
Joined: Tue 03 Oct 2017 11:00

Re: Multi database SQL query

Post by Stellar » Tue 13 Nov 2018 15:18

Oracle is one of the predefined macros that helps to solve the most common differences in SQL syntax. Therefore, there is no need in adding this macro, it will be automatically added when using the Oracle provider. The list of predefined macros: devart.com/unidac/docs/unisql.htm#macroref
If you want to use the custom macro "DBSTO" for conditional jumps, then before executing the query you need to add it to the macro list, for example:
UniConnection1->Macros->Add("DBSTO", "", "");

Post Reply