Page 1 of 1

SQL Script

Posted: Tue Jun 12, 2007 4:36 pm
by Lin
I am wondering if there is any good way to the location of the master databse mdf and ldf files in InstallAware.
I need this location in order to install my other database files that I need to attach.

Thanks

Posted: Wed Jun 13, 2007 9:45 am
by BrandonK
Lin,

My install runs a SQL script to create the app database from scratch. It finds the path to master.mdf using the code below.

Not sure how you could do this in IA script; SQL scripting isn't my strong suit. Maybe run the first half in an IA "MS SQL Server" action and return @SQLDataPath into an IA variable?

Code: Select all

DECLARE @SQLDataPath AS NVARCHAR(256)
SET @SQLDataPath = (
   SELECT SUBSTRING(physical_name, 1, CHARINDEX('master.mdf', LOWER(physical_name)) - 1)
   FROM master.sys.master_files
   WHERE database_id = 1 AND file_id = 1
   )

EXECUTE ('
   CREATE DATABASE [YOUR_DB_NAME]
   ON (
      NAME = ''YOUR_DB_NAME'',
      FILENAME = ''' + @SQLDataPath + 'YOUR_DB_NAME.mdf'',
      SIZE = 3072KB ,
      MAXSIZE = UNLIMITED,
      FILEGROWTH = 10%
      )
   LOG ON (
      NAME = ''YOUR_DB_NAME Log'',
      FILENAME = ''' + @SQLDataPath + 'YOUR_DB_NAME.ldf'',
      SIZE = 1024KB ,
      MAXSIZE = 1GB , FILEGROWTH = 10%
      )
   COLLATE SQL_Latin1_General_CP1_CI_AS
   ')
GO


Hope this helps,
Brandon