Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
jonathan_dau
Contributor III
Contributor III

Dynamically set database name in SQL Select

Hi guys,

I don't know if my question has already been answered in another post but I couldn't find anything on the topic.

Here is the thing:

I know we can use a variable as parameter in a WHERE condition in a SQL SELECT e.g:

     SQL SELECT * FROM DB.Table WHERE Date=$(MyDate)  -- or something approaching.

Now my issue is that I have to use different databases when moving from DEV to PREPROD and PROD.

So taking the same example, in DEV the name of the database will be DB when in PREPROD it will be PPDB (don't ask why the back end team has created different database names in different environments).

What I am trying to do is to set dynamically the name of the database so that I don't have to change manually the name in all my applications e.g:

SET DBname = If(PREPROD, 'PP','')

SQL SELECT * FROM $(DBname)DB.Table

Do you know if this is possible to use a variable in the SQL Select itself, and if yes how I could to that.

Thanks in advance for your help.

Jon

1 Solution

Accepted Solutions
jonathandienst
Partner - Champion III
Partner - Champion III

Hi

Change the SET to LET DBname = If(PREPROD, 'PP','');

Then it should work.


HTH

Jonathan

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein

View solution in original post

4 Replies
Siva_Sankar
Master II
Master II

Not sure about dynamic selection of DB in SQL Select. But possible to change the odbc dynamically. Check out here

Dynamic Connection String in QlikView - QlikView Blog, QlikView Videos and Tips at QlikShare

jonathandienst
Partner - Champion III
Partner - Champion III

Hi

Change the SET to LET DBname = If(PREPROD, 'PP','');

Then it should work.


HTH

Jonathan

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
jagan
Luminary Alumni
Luminary Alumni

Hi,

Configure the database dynamically in your connection string so that you don't need to do this in all select queries.  Please check below


LET DBname = If(PREPROD, 'PP','')

ODBC CONNECT TO [orafin;DBQ=$(DBname)] (XUserId is saetst, XPassword is stedfdf);

Hope this helps you.

Regards,

Jagan.

jonathan_dau
Contributor III
Contributor III
Author

That was a pretty stupid question in fact and really easy to fix.....

Thanks guys useful answers.

Cheers

Jon