OSQL脚本文件执行错误

时间:2012-02-08 18:07:23

标签: sql-server osql

我有一个脚本文件(实际上是一系列的..),它们在查询分析器中打开时,执行完美。

当我通过OSQL命令执行它时,它无法说它找不到我知道存在的数据库......

然而,当我在Q / A中以相同的起始状态执行相同的脚本时,它可以100%工作..

另一件事,OSQL说失败的那一行是第54行“BACKUP ...”

    DECLARE @DEVICENAME VARCHAR(1000)
    DECLARE @SYSDATABASE VARCHAR(1000)
    DECLARE @backupSetId INT
    -- MD 
    DECLARE @POS INT
    DECLARE @VERSIONNUMBER INT
    -- NEed to Get The Version it somes to us as MMM.mmm.vvvv and the 1st decimal can         be in position 2 or 3.. 
    -- So we cast the Version to a varchar cast(SERVERPROPERTY('ProductVersion')as varchar(50))
    -- then we find the CHARiNDEX OF THE DECIMAL (IT'LL ALWAYS BE THERE. 
    SET @POS=CHARINDEX('.',cast(SERVERPROPERTY('ProductVersion')as varchar(50)))-1  
    Set @VERSIONNUMBER = left(cast(SERVERPROPERTY('ProductVersion')as         varchar(50)),@POS)

--if left (cast((SERVERPROPERTY('productversion')) as varchar(25)),1) ='8'
    IF @VERSIONNUMBER='8' 
    BEGIN
        SET @DEVICENAME = (SELECT TOP 1 REPLACE(phyname,'master.mdf','') + 'GenericDBName_Archive_Backup.dat' AS DEVICENAME  FROM master..sysdevices WHERE name ='master')
    END
ELSE
    BEGIN
        SET @DEVICENAME = (SELECT TOP 1 REPLACE(physical_name,'master.mdf','') + 'GenericDBName_Archive_Backup.dat' AS DEVICENAME FROM sys.master_files WHERE name ='master')
    END 

    print @devicename

    if exists(select * from master..sysdatabases where name = 'GenericDBName_Archive' and CONVERT(varchar(24), crdate, 102) <= CONVERT(varchar(24), getdate(), 102))
        BEGIN
            if not exists(select * from master..sysdatabases where name = 'GenericDBName_Archive_Backup' and CONVERT(varchar(24), crdate, 102) <= CONVERT(varchar(24), getdate(), 102))
                BEGIN
                    USE [MASTER]
                    BACKUP DATABASE [GenericDBName] TO  DISK =@DEVICENAME WITH NOFORMAT, INIT,  NAME = N'GenericDBName-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
                    select @backupSetId = position from msdb..backupset where database_name=N'GenericDBName' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'GenericDBName' )

                    if @backupSetId is null 
                    BEGIN 
                        raiserror(N'Verify failed. Backup information for database ''GenericDBName'' not found.', 16, 1) 
                    END

                    RESTORE VERIFYONLY FROM  DISK = @DEVICENAME WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
                END
            else
                BEGIN
                    RAISERROR (N'The GenericDBName_Archive_Backup already exists. Is not neccesary to make backup again.', -- Message text.
                           10, -- Severity,
                           1, -- State,
                           N'number', -- First argument.
                           5); -- Second argument.
                END 

        END

    ELSE
        BEGIN
            USE [MASTER]
            BACKUP DATABASE [GenericDBName] TO  DISK =@DEVICENAME WITH NOFORMAT, INIT,  NAME = N'GenericDBName-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
            select @backupSetId = position from msdb..backupset where database_name=N'GenericDBName' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'GenericDBName' )

            if @backupSetId is null 
                BEGIN 
                    raiserror(N'Verify failed. Backup information for database ''GenericDBName'' not found.', 16, 1) 
                END

            RESTORE VERIFYONLY FROM  DISK = @DEVICENAME WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
        END     

        BEGIN

            RAISERROR (N'Backup is created to restore on new archive database.', -- Message text.
                10, -- Severity,
                1, -- State,
                N'number', -- First argument.
                5); -- Second argument.
        END

0 个答案:

没有答案