Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Database connection testing Script

Hello Community Folks,

I am trying to create a script that tests the database connection .I want variable value to be '0' when database connection fails so that variable value can be used to send email alerts about the connection issue.And I also want to create a qlik table with database name and sysdate as NULL if the db connection fails.Here is the script I created but when I give incorrect db creds in connection string, script is not entering IF block.Can some one correct my script.

Set ErrorMode=0;

Set NullInterpret = '';

LET vDBConnection = 1;


Trace 'Now Testing DBConnection';

OLEDB CONNECT TO [Provider=OraOLEDB.Oracle.1;Persist Security Info=True;User ID=xxxxx;Data Source=xxxx;Extended Properties=""] (XPassword is xxxxxx);

StreamHist:

SQL SELECT  'Databse Name' as DBNAME,SYSDATE FROM DUAL;

LET DBconnection_DATE =PEEK('SYSDATE',0,'StreamHist');

IF DBconnection_DATE = '' THEN

LET vDBConnection = 0;

StreamHist:

LOAD * INLINE [

    DBNAME, SYSDATE

    'Databse Name'

];

ENDIF

exit script;

1 Solution

Accepted Solutions
maxgro
MVP
MVP

maybe

Set ErrorMode=0;

Set NullInterpret = '';

LET vDBConnection = 1;

Trace 'Now Testing DBConnection';

OLEDB CONNECT TO [Provider=OraOLEDB.Oracle.1;Persist Security Info=True;User ID=xxxxx;Data Source=xxxx;Extended Properties=""] (XPassword is xxxxxx);

StreamHist:

SQL SELECT  'Databse Name' as DBNAME, SYSDATE, 'OK' as Status FROM DUAL;

LET DBconnection_DATE =PEEK('SYSDATE',0,'StreamHist');

IF len(trim(DBconnection_DATE)) = 0 THEN

  LET vDBConnection = 0;

  StreamHist:

  LOAD 'Database Name' as DBNAME, null() as SYSDATE, 'KO' as Status AutoGenerate 1;

ENDIF;

exit script;

View solution in original post

3 Replies
maxgro
MVP
MVP

maybe

Set ErrorMode=0;

Set NullInterpret = '';

LET vDBConnection = 1;

Trace 'Now Testing DBConnection';

OLEDB CONNECT TO [Provider=OraOLEDB.Oracle.1;Persist Security Info=True;User ID=xxxxx;Data Source=xxxx;Extended Properties=""] (XPassword is xxxxxx);

StreamHist:

SQL SELECT  'Databse Name' as DBNAME, SYSDATE, 'OK' as Status FROM DUAL;

LET DBconnection_DATE =PEEK('SYSDATE',0,'StreamHist');

IF len(trim(DBconnection_DATE)) = 0 THEN

  LET vDBConnection = 0;

  StreamHist:

  LOAD 'Database Name' as DBNAME, null() as SYSDATE, 'KO' as Status AutoGenerate 1;

ENDIF;

exit script;

Anonymous
Not applicable
Author

maxgro‌: Thank you.It worked

Not applicable
Author

Hi, you could also use ScriptError variable.

It takes values different from 0, when an error occures.

Combining it with if condition you can set the value for your variable:


let ScriptError=Null();     //clear ScriptError variable in case some other error occured few lines above


command causing failure


if ScriptError>0 then

  let vDBConnection =0;

ELSE

  let vDBConnection =1;

ENDIF