Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

pass a variable into connectstring with [ ...]

Hi fellow qlikies.

My first post to the community! Hope you can help.

I want to pass a variable into my connectstring to be able to change the database name. Read some examples on the forum but none of them explain how to do this when you work inside the square brackets.

    CONNECT TO [    Provider=SQLOLEDB.1;

                    Persist Security Info=True;

                    User ID=Sa;

                    Initial Catalog=DB_LIVE;

                    Data Source=server1;

                    Use Procedure for Prepare=1;

                    Auto Translate=True;

                    Packet Size=4096;

                    Workstation ID=ICT0231;

                    Use Encryption for Data=False;

                    Tag with column collation when possible=False] (XPassword is SCRAMBLED);

The above statement works fine. However setting ans inserting the variable won't work for me

SET DbName='DB_LIVE';

CONNECT TO [    Provider=SQLOLEDB.1;

                    Persist Security Info=True;

                    User ID=Sa;

                    Initial Catalog=DbName$;

                    Data Source=server1;

                    Use Procedure for Prepare=1;

                    Auto Translate=True;

                    Packet Size=4096;

                    Workstation ID=ICT0231;

                    Use Encryption for Data=False;

                    Tag with column collation when possible=False] (XPassword is SCRAMBLED);

What's wrong ??

3 Replies
rbecher
MVP
MVP

Hi coolman,

this should work with: Initial Catalog=$(DbName);

- Ralf

Astrato.io Head of R&D
Not applicable
Author

Hi Ralph,

Thanks for stepping in. Made the change as you suggested but no luck and a timeout on connection occurs. I reckon this is because the variable is inside the [ ] brackets, because the script also doesn't mark it grey as it would normally expect.

Any suggestions?

Not applicable
Author

Hi,

You could skip the Initial Database entry in the "Connect TO" and use a full qualified schema name instead.It would work with the $ expansion... DBName.Owner.Table...

I.e

SQL SELECT *

FROM $(DBName).dbo.TableName;

BR

Hans