Page 1 of 1

SQL script works (but not with varabiles)

Posted: Wed Aug 08, 2007 11:54 am
by nanashi16
I have this script I am trying to run, its quite simple. It works fine with one small problem. I have the script below

USE [Master]

DECLARE @spid smallint,
@SQLString varchar(50),
@cmd nvarchar(4000),
@data_path nvarchar(256)

-- retrieve data path based on location of master database
SET @data_path = (SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1) FROM master.sys.master_files WHERE database_id = 1 AND file_id = 1);

IF EXISTS (SELECT name FROM sys.databases WHERE name = N'xxxxTools')
BEGIN
DECLARE ffcursor CURSOR FAST_FORWARD FOR
select es.session_id from sys.dm_exec_sessions es join sysprocesses sp on (es.session_id = sp.spid)
where db_name(sp.dbid) = 'xxxxTools'

OPEN ffcursor

FETCH NEXT FROM ffcursor
INTO @spid

WHILE @@FETCH_STATUS = 0
BEGIN
set @SQLString = 'KILL ' + cast(@spid as varchar(5))
exec(@SQLString)

FETCH NEXT FROM ffcursor
INTO @spid
END

CLOSE ffcursor
DEALLOCATE ffcursor

RESTORE DATABASE [xxxxTools] FROM DISK = '$supportdir$xxxxtoolsdb' WITH FILE = 1, NOUNLOAD, REPLACE
END
ELSE
BEGIN
DECLARE @log_path nvarchar(256)
set @log_path = @data_path + 'xxxxTools_Log.LDF'
set @data_path = @data_path + 'xxxxTools_Data.MDF'

RESTORE DATABASE xxxxTools
FROM DISK ='$supportdir$xxxxtoolsdb'
WITH MOVE 'xxxxTools_Data' TO @data_path,
MOVE 'xxxxTools_Log' TO @log_path
END

Now you will see the From Disk section has $supportdir$xxxxtoolsdb
I have the xxxxtoolsdb in the creatives dir

Now if I replace the $supportdir$ with a hard coded path it works.
ex C:/programfiles/xxxxtools/xxxxtoolsdb


Does anybody have any ideas why its not working with $supportdir$ in it?

Steven

Posted: Thu Aug 09, 2007 6:12 am
by MichaelNesmith
Did you try as $SUPPORTDIR$?

Posted: Thu Aug 09, 2007 1:27 pm
by nanashi16
MichaelNesmith wrote:Did you try as $SUPPORTDIR$?


Yup, I tried upper case, lower case, camel case.