Custom Errors in SQL Script - Advanced Installer Community

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!

kissack
Posts: 57
Joined: Fri Feb 04, 2011 11:45 am

Custom Errors in SQL Script

Hello,
I am running a SQL script in the installer that creates a new database. I would like a user to receive a message saying that the database already exists and that they need to re-run the installer and provide a different name.

The way I was thinking to do it was to provide a raiserror in the sql script with a return statement. But the script continues and I receive an exception that TABLE ... already exists . Later tried with IF condition. Same script stops execution in SQL management studio but in the installer not and continues to create table. here is a sample:

/*
**********************************************
CHECK IF THE DATABASE ALREADY EXISTS
**********************************************
*/
DECLARE @bContinue BIT;
SET @bContinue = 1
IF EXISTS(SELECT * FROM sys.databases WHERE name = '#DBNAME_PROP#')
BEGIN
RAISERROR ('Database ''#DBNAME_PROP#'' already exists. Restart the setup and provide a different database name.',16,1)
SET @bContinue = 0
END

IF @bContinue = 0
RETURN;
....
Create Tables follows
----------------------------------------------------

Am I on the right track and just need to modify my SQL script or is there a better way to achieve what I am trying to do ?

Thank you.

Kissack
mihai.petcu
Posts: 3860
Joined: Thu Aug 05, 2010 8:01 am

Re: Custom Errors in SQL Script

Hello Kissack and welcome to the Advanced Installer Forums,

Another approach would be to follow this tutorial to learn the concept and then adapt the properties and methods used to fit your needs. In the end you could condition your error message with the DATABASE_PROP.
Something like :

Code: Select all

(DATABASE_PROP >< YOUR_DATABASE_NAME)
Best,
Mihai
Mihai Petcu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
kissack
Posts: 57
Joined: Fri Feb 04, 2011 11:45 am

Re: Custom Errors in SQL Script

Hello Mihai,
thank you for the tips I took this way and made some decent progress. However now I have a different problem and if you don't mind me asking, here is something that works in a different way than expected.
Among other things I have this:

1.A dialog where user decides whether to create new or use existing database

2. SqlConnectionDlg - standard, only the OdbcResourceEdit is hidden.
I have some events:
SetProperty
[SqlConnectionString] Driver=[\{][ODBC_RES_PROP][\}];Server=[SERVER_PROP];Port=[PORT_PROP];Uid=[USERNAME_PROP];Pwd=[PASSWORD_PROP];
Based on the selection on 1. dialog I do ODBConnectionTest (for existing db) or pass to SqlAdminDlg
3.SQLAdminDlg - has radios to choose Trusted/SQL login (followed your tutorial)
Events:
SetProperty
-----------------
[SQL_AUTHORIZATION] : Uid=[LogonUser];Trusted_Connection=yes; for trusted
[SQL_AUTHORIZATION] : Uid=[SQL_ADMIN_USER];Pwd=[SQL_ADMIN_PASSWORD]; for SQL - these are the textboxes on the SqlAdminDlg
[SqlConnectionString]: Driver=SQL Server;Server=[SERVER_PROP];Port=[PORT_PROP];[SQL_AUTHORIZATION]
InlineScript Custom Action
-------------------------------
MsgBox Session.Property("SqlConnectionString") - to see what gets into SqlConnectionString - I get what I expect
DoAction
--------------------
ShowDatabases

My Problem is, that the ShowDatabases shows me databases for the user specified on SqlConnectionDlg and not for the one specified on SQLAdminDlg. So it looks like the SqlConnectionString does not get updated for the ShowDatabases custom action. Is this possible ?

Note:
The user name on SqlConnectionDlg is meant for user that will be created during the screnario when creating a new DB.

Would appreciate any hint that might help me to solve this, and equally hope that it is not some silly case sensitive/spell error on my side.

Thanks Kissack
Bogdan
Posts: 2796
Joined: Tue Jul 07, 2009 7:34 am
Contact: Website

Re: Custom Errors in SQL Script

Hi Kissack,

From what I can see the custom action "ShowDatabases" takes the username from the property "USERNAME_PROP", not from the SQLConnectionString.

Can you please try to set this property to the desired user before the custom action is called?

Regards,
Bogdan
Bogdan Mitrache - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
kissack
Posts: 57
Joined: Fri Feb 04, 2011 11:45 am

Re: Custom Errors in SQL Script

Hello Bogdan,

yes I discovered the same thing and solved the issue with setting the properties as you suggest with SQL type authorization.

ShowDatabases does not take the connection string from SQLConnectionString. Does this mean that using ShowDatabases does not work with Trusted login type authorization?

Thanks.

Kissack
mihai.petcu
Posts: 3860
Joined: Thu Aug 05, 2010 8:01 am

Re: Custom Errors in SQL Script

Hi Kissack,
Does this mean that using ShowDatabases does not work with Trusted login type authorization?
It does work, you just need to set the SqlTrustedConnection property. The input properties are listed in our show databases tutorial in the "Input Properties" section.

Regards,
Mihai
Mihai Petcu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
kissack
Posts: 57
Joined: Fri Feb 04, 2011 11:45 am

Re: Custom Errors in SQL Script

Thank you for all the comments, achieved all as needed. Kissack

Return to “Common Problems”