We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

jderosa
Posts: 19
Joined: Wed Sep 28, 2016 8:27 pm

Need to do conditional operation

I need to do something conditionally.

I have 3 steps

1 Do Some SQL Database stuff (Backup)
2 Do Some SQL Database stuff (Install)
3 Do Some SQL Database stuff (Restore)

If a particular database is installed, I want to execute steps 1 (a backup) step 2 (an install) and step 3 (a partial restore of what was backed up in 1)
If the database is not installed I will just do step 2

All of the steps in 1,2 and 3 are defined in The SQL Databases section, but I can't seem to figure out how to make the steps operate conditionally.

Sorry if this is a n00b question, but I have only been using the product for a week.

Thanks for any help.
Eusebiu
Posts: 4971
Joined: Wed Nov 14, 2012 2:04 pm

Re: Need to do conditional operation

Hi and welcome to our forums.

In order to achieve this you can simply add a condition in your scripts, so that they will be executed only if the database exists as in the example below:

Code: Select all

IF EXISTS(SELECT * FROM master.sys.sysdatabases WHERE name='DatabaseName')
BEGIN
   <your SQL script code
END
Another method is to select each of your backup, install and restore database scripts and set the appropriate condition for them using some search properties.

In order to search if a database exists you can use one of the following solutions:

1. The recommended way is to create a script that queries the database and run it in the "SQL Query" tab from the "SQL Databases" page under a database connection. For example:

Code: Select all

SELECT * FROM dbo.sysdatabases WHERE name = 'DatabaseName'
In the "Data Binding" section add a property for each result of the query (i.e. if the query returns three values you can add three properties; if you add one property, only the first result will be stored, i.e. DATABASE_NAME).

You can use the "Schedule Action" blue link and schedule this query to run before the "Add Resources" action group from the "Custom Actions" page. If the data binding property contains a value it means that the database exists, otherwise it is not installed.

2. The second method it to add a "Browse SQL Database" action, click on the "Show advanced options" blue link and "Put the result into a property" that you desire (i.e. FOUND_DATABASES). A flat, comma separated list of the available databases (names only) will be dumped into this property, so you can use the following condition to make sure that your database exists: FOUND_DATABASES >< "DatabaseName" (TRUE if the property contains the right string).

3. Another way to determine if a database is installed is to do a "File search/Registry search" for the file or the registry that contains the database. Just go to "Search" page in the "Custom Behavior" section and use the "New File Search" toolbar button. You can rename the result property according to the search you perform (i.e. DATABASE_EXISTS).

Let me know if this helped, otherwise give me more details about your scenario.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jderosa
Posts: 19
Joined: Wed Sep 28, 2016 8:27 pm

Re: Need to do conditional operation

Thanks, lot's of good info here.

This gives me the how do I set a variable portion of the problem (in a couple of different ways)

I have the #2 and #3 sections of sql code defined in SQL databases, how do I get them to execute conditionally if this variable I set contains the database name.

So if I set PREV_DB='Existdb' using your #1 recommended method how do I set the scripts to execute only if PREV_DB='Existdb'

Thanks
jderosa
Posts: 19
Joined: Wed Sep 28, 2016 8:27 pm

Re: Need to do conditional operation

Ok figured out how to get it to run conditionally.

Now I need to set this variable, after the user has defined the database connection information. I need to display a dialog indicating that there is a previously installed database that will be backed up then removed.

Is there a way to do this ?
Eusebiu
Posts: 4971
Joined: Wed Nov 14, 2012 2:04 pm

Re: Need to do conditional operation

Hi,

In order to display a message to the user you can use a "Display message box" custom action "without sequence" and place in on the "Next" button of the dialog where the user introduces the connection information.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jderosa
Posts: 19
Joined: Wed Sep 28, 2016 8:27 pm

Re: Need to do conditional operation

Ok I have most of this working now.

The UI has a conditional dialog that displays if the database already exists.

But I can't seem to get the SQL Scripts to operate conditionally.

I have Run at Install checked, and condition =

Code: Select all

Upgrade2.0 = "Bernoulli"
The UI code that runs when

Code: Select all

Upgrade2.0 = "Bernoulli"
is working fine, so I know the Upgrade2.0 variable is being set.
Eusebiu
Posts: 4971
Joined: Wed Nov 14, 2012 2:04 pm

Re: Need to do conditional operation

Hi,

I see that you use the "Upgrade2.0" property as a condition, but this property is private (contains lower-case letters in its name) and therefore its updated value will not be passed from the "Wizard Dialog Stage" section to "Install Execution Stage" section. In order to accomplish what you want, you need to use a public property (its name must have all the letters in upper-case). Can you test this and see if it works?

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jderosa
Posts: 19
Joined: Wed Sep 28, 2016 8:27 pm

Re: Need to do conditional operation

I changed the variable (using text editor global search and replace) from Upgrade2.0 to UPGRADE20. I wasn't sure if the "." would affect it so I removed it.

Seems to be working now, will finish testing.

Thanks.
Eusebiu
Posts: 4971
Joined: Wed Nov 14, 2012 2:04 pm

Re: Need to do conditional operation

You're welcome. I'm glad you got this working.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”