Linked Server error - Msg 7311, Level 16, State 2

We have three SQL Servers:
SQL 2008 Instance w/ SP1 cumulative update #5; running on Windows Server 2008 r2
SQL 2008 Instance w/ SP1 cumulative update #5; running on Windows Server 2003 r2
SQL 2000 Instance w/ SP4; running on Windows Server 2003 r2 W/ SP2
We have a linked server connection from #2 to #3, which works perfectly.  However, our linked server connection from #1 to #3 does not work.  here is the error message we get:
OLE DB provider "SQLNCLI10" for linked server "CCSSQLTEST01A" returned message "Unspecified error".
OLE DB provider "SQLNCLI10" for linked server "CCSSQLTEST01A" returned message "The stored procedure required to complete this operation could not be found on the server. Please contact your system administrator.".
Msg 7311, Level 16, State 2, Line 1
Cannot obtain the schema rowset "DBSCHEMA_TABLES_INFO" for OLE DB provider "SQLNCLI10" for linked server "CCSSQLTEST01A". The provider supports the interface, but returns a failure code when it is used.
I've read articles saying we may need to run a specific script on the server we are linking to, due to missing SPs, but how can that be possible if the other linked server connection works?  We are linking from two identical
SQL Server instances (other than the OS)...
The only other difference I know of, is that #1 was an upgrade from SQL 2005, but #2 was a clean install of SQL 2008.
Suggestions? 
Clint

I really don't think this is the case otherwise in my .Net applications I'd have to determine the version of the target SQL server, then instantiate the appropriate connection object.  Things just don't work that way.   I'm pretty sure
you will find that a SQL Native Client 10 provider can connect to anything from SQL 2008 and below, but not anything above (once it is released :).
The real issue is given in the message. Cannot obtain the schema rowset "DBSCHEMA_TABLES_INFO"
If
you source server is 64 bit and your target is 32 bit then you will need to do the following.  (I have to pass credit to Kun Sik Lee of Costar Group for this one)
create
procedure sp_tables_info_rowset_64
     @table_name
sysname,
     @table_schema    
sysname =
null,  
     @table_type
nvarchar(255)
= null
as
declare @Result
int set @Result
= 0
exec @Result
= sp_tables_info_rowset
@table_name, @table_schema, @table_type
GO
use [master]
GO
GRANT
EXECUTE ON [dbo].[sp_tables_info_rowset_64]
TO [LinkServerUser]
GO

Similar Messages

  • Increase the DB size in Azure gives the Error " Msg 40197, Level 16, State 1, Line 1 The service has encountered an error processing your request. Please try again. Error code 40671."

    Increase the DB size in Azure gives the following error
    Msg 40197, Level 16, State 1, Line 1 The service has encountered an error processing your request. Please try again. Error code 40671.
    i have used following ALTER query,
    ALTER DATABASE database_name MODIFY (EDITION='Business', MAXSIZE=10GB);
    Regards
    Vel

    Hi Taiye,
    According to your description, in theory, we can modify the size of database, and the T-SQL statement in your post is right. So we need to verify if there is no problem with network when you execute the modification statement, in addition, you must be connected
    to the master database when executing the ALTER DATABASE statement. As the error message, I recommend you login again and connect to the master database then run the statement again.
     For more information, see: ALTER DATABASE (Windows Azure SQL Database)
    If the error is still exist, the Microsoft support engineer will help to solve the problem from backend. Sometime delay might be expected. Your patience is greatly appreciated.
    Thanks,
    Sofiya Li
    If you have any feedback on our support, please click here.
    Sofiya Li
    TechNet Community Support

  • Query in SQL 2008R2 error Msg 1087, Level 15, State 2, Line 31 Must declare the table variable

    Hello Experts, 
    Can You help me; I get the follwing error in my query
    Msg 1087, Level 15, State 2, Line 31
    Must declare the table variable "@ASE_SUBART_GROEP".
    the @ASE_SUBART_GROEP is a self-defined table in this SQL environment.
    Maybe you know the error or the missing part
    Thanks in advance, Jos Dielemans

    I'm not familiar with SAP Business One unfortunately, so can only be of limited help here. But I'm going to guess that @ASE_SUBART_GROEP is a table variable passed to a stored procedure and is defined and passed down by the application, so you won't
    be able to copy-paste this code directly into management studio and get it to work.
    You could remove the join to return *all* records. Although the dataset might be very big
    SELECT T0.CardCode AS 'Klantnr'
    ,T0.CardName AS 'Klantnaam'
    ,T4.GroupName AS 'Klantgroep'
    ,T9.Descr AS 'Merknaam'
    ,T5.SlpName AS 'Verkoper'
    ,T3.MailCity AS 'Leverplaats'
    ,T7.NAME AS 'Leverland'
    ,T3.City AS 'Factuurplaats'
    ,T7.NAME AS 'Factuurland'
    ,T0.DocNum AS 'Documentnr'
    ,T0.DocDate AS 'Datum'
    ,Cast((Datepart(Year, T0.DocDate)) AS VARCHAR) AS 'Jaar'
    ,RIGHT('00' + CAST(DATEPART(MONTH, T0.DocDate) AS VARCHAR(2)), 2) AS 'Maand'
    ,(Cast((Datepart(Year, T0.DocDate)) AS VARCHAR) + '-' + RIGHT('00' + CAST(DATEPART(MONTH, T0.DocDate) AS VARCHAR(2)), 2)) AS 'Periode'
    ,T1.ItemCode AS 'Artikelnr'
    ,T2.ItemName AS 'Artikelnaam'
    ,T1.Quantity AS 'Aantal VEH'
    ,T2.SVolume AS 'Aantal EH per VEH'
    ,(T1.Quantity * T2.SVolume) AS 'Aantal EH'
    ,T8.UnitName AS 'EH-Naam'
    ,T6.ItmsGrpNam AS 'Artikelgroep'
    ,T2.U_ItemGrp AS 'Hoofdgroep'
    ,T10.NAME AS 'Subgroep'
    ,T1.CogsAcct AS 'Kostpr.rek.'
    ,T1.AcctCode AS 'Opbrengstrek.'
    ,T1.LineTotal AS 'BrutoOmzet'
    ,T0.DiscPrcnt AS 'Korting'
    ,(+ T1.LineTotal * (100 - T0.DiscPrcnt) / 100) AS 'NettoOmzet'
    FROM dbo.OINV T0
    INNER JOIN dbo.INV1 T1 ON T0.DocEntry = T1.DocEntry
    INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
    INNER JOIN OCRD T3 ON T0.CardCode = T3.CardCode
    INNER JOIN OCRG T4 ON T3.GroupCode = T4.Groupcode
    INNER JOIN OSLP T5 ON T3.SlpCode = T5.SlpCode
    INNER JOIN OITB T6 ON T2.ItmsGrpCod = T6.ItmsGrpCod
    INNER JOIN OCRY T7 ON T3.Country = T7.Code
    LEFT OUTER JOIN OLGT T8 ON T2.SVolUnit = T8.UnitCode
    LEFT OUTER JOIN UFD1 T9 ON T3.U_ZPgroep = T9.FldValue
    AND TableID = 'OCRD'
    AND FieldID = 2
    --LEFT OUTER JOIN @ASE_SUBART_GROEP T10 ON T2.U_ASE_SUB_ARTGROEP = T10.Code
    WHERE T0.DocDate >= '20120101'
    AND T0.Canceled = 'N'
    UNION ALL
    SELECT T0.CardCode AS 'Klantnr'
    ,T0.CardName AS 'Klantnaam'
    ,T4.GroupName AS 'Klantgroep'
    ,T9.Descr AS 'Merknaam'
    ,T5.SlpName AS 'Verkoper'
    ,T3.MailCity AS 'Leverplaats'
    ,T7.NAME AS 'Leverland'
    ,T3.City AS 'Factuurplaats'
    ,T7.NAME AS 'Factuurland'
    ,T0.DocNum AS 'Documentnr'
    ,T0.DocDate AS 'Datum'
    ,Cast((Datepart(Year, T0.DocDate)) AS VARCHAR) AS 'Jaar'
    ,RIGHT('00' + CAST(DATEPART(MONTH, T0.DocDate) AS VARCHAR(2)), 2) AS 'Maand'
    ,(Cast((Datepart(Year, T0.DocDate)) AS VARCHAR) + '-' + RIGHT('00' + CAST(DATEPART(MONTH, T0.DocDate) AS VARCHAR(2)), 2)) AS 'Periode'
    ,T1.ItemCode AS 'Artikelnr'
    ,T2.ItemName AS 'Artikelnaam'
    ,- T1.Quantity AS 'Aantal VEH'
    ,T2.SVolume AS 'Aantal EH per VEH'
    ,(- T1.Quantity * T2.SVolume) AS 'aantal EH'
    ,T8.UnitName AS 'EH-Naam'
    ,T6.ItmsGrpNam AS 'Artikelgroep'
    ,T2.U_ItemGrp AS 'Hoofdgroep'
    ,T10.NAME AS 'Subgroep'
    ,T1.CogsAcct AS 'Kostpr.rek.'
    ,T1.AcctCode AS 'Opbrengstrek.'
    ,- T1.LineTotal AS 'BrutoOmzet'
    ,T0.DiscPrcnt AS 'Korting'
    ,(+ T1.LineTotal * (100 - T0.DiscPrcnt) / 100) AS 'NettoOmzet'
    FROM dbo.ORIN T0
    INNER JOIN dbo.RIN1 T1 ON T0.DocEntry = T1.DocEntry
    INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
    INNER JOIN OCRD T3 ON T0.CardCode = T3.CardCode
    INNER JOIN OCRG T4 ON T3.GroupCode = T4.Groupcode
    INNER JOIN OSLP T5 ON T3.SlpCode = T5.SlpCode
    INNER JOIN OITB T6 ON T2.ItmsGrpCod = T6.ItmsGrpCod
    INNER JOIN OCRY T7 ON T3.Country = T7.Code
    LEFT OUTER JOIN OLGT T8 ON T2.SVolUnit = T8.UnitCode
    LEFT OUTER JOIN UFD1 T9 ON T3.U_ZPgroep = T9.FldValue
    AND TableID = 'OCRD'
    AND FieldID = 2
    --LEFT OUTER JOIN @ASE_SUBART_GROEP T10 ON T2.U_ASE_SUB_ARTGROEP = T10.Code
    WHERE T0.DocDate >= '20120101'
    AND T0.Canceled = 'N'
    ORDER BY T0.CardCode
    The join to the table variable has been commented out above, so the code should run. After that you might want to update the WHERE clause to include only particular sub-groups

  • Linked Server Error from SQL2008R2 to SQL2000

    I am trying to Execute Query/SP from my local machine which SQL 2008R2 To SQL 2000 32 bit. 
    Created Linked Server from Local machine to SQL2000 Machine..
    Query /SP run fine from SSMS but when I am trying to run through this below script keep giving me error .
    I converted Select query into SP 
     EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'SQL-MailAlerts',
        @recipients = '**********',
        @query = 'EXEC dbo.QueryResult',
        @query_attachment_filename = 'result.csv',
        @attach_query_result_as_file = 1,
        @body_format = 'text' ;
    Error :
    Msg 22050, Level 16, State 1, Line 0
    Error formatting query, probably invalid parameters
    Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 504
    Linked servers cannot be used under impersonation without a mapping for the impersonated login.
    Basically trying to get Query result from SQL 2000 Database into mail if anyone has better idea let me know.
    Thanks
    Please Mark As Answer if it is helpful. \\Aim To Inspire Rather to Teach A.Shah

    Maybe you should read the posts before you reply? Logicinisde runs DB mail on SQL 2008R2, but is trying to retrieve data from SQL 2000 instances.
    SQL 2000 may be out of support, but that will not prevent users from keeping their instances around as long as needed. In some cases they may have no choice, because they are running a third-party system which does not support later versions of SQL Server.
    I have gathered that this is particularly common in the healthcare industry in the US.
    Hi Erland Sommarskog,
    Thanks for your suggestion, the scenario which you post is really to take into consideration. I suggest our customer upgrading the SQL Server version for helping resolve his issues which may be encountered on the SQL Server 2000. Thanks for your understanding.
    Thanks for your post again.
    Regards,
    Sofiya Li
    Sofiya Li
    TechNet Community Support

  • Msg 2601, Level 14, State 1, Procedure sp_flush_commit_table, Line 15 Cannot insert duplicate key row in object 'sys.syscommittab' with unique index 'si_xdes_id'. The duplicate key value is (2238926153). The statement has been terminated.

    I am using SQL server 2008 R1 SP3. And when we are doing back up operations we are facing the below error
    Msg 2601, Level 14, State 1, Procedure sp_flush_commit_table, Line 15
    Cannot insert duplicate key row in object 'sys.syscommittab' with unique index 'si_xdes_id'. The
    duplicate key value is (2238926153).
    The statement has been terminated.
    Please assist me with your inputs.
    Thanks,
    Rakesh.

    Hello,
    Did you enable change tracking on the database? If so, please try to disable and re-enable the change tracking.
    The following thread is about the similar issue, please refer to:
    http://social.msdn.microsoft.com/forums/sqlserver/en-US/c2294c73-4fdf-46e9-be97-8fade702e331/backup-fails-after-installing-sql2012-sp1-cu1-build-3321
    Regards,
    Fanny Liu
    Fanny Liu
    TechNet Community Support

  • Msg 116, Level 16, State 1, Line 6 Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

    Hello,
    Can anyone help me?
    select year(po.rdata) as 'Ano', 
    sum(etotal) as 'Valorp' ,
    (select year(fo.data),Sum(etotal) 
    FROM fo (nolock) 
    where  not exists ( select 1 from pl (nolock) where pl.adoc = fo.adoc and pl.cm = fo.doccode ) 
    group by year(fo.data))as 'Valorap'   
    from po 
    group by year(po.rdata)
    order by year(po.rdata)
    Error 
    Msg 116, Level 16, State 1, Line 6
    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
    Consultora

    Hi Prashanth,
    select year(po.rdata) as 'Ano',
    sum(etotal) as 'Valorp' ,
    (select Sum(etotal)
    FROM fo (nolock)
    where not exists ( select 1 from pl (nolock) where pl.adoc = fo.adoc and pl.cm = fo.doccode )
    group by year(fo.data))as 'Valorap'
    from po(nolock)
    group by year(po.rdata)
    order by year(po.rdata)
    Gives me the error: 
    Msg 512, Level 16, State 1, Line 1
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
    Thanks
    Consultora

  • I am geeting problem whem i run bulk insert query i got error Location:_ bcpimp.cpp:1797 Expression:_ cbOutTemp cbOutputStr SPID:__ 52 Process ID:_ 2364 Msg 3624, Level 20, State 1, Line 3 A system assertion check has failed. Check the SQL Server error

    Location: bcpimp.cpp:1797
    Expression: cbOutTemp < cbOutputStr
    SPID: 52
    Process ID: 2364
    Msg 3624, Level 20, State 1, Line 3
    A system assertion check has failed. Check the SQL Server error log for details. Typically, an assertion failure is caused by a software bug or data corruption. To check for database corruption, consider running DBCC CHECKDB. If you agreed to send dumps to
    Microsoft during setup, a mini dump will be sent to Microsoft. An update might be available from Microsoft in the latest Service Pack or in a QFE from Technical Support. 
    Msg 0, Level 20, State 0, Line 0
    A severe error occurred on the current command.  The results, if any, should be discarded.

    If you check your sql server error log folder you will see dump files created. You need to send those dump files to Microsoft for inspection because outside of microsoft product support the symbols for reading the dumps are not available without which it
    wont be easy to find the issue.
    http://support.microsoft.com/kb/2028589
    In the meantime check if you are on the latest version of Service Pack and if not apply the latest service pack. Majority of these kind of assertions are bugs which are solved in Service Packs/Cumulative Updates.
    Regards, Ashwin Menon My Blog - http:\\sqllearnings.com

  • Linked server error from sql server to oracle

    Hi
    Please help i am getting below error
    Problem Description: OLE DB provider "OraOLEDB.Oracle" for linked server "ORACLE10G" returned message "ORA-01403: no data found".
    Msg 7346, Level 16, State 2, Line 1
    Cannot get the data of the row from the OLE DB provider "OraOLEDB.Oracle" for linked server "ORACLE10G".
    Platform details
    SQl server end
    =====================
    SQl server version :2005
    Oracle client version :10.2.0.1.0
    sql server Platform:widow server 2003 standard edition 32 bit
    ===========================
    oracle server end
    * DB Server = 10.2.0.4
    * Platform = IBM AIX
    AIX version=5.3
    Regards

    Hi,
    This forum is for connecting to non-Oracle databases from Oracle.
    There is a specific forum for Oracle Provider for OLE DB so it would be better to ask the question in that forum -
    Oracle Provider for OLE DB
    Regards,
    Mike

  • Linked server error

    When I run below query its giving error:
       SELECT *
    FROM [P3FI.WORLD]..[ADHOC].[V_POSITION_FW]
    Msg 7356, Level 16, State 1, Line 1
    The OLE DB provider "OraOLEDB.Oracle" for linked server "P3FI.WORLD" supplied inconsistent metadata for a column. The column "CONFID_AGREEMENT_SIGNED" (compile-time ordinal 3) of object ""ADHOC"."V_CONTRACTOR_FW""
    was reported to have a "DBCOLUMNFLAGS_ISNULLABLE" of 0 at compile time and 32 at run time.
    When I run this query  
    select * from openquery([P3FI.WORLD],'select * from adhoc.V_CONTRACTOR_FW')  
    it runs fine
    Please suggest
    Best regards,
    Vishal

    What happens is that when SQL Server compiles the query, it interrogates the OLE DB provider about the metadata for the table, and the OLE DB provider appears to say that the column does not accept null. But later when the query is exectued, SQL Server is
    told that the column is nullable. SQL Server does not like being lied to, and drops out.
    This is not very simple to troubleshoot, at least not for an SQL Server person, because the prime suspect is the OLE DB provider, which comes from Oracle.
    However, there is one possibility where SQL Server is involved: Assume that this query already was in the cache, because someone ran it yesterday. Since then the column on the Oracle side was altered to permit NULL. When you do this inside SQL Server, all
    queries related to the table are flushed from the cache, but obviously this does not happen when you change a table on a different server.
    One way to test this is to run the query with a different query text (so that you don't get a cache hit), for instance:
    SELECT * FROM [P3FI.WORLD]..[ADHOC].[V_POSITION_FW] AS MyAliasNewOfToday
    (The cache lookup is from a hash of the query text.)
    Erland Sommarskog, SQL Server MVP, [email protected]

  • BACKUP LOG suddenly failed with Msg 35250, Level 16, State 11 The connection to the primary replica is not active. The command cannot be processed.

    I have AlwaysOn SQL 2012 Enterprise set-up using Windows Failover Clustering Services (not FCI), and have 1 Primary node (P), 1 Synchronous Commit Auto Failover (SC), and 1 Asynchronous Commit Manual Failover (AC) node.  It is set up to prefer secondary,
    with the highest priority given to AC node.
    I am using Ola Hollengren's scripts for Database Maintenance jobs, including a native BACKUP LOG job for the transaction logs of all user databases on a 1 minute schedule.  His scripts already consider AlwaysOn, and although the job is set-up on all
    3 nodes, only ever runs on AC node.
    The job has been running successfully since initial set-up almost 1 year ago, but suddenly yesterday morning started to fail with the following error, only on 1 of the 13 databases in my availability group:
    Date and time: 2014-06-08 09:36:11
    Command: BACKUP LOG [my_db] TO DISK = N'E:\MSSQL\\Transaction Dumps\my_db\MySQLCL$MySQLAG_my_db_20140608_093610_U_LOG.trn' WITH CHECKSUM, COMPRESSION
    Msg 35250, Level 16, State 11, Server AC, Line 1
    The connection to the primary replica is not active.  The command cannot be processed.
    Msg 3013, Level 16, State 1, Server AC, Line 1
    BACKUP LOG is terminating abnormally.
    Outcome: Failed
    Duration: 00:01:00
    The other 12 databases continued to backup successfully.
    Checking the Availability Group dashboard, windows event logs, and SQL Server error logs, including Failover Cluster events showed no issues.
    However, monitoring software (Idera SQLdm) showed blocked sessions on P node.  When I ran sp_who2, it showed that a background process was being blocked by another background process with an HADR BACKUP LOCK.
    Since both processes were background processes, I was unable to kill either process.  I temporarily disabled the transaction log backup job, but the blocked process was still active.
    I ran DBCC CHECKDB (my_db) WITH all_errormsgs, no_infomsgs, data_purity on both P and AC nodes, with no errors.  However, on AC node, it also showed 1 transaction rolled forward and 0 transactions rolled back.  This also had the effect of releasing
    the blocked background process, but another background process was now blocking with the same HADR BACKUP LOCK.
    I tried to restart SQL Server Agent on AC node, which did not immediately seem to work.  However, after a few minutes, I noticed that the block had disappeared.  I re-enabled the transaction log backup job on AC and it started working normally
    again.  The error has not occurred again, but I am at a loss as to what happened, and how to prevent it from happening again.
    Any help would be greatly appreciated.
    Diane

    And here is part 2 of the stored procedure:
    --// Execute backup commands //--
    WHILE EXISTS (SELECT * FROM @tmpDatabases WHERE Selected = 1 AND Completed = 0)
    BEGIN
    SELECT TOP 1 @CurrentDBID = ID,
    @CurrentDatabaseName = DatabaseName,
    @CurrentDatabaseNameFS = DatabaseNameFS,
    @CurrentDatabaseType = DatabaseType
    FROM @tmpDatabases
    WHERE Selected = 1
    AND Completed = 0
    ORDER BY ID ASC
    SET @CurrentDatabaseID = DB_ID(@CurrentDatabaseName)
    IF DATABASEPROPERTYEX(@CurrentDatabaseName,'Status') = 'ONLINE'
    BEGIN
    IF EXISTS (SELECT * FROM sys.database_recovery_status WHERE database_id = @CurrentDatabaseID AND database_guid IS NOT NULL)
    BEGIN
    SET @CurrentIsDatabaseAccessible = 1
    END
    ELSE
    BEGIN
    SET @CurrentIsDatabaseAccessible = 0
    END
    END
    ELSE
    BEGIN
    SET @CurrentIsDatabaseAccessible = 0
    END
    SELECT @CurrentDifferentialBaseLSN = differential_base_lsn
    FROM sys.master_files
    WHERE database_id = @CurrentDatabaseID
    AND [type] = 0
    AND [file_id] = 1
    -- Workaround for a bug in SQL Server 2005
    IF @Version >= 9 AND @Version < 10
    AND EXISTS(SELECT * FROM sys.master_files WHERE database_id = @CurrentDatabaseID AND [type] = 0 AND [file_id] = 1 AND differential_base_lsn IS NOT NULL AND differential_base_guid IS NOT NULL AND differential_base_time IS NULL)
    BEGIN
    SET @CurrentDifferentialBaseLSN = NULL
    END
    SELECT @CurrentDifferentialBaseIsSnapshot = is_snapshot
    FROM msdb.dbo.backupset
    WHERE database_name = @CurrentDatabaseName
    AND [type] = 'D'
    AND checkpoint_lsn = @CurrentDifferentialBaseLSN
    IF DATABASEPROPERTYEX(@CurrentDatabaseName,'Status') = 'ONLINE'
    BEGIN
    SELECT @CurrentLogLSN = last_log_backup_lsn
    FROM sys.database_recovery_status
    WHERE database_id = @CurrentDatabaseID
    END
    SET @CurrentBackupType = @BackupType
    IF @ChangeBackupType = 'Y'
    BEGIN
    IF @CurrentBackupType = 'LOG' AND DATABASEPROPERTYEX(@CurrentDatabaseName,'Recovery') <> 'SIMPLE' AND @CurrentLogLSN IS NULL AND @CurrentDatabaseName <> 'master'
    BEGIN
    SET @CurrentBackupType = 'DIFF'
    END
    IF @CurrentBackupType = 'DIFF' AND @CurrentDifferentialBaseLSN IS NULL AND @CurrentDatabaseName <> 'master'
    BEGIN
    SET @CurrentBackupType = 'FULL'
    END
    END
    IF @CurrentBackupType = 'LOG'
    BEGIN
    SELECT @CurrentLatestBackup = MAX(backup_finish_date)
    FROM msdb.dbo.backupset
    WHERE [type] IN('D','I')
    AND is_damaged = 0
    AND database_name = @CurrentDatabaseName
    END
    IF @Version >= 11 AND @Cluster IS NOT NULL
    BEGIN
    SELECT @CurrentAvailabilityGroup = availability_groups.name,
    @CurrentAvailabilityGroupRole = dm_hadr_availability_replica_states.role_desc
    FROM sys.databases databases
    INNER JOIN sys.availability_databases_cluster availability_databases_cluster ON databases.group_database_id = availability_databases_cluster.group_database_id
    INNER JOIN sys.availability_groups availability_groups ON availability_databases_cluster.group_id = availability_groups.group_id
    INNER JOIN sys.dm_hadr_availability_replica_states dm_hadr_availability_replica_states ON availability_groups.group_id = dm_hadr_availability_replica_states.group_id AND databases.replica_id = dm_hadr_availability_replica_states.replica_id
    WHERE databases.name = @CurrentDatabaseName
    END
    IF @Version >= 11 AND @Cluster IS NOT NULL AND @CurrentAvailabilityGroup IS NOT NULL
    BEGIN
    SELECT @CurrentIsPreferredBackupReplica = sys.fn_hadr_backup_is_preferred_replica(@CurrentDatabaseName)
    END
    SELECT @CurrentDatabaseMirroringRole = UPPER(mirroring_role_desc)
    FROM sys.database_mirroring
    WHERE database_id = @CurrentDatabaseID
    IF EXISTS (SELECT * FROM msdb.dbo.log_shipping_primary_databases WHERE primary_database = @CurrentDatabaseName)
    BEGIN
    SET @CurrentLogShippingRole = 'PRIMARY'
    END
    ELSE
    IF EXISTS (SELECT * FROM msdb.dbo.log_shipping_secondary_databases WHERE secondary_database = @CurrentDatabaseName)
    BEGIN
    SET @CurrentLogShippingRole = 'SECONDARY'
    END
    -- Set database message
    SET @DatabaseMessage = 'Date and time: ' + CONVERT(nvarchar,GETDATE(),120) + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Database: ' + QUOTENAME(@CurrentDatabaseName) + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Status: ' + CAST(DATABASEPROPERTYEX(@CurrentDatabaseName,'Status') AS nvarchar) + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Standby: ' + CASE WHEN DATABASEPROPERTYEX(@CurrentDatabaseName,'IsInStandBy') = 1 THEN 'Yes' ELSE 'No' END + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Updateability: ' + CAST(DATABASEPROPERTYEX(@CurrentDatabaseName,'Updateability') AS nvarchar) + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'User access: ' + CAST(DATABASEPROPERTYEX(@CurrentDatabaseName,'UserAccess') AS nvarchar) + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Is accessible: ' + CASE WHEN @CurrentIsDatabaseAccessible = 1 THEN 'Yes' ELSE 'No' END + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Recovery model: ' + CAST(DATABASEPROPERTYEX(@CurrentDatabaseName,'Recovery') AS nvarchar) + CHAR(13) + CHAR(10)
    IF @CurrentAvailabilityGroup IS NOT NULL SET @DatabaseMessage = @DatabaseMessage + 'Availability group: ' + @CurrentAvailabilityGroup + CHAR(13) + CHAR(10)
    IF @CurrentAvailabilityGroup IS NOT NULL SET @DatabaseMessage = @DatabaseMessage + 'Availability group role: ' + @CurrentAvailabilityGroupRole + CHAR(13) + CHAR(10)
    IF @CurrentAvailabilityGroup IS NOT NULL SET @DatabaseMessage = @DatabaseMessage + 'Is preferred backup replica: ' + CASE WHEN @CurrentIsPreferredBackupReplica = 1 THEN 'Yes' WHEN @CurrentIsPreferredBackupReplica = 0 THEN 'No' ELSE 'N/A' END + CHAR(13) + CHAR(10)
    IF @CurrentDatabaseMirroringRole IS NOT NULL SET @DatabaseMessage = @DatabaseMessage + 'Database mirroring role: ' + @CurrentDatabaseMirroringRole + CHAR(13) + CHAR(10)
    IF @CurrentLogShippingRole IS NOT NULL SET @DatabaseMessage = @DatabaseMessage + 'Log shipping role: ' + @CurrentLogShippingRole + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Differential base LSN: ' + ISNULL(CAST(@CurrentDifferentialBaseLSN AS nvarchar),'N/A') + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Differential base is snapshot: ' + CASE WHEN @CurrentDifferentialBaseIsSnapshot = 1 THEN 'Yes' WHEN @CurrentDifferentialBaseIsSnapshot = 0 THEN 'No' ELSE 'N/A' END + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = @DatabaseMessage + 'Last log backup LSN: ' + ISNULL(CAST(@CurrentLogLSN AS nvarchar),'N/A') + CHAR(13) + CHAR(10)
    SET @DatabaseMessage = REPLACE(@DatabaseMessage,'%','%%') + ' '
    RAISERROR(@DatabaseMessage,10,1) WITH NOWAIT
    IF DATABASEPROPERTYEX(@CurrentDatabaseName,'Status') = 'ONLINE'
    AND NOT (DATABASEPROPERTYEX(@CurrentDatabaseName,'UserAccess') = 'SINGLE_USER' AND @CurrentIsDatabaseAccessible = 0)
    AND DATABASEPROPERTYEX(@CurrentDatabaseName,'IsInStandBy') = 0
    AND NOT (@CurrentBackupType = 'LOG' AND (DATABASEPROPERTYEX(@CurrentDatabaseName,'Recovery') = 'SIMPLE' OR @CurrentLogLSN IS NULL))
    AND NOT (@CurrentBackupType = 'DIFF' AND @CurrentDifferentialBaseLSN IS NULL)
    AND NOT (@CurrentBackupType IN('DIFF','LOG') AND @CurrentDatabaseName = 'master')
    AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'FULL' AND @CopyOnly = 'N' AND (@CurrentAvailabilityGroupRole <> 'PRIMARY' OR @CurrentAvailabilityGroupRole IS NULL))
    AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'FULL' AND @CopyOnly = 'Y' AND (@CurrentIsPreferredBackupReplica <> 1 OR @CurrentIsPreferredBackupReplica IS NULL))
    AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'DIFF' AND (@CurrentAvailabilityGroupRole <> 'PRIMARY' OR @CurrentAvailabilityGroupRole IS NULL))
    AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'LOG' AND @CopyOnly = 'N' AND (@CurrentIsPreferredBackupReplica <> 1 OR @CurrentIsPreferredBackupReplica IS NULL))
    AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'LOG' AND @CopyOnly = 'Y' AND (@CurrentAvailabilityGroupRole <> 'PRIMARY' OR @CurrentAvailabilityGroupRole IS NULL))
    AND NOT ((@CurrentLogShippingRole = 'PRIMARY' AND @CurrentLogShippingRole IS NOT NULL) AND @CurrentBackupType = 'LOG')
    BEGIN
    -- Set variables
    SET @CurrentDate = GETDATE()
    IF @CleanupTime IS NULL OR (@CurrentBackupType = 'LOG' AND @CurrentLatestBackup IS NULL) OR @CurrentBackupType <> @BackupType
    BEGIN
    SET @CurrentCleanupDate = NULL
    END
    ELSE
    IF @CurrentBackupType = 'LOG'
    BEGIN
    SET @CurrentCleanupDate = (SELECT MIN([Date]) FROM(SELECT DATEADD(hh,-(@CleanupTime),@CurrentDate) AS [Date] UNION SELECT @CurrentLatestBackup AS [Date]) Dates)
    END
    ELSE
    BEGIN
    SET @CurrentCleanupDate = DATEADD(hh,-(@CleanupTime),@CurrentDate)
    END
    SELECT @CurrentFileExtension = CASE
    WHEN @BackupSoftware IS NULL AND @CurrentBackupType = 'FULL' THEN 'bak'
    WHEN @BackupSoftware IS NULL AND @CurrentBackupType = 'DIFF' THEN 'bak'
    WHEN @BackupSoftware IS NULL AND @CurrentBackupType = 'LOG' THEN 'trn'
    WHEN @BackupSoftware = 'LITESPEED' AND @CurrentBackupType = 'FULL' THEN 'bak'
    WHEN @BackupSoftware = 'LITESPEED' AND @CurrentBackupType = 'DIFF' THEN 'bak'
    WHEN @BackupSoftware = 'LITESPEED' AND @CurrentBackupType = 'LOG' THEN 'trn'
    WHEN @BackupSoftware = 'SQLBACKUP' AND @CurrentBackupType = 'FULL' THEN 'sqb'
    WHEN @BackupSoftware = 'SQLBACKUP' AND @CurrentBackupType = 'DIFF' THEN 'sqb'
    WHEN @BackupSoftware = 'SQLBACKUP' AND @CurrentBackupType = 'LOG' THEN 'sqb'
    WHEN @BackupSoftware = 'HYPERBAC' AND @CurrentBackupType = 'FULL' AND @Encrypt = 'N' THEN 'hbc'
    WHEN @BackupSoftware = 'HYPERBAC' AND @CurrentBackupType = 'DIFF' AND @Encrypt = 'N' THEN 'hbc'
    WHEN @BackupSoftware = 'HYPERBAC' AND @CurrentBackupType = 'LOG' AND @Encrypt = 'N' THEN 'hbc'
    WHEN @BackupSoftware = 'HYPERBAC' AND @CurrentBackupType = 'FULL' AND @Encrypt = 'Y' THEN 'hbe'
    WHEN @BackupSoftware = 'HYPERBAC' AND @CurrentBackupType = 'DIFF' AND @Encrypt = 'Y' THEN 'hbe'
    WHEN @BackupSoftware = 'HYPERBAC' AND @CurrentBackupType = 'LOG' AND @Encrypt = 'Y' THEN 'hbe'
    WHEN @BackupSoftware = 'SQLSAFE' AND @CurrentBackupType = 'FULL' THEN 'safe'
    WHEN @BackupSoftware = 'SQLSAFE' AND @CurrentBackupType = 'DIFF' THEN 'safe'
    WHEN @BackupSoftware = 'SQLSAFE' AND @CurrentBackupType = 'LOG' THEN 'safe'
    END
    INSERT INTO @CurrentDirectories (ID, DirectoryPath, CreateCompleted, CleanupCompleted)
    SELECT ROW_NUMBER() OVER (ORDER BY ID), DirectoryPath + CASE WHEN RIGHT(DirectoryPath,1) = '\' THEN '' ELSE '\' END + CASE WHEN @CurrentBackupType = 'LOG' THEN '\Transaction Dumps\' + @CurrentDatabaseNameFS ELSE '' END, 0, 0
    FROM @Directories
    ORDER BY ID ASC
    SET @CurrentFileNumber = 0
    SET @CurrentMirrorFilePath = NULL
    WHILE @CurrentFileNumber < @NumberOfFiles
    BEGIN
    SET @CurrentFileNumber = @CurrentFileNumber + 1
    SELECT @CurrentDirectoryPath = DirectoryPath
    FROM @CurrentDirectories
    WHERE @CurrentFileNumber >= (ID - 1) * (SELECT @NumberOfFiles / COUNT(*) FROM @CurrentDirectories) + 1
    AND @CurrentFileNumber <= ID * (SELECT @NumberOfFiles / COUNT(*) FROM @CurrentDirectories)
    SET @CurrentFilePath = @CurrentDirectoryPath + '\' + CASE WHEN @CurrentAvailabilityGroup IS NOT NULL THEN @Cluster + '$' + @CurrentAvailabilityGroup ELSE REPLACE(CAST(SERVERPROPERTY('servername') AS nvarchar),'\','$') END + '_' + @CurrentDatabaseNameFS + '_' + REPLACE(REPLACE(REPLACE((CONVERT(nvarchar,@CurrentDate,120)),'-',''),' ','_'),':','') + CASE WHEN @NumberOfFiles > 1 AND @NumberOfFiles <= 9 THEN '_' + CAST(@CurrentFileNumber AS nvarchar) WHEN @NumberOfFiles >= 10 THEN '_' + RIGHT('0' + CAST(@CurrentFileNumber AS nvarchar),2) ELSE '' END + '_' + @CurrentDatabaseType + '_' + UPPER(@CurrentBackupType) + CASE WHEN @ReadWriteFileGroups = 'Y' THEN '_PARTIAL' ELSE '' END + CASE WHEN @CopyOnly = 'Y' THEN '_COPY_ONLY' ELSE '' END + '.' + @CurrentFileExtension
    IF LEN(@CurrentFilePath) > 257
    BEGIN
    SET @CurrentFilePath = @CurrentDirectoryPath + '\' + CASE WHEN @CurrentAvailabilityGroup IS NOT NULL THEN @Cluster + '$' + @CurrentAvailabilityGroup ELSE REPLACE(CAST(SERVERPROPERTY('servername') AS nvarchar),'\','$') END + '_' + LEFT(@CurrentDatabaseNameFS,CASE WHEN (LEN(@CurrentDatabaseNameFS) + 257 - LEN(@CurrentFilePath) - 3) < 20 THEN 20 ELSE (LEN(@CurrentDatabaseNameFS) + 257 - LEN(@CurrentFilePath) - 3) END) + '...' + '_' + REPLACE(REPLACE(REPLACE((CONVERT(nvarchar,@CurrentDate,120)),'-',''),' ','_'),':','') + CASE WHEN @NumberOfFiles > 1 AND @NumberOfFiles <= 9 THEN '_' + CAST(@CurrentFileNumber AS nvarchar) WHEN @NumberOfFiles >= 10 THEN '_' + RIGHT('0' + CAST(@CurrentFileNumber AS nvarchar),2) ELSE '' END + '_' + @CurrentDatabaseType + '_' + UPPER(@CurrentBackupType) + CASE WHEN @ReadWriteFileGroups = 'Y' THEN '_PARTIAL' ELSE '' END + CASE WHEN @CopyOnly = 'Y' THEN '_COPY_ONLY' ELSE '' END + '.' + @CurrentFileExtension
    END
    IF @CurrentFileNumber = 1 AND LEN(@MirrorDirectory) > 0
    BEGIN
    SET @CurrentMirrorFilePath = @MirrorDirectory + CASE WHEN RIGHT(@MirrorDirectory,1) = '\' THEN '' ELSE '\' END + CASE WHEN @CurrentBackupType = 'LOG' THEN '\Transaction Dumps\' + @CurrentDatabaseNameFS ELSE '' END + '\' + CASE WHEN @CurrentAvailabilityGroup IS NOT NULL THEN @Cluster + '$' + @CurrentAvailabilityGroup ELSE REPLACE(CAST(SERVERPROPERTY('servername') AS nvarchar),'\','$') END + '_' + @CurrentDatabaseNameFS + '_' + REPLACE(REPLACE(REPLACE((CONVERT(nvarchar,@CurrentDate,120)),'-',''),' ','_'),':','') + CASE WHEN @NumberOfFiles > 1 AND @NumberOfFiles <= 9 THEN '_' + CAST(@CurrentFileNumber AS nvarchar) WHEN @NumberOfFiles >= 10 THEN '_' + RIGHT('0' + CAST(@CurrentFileNumber AS nvarchar),2) ELSE '' END + '_' + @CurrentDatabaseType + '_' + UPPER(@CurrentBackupType) + CASE WHEN @ReadWriteFileGroups = 'Y' THEN '_PARTIAL' ELSE '' END + CASE WHEN @CopyOnly = 'Y' THEN '_COPY_ONLY' ELSE '' END + '.' + @CurrentFileExtension
    IF LEN(@CurrentFilePath) > 257
    BEGIN
    SET @CurrentMirrorFilePath = @MirrorDirectory + CASE WHEN RIGHT(@MirrorDirectory,1) = '\' THEN '' ELSE '\' END + CASE WHEN @CurrentBackupType = 'LOG' THEN '\Transaction Dumps\' + @CurrentDatabaseNameFS ELSE '' END + '\' + CASE WHEN @CurrentAvailabilityGroup IS NOT NULL THEN @Cluster + '$' + @CurrentAvailabilityGroup ELSE REPLACE(CAST(SERVERPROPERTY('servername') AS nvarchar),'\','$') END + '_' + LEFT(@CurrentDatabaseNameFS,CASE WHEN (LEN(@CurrentDatabaseNameFS) + 257 - LEN(@CurrentFilePath) - 3) < 20 THEN 20 ELSE (LEN(@CurrentDatabaseNameFS) + 257 - LEN(@CurrentFilePath) - 3) END) + '...' + '_' + REPLACE(REPLACE(REPLACE((CONVERT(nvarchar,@CurrentDate,120)),'-',''),' ','_'),':','') + CASE WHEN @NumberOfFiles > 1 AND @NumberOfFiles <= 9 THEN '_' + CAST(@CurrentFileNumber AS nvarchar) WHEN @NumberOfFiles >= 10 THEN '_' + RIGHT('0' + CAST(@CurrentFileNumber AS nvarchar),2) ELSE '' END + '_' + @CurrentDatabaseType + '_' + UPPER(@CurrentBackupType) + CASE WHEN @ReadWriteFileGroups = 'Y' THEN '_PARTIAL' ELSE '' END + CASE WHEN @CopyOnly = 'Y' THEN '_COPY_ONLY' ELSE '' END + '.' + @CurrentFileExtension
    END
    END
    INSERT INTO @CurrentFiles (CurrentFilePath)
    SELECT @CurrentFilePath
    SET @CurrentDirectoryPath = NULL
    SET @CurrentFilePath = NULL
    END
    -- Create directory
    WHILE EXISTS (SELECT * FROM @CurrentDirectories WHERE CreateCompleted = 0)
    BEGIN
    SELECT TOP 1 @CurrentDirectoryID = ID,
    @CurrentDirectoryPath = DirectoryPath
    FROM @CurrentDirectories
    WHERE CreateCompleted = 0
    ORDER BY ID ASC
    SET @CurrentCommandType01 = 'xp_create_subdir'
    SET @CurrentCommand01 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_create_subdir N''' + REPLACE(@CurrentDirectoryPath,'''','''''') + ''' IF @ReturnCode <> 0 RAISERROR(''Error creating directory.'', 16, 1)'
    EXECUTE @CurrentCommandOutput01 = [dbo].[CommandExecute] @Command = @CurrentCommand01, @CommandType = @CurrentCommandType01, @Mode = 1, @DatabaseName = @CurrentDatabaseName, @LogToTable = @LogToTable, @Execute = @Execute
    SET @Error = @@ERROR
    IF @Error <> 0 SET @CurrentCommandOutput01 = @Error
    IF @CurrentCommandOutput01 <> 0 SET @ReturnCode = @CurrentCommandOutput01
    UPDATE @CurrentDirectories
    SET CreateCompleted = 1,
    CreateOutput = @CurrentCommandOutput01
    WHERE ID = @CurrentDirectoryID
    SET @CurrentDirectoryID = NULL
    SET @CurrentDirectoryPath = NULL
    SET @CurrentCommand01 = NULL
    SET @CurrentCommandOutput01 = NULL
    SET @CurrentCommandType01 = NULL
    END
    -- Perform a backup
    IF NOT EXISTS (SELECT * FROM @CurrentDirectories WHERE CreateOutput <> 0 OR CreateOutput IS NULL)
    BEGIN
    IF @BackupSoftware IS NULL
    BEGIN
    SELECT @CurrentCommandType02 = CASE
    WHEN @CurrentBackupType IN('DIFF','FULL') THEN 'BACKUP_DATABASE'
    WHEN @CurrentBackupType = 'LOG' THEN 'BACKUP_LOG'
    END
    SELECT @CurrentCommand02 = CASE
    WHEN @CurrentBackupType IN('DIFF','FULL') THEN 'BACKUP DATABASE ' + QUOTENAME(@CurrentDatabaseName)
    WHEN @CurrentBackupType = 'LOG' THEN 'BACKUP LOG ' + QUOTENAME(@CurrentDatabaseName)
    END
    IF @ReadWriteFileGroups = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ' READ_WRITE_FILEGROUPS'
    SET @CurrentCommand02 = @CurrentCommand02 + ' TO'
    SELECT @CurrentCommand02 = @CurrentCommand02 + ' DISK = N''' + REPLACE(CurrentFilePath,'''','''''') + '''' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) <> @NumberOfFiles THEN ',' ELSE '' END
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand02 = @CurrentCommand02 + ' WITH '
    IF @CheckSum = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand02 = @CurrentCommand02 + 'NO_CHECKSUM'
    IF @Compress = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ', COMPRESSION'
    IF @Compress = 'N' AND @Version >= 10 SET @CurrentCommand02 = @CurrentCommand02 + ', NO_COMPRESSION'
    IF @CurrentBackupType = 'DIFF' SET @CurrentCommand02 = @CurrentCommand02 + ', DIFFERENTIAL'
    IF @CopyOnly = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ', COPY_ONLY'
    IF @BlockSize IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', BLOCKSIZE = ' + CAST(@BlockSize AS nvarchar)
    IF @BufferCount IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', BUFFERCOUNT = ' + CAST(@BufferCount AS nvarchar)
    IF @MaxTransferSize IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', MAXTRANSFERSIZE = ' + CAST(@MaxTransferSize AS nvarchar)
    IF @Description IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', DESCRIPTION = N''' + REPLACE(@Description,'''','''''') + ''''
    END
    IF @BackupSoftware = 'LITESPEED'
    BEGIN
    SELECT @CurrentCommandType02 = CASE
    WHEN @CurrentBackupType IN('DIFF','FULL') THEN 'xp_backup_database'
    WHEN @CurrentBackupType = 'LOG' THEN 'xp_backup_log'
    END
    SELECT @CurrentCommand02 = CASE
    WHEN @CurrentBackupType IN('DIFF','FULL') THEN 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_backup_database @database = N''' + REPLACE(@CurrentDatabaseName,'''','''''') + ''''
    WHEN @CurrentBackupType = 'LOG' THEN 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_backup_log @database = N''' + REPLACE(@CurrentDatabaseName,'''','''''') + ''''
    END
    SELECT @CurrentCommand02 = @CurrentCommand02 + ', @filename = N''' + REPLACE(CurrentFilePath,'''','''''') + ''''
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand02 = @CurrentCommand02 + ', @with = '''
    IF @CheckSum = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand02 = @CurrentCommand02 + 'NO_CHECKSUM'
    IF @CurrentBackupType = 'DIFF' SET @CurrentCommand02 = @CurrentCommand02 + ', DIFFERENTIAL'
    IF @CopyOnly = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ', COPY_ONLY'
    IF @BlockSize IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', BLOCKSIZE = ' + CAST(@BlockSize AS nvarchar)
    SET @CurrentCommand02 = @CurrentCommand02 + ''''
    IF @ReadWriteFileGroups = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ', @read_write_filegroups = 1'
    IF @CompressionLevel IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @compressionlevel = ' + CAST(@CompressionLevel AS nvarchar)
    IF @BufferCount IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @buffercount = ' + CAST(@BufferCount AS nvarchar)
    IF @MaxTransferSize IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @maxtransfersize = ' + CAST(@MaxTransferSize AS nvarchar)
    IF @Threads IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @threads = ' + CAST(@Threads AS nvarchar)
    IF @Throttle IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @throttle = ' + CAST(@Throttle AS nvarchar)
    IF @Description IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @desc = N''' + REPLACE(@Description,'''','''''') + ''''
    IF @EncryptionType IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @cryptlevel = ' + CASE
    WHEN @EncryptionType = 'RC2-40' THEN '0'
    WHEN @EncryptionType = 'RC2-56' THEN '1'
    WHEN @EncryptionType = 'RC2-112' THEN '2'
    WHEN @EncryptionType = 'RC2-128' THEN '3'
    WHEN @EncryptionType = '3DES-168' THEN '4'
    WHEN @EncryptionType = 'RC4-128' THEN '5'
    WHEN @EncryptionType = 'AES-128' THEN '6'
    WHEN @EncryptionType = 'AES-192' THEN '7'
    WHEN @EncryptionType = 'AES-256' THEN '8'
    END
    IF @EncryptionKey IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @encryptionkey = N''' + REPLACE(@EncryptionKey,'''','''''') + ''''
    SET @CurrentCommand02 = @CurrentCommand02 + ' IF @ReturnCode <> 0 RAISERROR(''Error performing LiteSpeed backup.'', 16, 1)'
    END
    IF @BackupSoftware = 'SQLBACKUP'
    BEGIN
    SET @CurrentCommandType02 = 'sqlbackup'
    SELECT @CurrentCommand02 = CASE
    WHEN @CurrentBackupType IN('DIFF','FULL') THEN 'BACKUP DATABASE ' + QUOTENAME(@CurrentDatabaseName)
    WHEN @CurrentBackupType = 'LOG' THEN 'BACKUP LOG ' + QUOTENAME(@CurrentDatabaseName)
    END
    IF @ReadWriteFileGroups = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ' READ_WRITE_FILEGROUPS'
    SET @CurrentCommand02 = @CurrentCommand02 + ' TO'
    SELECT @CurrentCommand02 = @CurrentCommand02 + ' DISK = N''' + REPLACE(CurrentFilePath,'''','''''') + '''' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) <> @NumberOfFiles THEN ',' ELSE '' END
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand02 = @CurrentCommand02 + ' WITH '
    IF @CheckSum = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand02 = @CurrentCommand02 + 'NO_CHECKSUM'
    IF @CurrentBackupType = 'DIFF' SET @CurrentCommand02 = @CurrentCommand02 + ', DIFFERENTIAL'
    IF @CopyOnly = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ', COPY_ONLY'
    IF @CompressionLevel IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', COMPRESSION = ' + CAST(@CompressionLevel AS nvarchar)
    IF @Threads IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', THREADCOUNT = ' + CAST(@Threads AS nvarchar)
    IF @MaxTransferSize IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', MAXTRANSFERSIZE = ' + CAST(@MaxTransferSize AS nvarchar)
    IF @Description IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', DESCRIPTION = N''' + REPLACE(@Description,'''','''''') + ''''
    IF @EncryptionType IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', KEYSIZE = ' + CASE
    WHEN @EncryptionType = 'AES-128' THEN '128'
    WHEN @EncryptionType = 'AES-256' THEN '256'
    END
    IF @EncryptionKey IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', PASSWORD = N''' + REPLACE(@EncryptionKey,'''','''''') + ''''
    SET @CurrentCommand02 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.sqlbackup N''-SQL "' + REPLACE(@CurrentCommand02,'''','''''') + '"''' + ' IF @ReturnCode <> 0 RAISERROR(''Error performing SQLBackup backup.'', 16, 1)'
    END
    IF @BackupSoftware = 'HYPERBAC'
    BEGIN
    SET @CurrentCommandType02 = 'BACKUP_DATABASE'
    SELECT @CurrentCommand02 = CASE
    WHEN @CurrentBackupType IN('DIFF','FULL') THEN 'BACKUP DATABASE ' + QUOTENAME(@CurrentDatabaseName)
    WHEN @CurrentBackupType = 'LOG' THEN 'BACKUP LOG ' + QUOTENAME(@CurrentDatabaseName)
    END
    IF @ReadWriteFileGroups = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ' READ_WRITE_FILEGROUPS'
    SET @CurrentCommand02 = @CurrentCommand02 + ' TO'
    SELECT @CurrentCommand02 = @CurrentCommand02 + ' DISK = N''' + REPLACE(CurrentFilePath,'''','''''') + '''' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) <> @NumberOfFiles THEN ',' ELSE '' END
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand02 = @CurrentCommand02 + ' WITH '
    IF @CheckSum = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand02 = @CurrentCommand02 + 'NO_CHECKSUM'
    IF @CurrentBackupType = 'DIFF' SET @CurrentCommand02 = @CurrentCommand02 + ', DIFFERENTIAL'
    IF @CopyOnly = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ', COPY_ONLY'
    IF @BlockSize IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', BLOCKSIZE = ' + CAST(@BlockSize AS nvarchar)
    IF @BufferCount IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', BUFFERCOUNT = ' + CAST(@BufferCount AS nvarchar)
    IF @MaxTransferSize IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', MAXTRANSFERSIZE = ' + CAST(@MaxTransferSize AS nvarchar)
    IF @Description IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', DESCRIPTION = N''' + REPLACE(@Description,'''','''''') + ''''
    END
    IF @BackupSoftware = 'SQLSAFE'
    BEGIN
    SET @CurrentCommandType02 = 'xp_ss_backup'
    SET @CurrentCommand02 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_ss_backup @database = N''' + REPLACE(@CurrentDatabaseName,'''','''''') + ''''
    SELECT @CurrentCommand02 = @CurrentCommand02 + ', ' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) = 1 THEN '@filename' ELSE '@backupfile' END + ' = N''' + REPLACE(CurrentFilePath,'''','''''') + ''''
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    IF @CurrentMirrorFilePath IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @mirrorfile = N''' + @CurrentMirrorFilePath + ''''
    SET @CurrentCommand02 = @CurrentCommand02 + ', @backuptype = ' + CASE WHEN @CurrentBackupType = 'FULL' THEN '''Full''' WHEN @CurrentBackupType = 'DIFF' THEN '''Differential''' WHEN @CurrentBackupType = 'LOG' THEN '''Log''' END
    IF @ReadWriteFileGroups = 'Y' SET @CurrentCommand02 = @CurrentCommand02 + ', @readwritefilegroups = 1'
    SET @CurrentCommand02 = @CurrentCommand02 + ', @checksum = ' + CASE WHEN @CheckSum = 'Y' THEN '1' WHEN @CheckSum = 'N' THEN '0' END
    SET @CurrentCommand02 = @CurrentCommand02 + ', @copyonly = ' + CASE WHEN @CopyOnly = 'Y' THEN '1' WHEN @CopyOnly = 'N' THEN '0' END
    IF @CompressionLevel IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @compressionlevel = ' + CASE WHEN @CompressionLevel = 5 THEN N'ispeed' WHEN @CompressionLevel = 6 THEN N'isize' ELSE CAST(@CompressionLevel AS nvarchar) END
    IF @RetryWrites IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @retrywrites = N''' + @RetryWrites + ''''
    IF @Threads IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @threads = ' + CAST(@Threads AS nvarchar)
    IF @Description IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @desc = N''' + REPLACE(@Description,'''','''''') + ''''
    IF @EncryptionType IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @encryptiontype = N''' + CASE
    WHEN @EncryptionType = 'AES-128' THEN 'AES128'
    WHEN @EncryptionType = 'AES-256' THEN 'AES256'
    END + ''''
    IF @EncryptionKey IS NOT NULL SET @CurrentCommand02 = @CurrentCommand02 + ', @encryptedbackuppassword = N''' + REPLACE(@EncryptionKey,'''','''''') + ''''
    SET @CurrentCommand02 = @CurrentCommand02 + ' IF @ReturnCode <> 0 RAISERROR(''Error performing SQLsafe backup.'', 16, 1)'
    END
    EXECUTE @CurrentCommandOutput02 = [dbo].[CommandExecute] @Command = @CurrentCommand02, @CommandType = @CurrentCommandType02, @Mode = 1, @DatabaseName = @CurrentDatabaseName, @LogToTable = @LogToTable, @Execute = @Execute
    SET @Error = @@ERROR
    IF @Error <> 0 SET @CurrentCommandOutput02 = @Error
    IF @CurrentCommandOutput02 <> 0 SET @ReturnCode = @CurrentCommandOutput02
    END
    -- Verify the backup
    IF @CurrentCommandOutput02 = 0 AND @Verify = 'Y'
    BEGIN
    IF @BackupSoftware IS NULL
    BEGIN
    SET @CurrentCommandType03 = 'RESTORE_VERIFYONLY'
    SET @CurrentCommand03 = 'RESTORE VERIFYONLY FROM'
    SELECT @CurrentCommand03 = @CurrentCommand03 + ' DISK = N''' + REPLACE(CurrentFilePath,'''','''''') + '''' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) <> @NumberOfFiles THEN ',' ELSE '' END
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand03 = @CurrentCommand03 + ' WITH '
    IF @CheckSum = 'Y' SET @CurrentCommand03 = @CurrentCommand03 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand03 = @CurrentCommand03 + 'NO_CHECKSUM'
    END
    IF @BackupSoftware = 'LITESPEED'
    BEGIN
    SET @CurrentCommandType03 = 'xp_restore_verifyonly'
    SET @CurrentCommand03 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_restore_verifyonly'
    SELECT @CurrentCommand03 = @CurrentCommand03 + ' @filename = N''' + REPLACE(CurrentFilePath,'''','''''') + '''' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) <> @NumberOfFiles THEN ',' ELSE '' END
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand03 = @CurrentCommand03 + ', @with = '''
    IF @CheckSum = 'Y' SET @CurrentCommand03 = @CurrentCommand03 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand03 = @CurrentCommand03 + 'NO_CHECKSUM'
    SET @CurrentCommand03 = @CurrentCommand03 + ''''
    IF @EncryptionKey IS NOT NULL SET @CurrentCommand03 = @CurrentCommand03 + ', @encryptionkey = N''' + REPLACE(@EncryptionKey,'''','''''') + ''''
    SET @CurrentCommand03 = @CurrentCommand03 + ' IF @ReturnCode <> 0 RAISERROR(''Error verifying LiteSpeed backup.'', 16, 1)'
    END
    IF @BackupSoftware = 'SQLBACKUP'
    BEGIN
    SET @CurrentCommandType03 = 'sqlbackup'
    SET @CurrentCommand03 = 'RESTORE VERIFYONLY FROM'
    SELECT @CurrentCommand03 = @CurrentCommand03 + ' DISK = N''' + REPLACE(CurrentFilePath,'''','''''') + '''' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) <> @NumberOfFiles THEN ',' ELSE '' END
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand03 = @CurrentCommand03 + ' WITH '
    IF @CheckSum = 'Y' SET @CurrentCommand03 = @CurrentCommand03 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand03 = @CurrentCommand03 + 'NO_CHECKSUM'
    IF @EncryptionKey IS NOT NULL SET @CurrentCommand03 = @CurrentCommand03 + ', PASSWORD = N''' + REPLACE(@EncryptionKey,'''','''''') + ''''
    SET @CurrentCommand03 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.sqlbackup N''-SQL "' + REPLACE(@CurrentCommand03,'''','''''') + '"''' + ' IF @ReturnCode <> 0 RAISERROR(''Error verifying SQLBackup backup.'', 16, 1)'
    END
    IF @BackupSoftware = 'HYPERBAC'
    BEGIN
    SET @CurrentCommandType03 = 'RESTORE_VERIFYONLY'
    SET @CurrentCommand03 = 'RESTORE VERIFYONLY FROM'
    SELECT @CurrentCommand03 = @CurrentCommand03 + ' DISK = N''' + REPLACE(CurrentFilePath,'''','''''') + '''' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) <> @NumberOfFiles THEN ',' ELSE '' END
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand03 = @CurrentCommand03 + ' WITH '
    IF @CheckSum = 'Y' SET @CurrentCommand03 = @CurrentCommand03 + 'CHECKSUM'
    IF @CheckSum = 'N' SET @CurrentCommand03 = @CurrentCommand03 + 'NO_CHECKSUM'
    END
    IF @BackupSoftware = 'SQLSAFE'
    BEGIN
    SET @CurrentCommandType03 = 'xp_ss_verify'
    SET @CurrentCommand03 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_ss_verify @database = N''' + REPLACE(@CurrentDatabaseName,'''','''''') + ''''
    SELECT @CurrentCommand03 = @CurrentCommand03 + ', ' + CASE WHEN ROW_NUMBER() OVER (ORDER BY CurrentFilePath ASC) = 1 THEN '@filename' ELSE '@backupfile' END + ' = N''' + REPLACE(CurrentFilePath,'''','''''') + ''''
    FROM @CurrentFiles
    ORDER BY CurrentFilePath ASC
    SET @CurrentCommand03 = @CurrentCommand03 + ' IF @ReturnCode <> 0 RAISERROR(''Error verifying SQLsafe backup.'', 16, 1)'
    END
    EXECUTE @CurrentCommandOutput03 = [dbo].[CommandExecute] @Command = @CurrentCommand03, @CommandType = @CurrentCommandType03, @Mode = 1, @DatabaseName = @CurrentDatabaseName, @LogToTable = @LogToTable, @Execute = @Execute
    SET @Error = @@ERROR
    IF @Error <> 0 SET @CurrentCommandOutput03 = @Error
    IF @CurrentCommandOutput03 <> 0 SET @ReturnCode = @CurrentCommandOutput03
    END
    -- Delete old backup files
    IF (@CurrentCommandOutput02 = 0 AND @Verify = 'N' AND @CurrentCleanupDate IS NOT NULL)
    OR (@CurrentCommandOutput02 = 0 AND @Verify = 'Y' AND @CurrentCommandOutput03 = 0 AND @CurrentCleanupDate IS NOT NULL)
    BEGIN
    WHILE EXISTS (SELECT * FROM @CurrentDirectories WHERE CleanupCompleted = 0)
    BEGIN
    SELECT TOP 1 @CurrentDirectoryID = ID,
    @CurrentDirectoryPath = DirectoryPath
    FROM @CurrentDirectories
    WHERE CleanupCompleted = 0
    ORDER BY ID ASC
    IF @BackupSoftware IS NULL
    BEGIN
    SET @CurrentCommandType04 = 'xp_delete_file'
    SET @CurrentCommand04 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_delete_file 0, N''' + REPLACE(@CurrentDirectoryPath,'''','''''') + ''', ''' + @CurrentFileExtension + ''', ''' + CONVERT(nvarchar(19),@CurrentCleanupDate,126) + ''' IF @ReturnCode <> 0 RAISERROR(''Error deleting files.'', 16, 1)'
    END
    IF @BackupSoftware = 'LITESPEED'
    BEGIN
    SET @CurrentCommandType04 = 'xp_slssqlmaint'
    SET @CurrentCommand04 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_slssqlmaint N''-MAINTDEL -DELFOLDER "' + REPLACE(@CurrentDirectoryPath,'''','''''') + '" -DELEXTENSION "' + @CurrentFileExtension + '" -DELUNIT "' + CAST(DATEDIFF(mi,@CurrentCleanupDate,GETDATE()) + 1 AS nvarchar) + '" -DELUNITTYPE "minutes" -DELUSEAGE'' IF @ReturnCode <> 0 RAISERROR(''Error deleting LiteSpeed backup files.'', 16, 1)'
    END
    IF @BackupSoftware = 'SQLBACKUP'
    BEGIN
    SET @CurrentCommandType04 = 'sqbutility'
    SET @CurrentCommand04 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.sqbutility 1032, N''' + REPLACE(@CurrentDatabaseName,'''','''''') + ''', N''' + REPLACE(@CurrentDirectoryPath,'''','''''') + ''', ''' + CASE WHEN @CurrentBackupType = 'FULL' THEN 'D' WHEN @CurrentBackupType = 'DIFF' THEN 'I' WHEN @CurrentBackupType = 'LOG' THEN 'L' END + ''', ''' + CAST(DATEDIFF(hh,@CurrentCleanupDate,GETDATE()) + 1 AS nvarchar) + 'h'', ' + ISNULL('''' + REPLACE(@EncryptionKey,'''','''''') + '''','NULL') + ' IF @ReturnCode <> 0 RAISERROR(''Error deleting SQLBackup backup files.'', 16, 1)'
    END
    IF @BackupSoftware = 'HYPERBAC'
    BEGIN
    SET @CurrentCommandType04 = 'xp_delete_file'
    SET @CurrentCommand04 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_delete_file 0, N''' + REPLACE(@CurrentDirectoryPath,'''','''''') + ''', ''' + @CurrentFileExtension + ''', ''' + CONVERT(nvarchar(19),@CurrentCleanupDate,126) + ''' IF @ReturnCode <> 0 RAISERROR(''Error deleting files.'', 16, 1)'
    END
    IF @BackupSoftware = 'SQLSAFE'
    BEGIN
    SET @CurrentCommandType04 = 'xp_ss_delete'
    SET @CurrentCommand04 = 'DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_ss_delete @filename = N''' + REPLACE(@CurrentDirectoryPath,'''','''''') + '\*.' + @CurrentFileExtension + ''', @age = ''' + CAST(DATEDIFF(mi,@CurrentCleanupDate,GETDATE()) + 1 AS nvarchar) + 'Minutes'' IF @ReturnCode <> 0 RAISERROR(''Error deleting SQLsafe backup files.'', 16, 1)'
    END
    EXECUTE @CurrentCommandOutput04 = [dbo].[CommandExecute] @Command = @CurrentCommand04, @CommandType = @CurrentCommandType04, @Mode = 1, @DatabaseName = @CurrentDatabaseName, @LogToTable = @LogToTable, @Execute = @Execute
    SET @Error = @@ERROR
    IF @Error <> 0 SET @CurrentCommandOutput04 = @Error
    IF @CurrentCommandOutput04 <> 0 SET @ReturnCode = @CurrentCommandOutput04
    UPDATE @CurrentDirectories
    SET CleanupCompleted = 1,
    CleanupOutput = @CurrentCommandOutput04
    WHERE ID = @CurrentDirectoryID
    SET @CurrentDirectoryID = NULL
    SET @CurrentDirectoryPath = NULL
    SET @CurrentCommand04 = NULL
    SET @CurrentCommandOutput04 = NULL
    SET @CurrentCommandType04 = NULL
    END
    END
    END
    -- Update that the database is completed
    UPDATE @tmpDatabases
    SET Completed = 1
    WHERE Selected = 1
    AND Completed = 0
    AND ID = @CurrentDBID
    -- Clear variables
    SET @CurrentDBID = NULL
    SET @CurrentDatabaseID = NULL
    SET @CurrentDatabaseName = NULL
    SET @CurrentBackupType = NULL
    SET @CurrentFileExtension = NULL
    SET @CurrentFileNumber = NULL
    SET @CurrentDifferentialBaseLSN = NULL
    SET @CurrentDifferentialBaseIsSnapshot = NULL
    SET @CurrentLogLSN = NULL
    SET @CurrentLatestBackup = NULL
    SET @CurrentDatabaseNameFS = NULL
    SET @CurrentDatabaseType = NULL
    SET @CurrentDate = NULL
    SET @CurrentCleanupDate = NULL
    SET @CurrentIsDatabaseAccessible = NULL
    SET @CurrentAvailabilityGroup = NULL
    SET @CurrentAvailabilityGroupRole = NULL
    SET @CurrentIsPreferredBackupReplica = NULL
    SET @CurrentDatabaseMirroringRole = NULL
    SET @CurrentLogShippingRole = NULL
    SET @CurrentCommand02 = NULL
    SET @CurrentCommand03 = NULL
    SET @CurrentCommandOutput02 = NULL
    SET @CurrentCommandOutput03 = NULL
    SET @CurrentCommandType02 = NULL
    SET @CurrentCommandType03 = NULL
    DELETE FROM @CurrentDirectories
    DELETE FROM @CurrentFiles
    END
    --// Log completing information //--
    Logging:
    SET @EndMessage = 'Date and time: ' + CONVERT(nvarchar,GETDATE(),120)
    SET @EndMessage = REPLACE(@EndMessage,'%','%%')
    RAISERROR(@EndMessage,10,1) WITH NOWAIT
    IF @ReturnCode <> 0
    BEGIN
    RETURN @ReturnCode
    END
    END
    GO
    ALTER AUTHORIZATION ON [dbo].[DatabaseBackup] TO SCHEMA OWNER
    GO
    Diane

  • Msg 102, Level 15, State 1, Line 1

    There is index rebuild anr reorzine index following error showing.Early days it was working fine.
    Executed: ALTER INDEX [L_CONFI_HIST_PK] ON [dbo].[L_CONFI_HIST] REBUILD PARTITION=6 PARTITION=2
    Msg 102, Level 15, State 1, Line 1
    Incorrect syntax near 'PARTITION'.

    As others suggested, you can not REBUILD more than 1 partition without specifying ALL.
    Excerpt:
    PARTITION
    Specifies that only one partition of an index will be rebuilt or reorganized. PARTITION cannot be specified if index_name is not a partitioned
    index.
    Ref: https://technet.microsoft.com/en-us/library/ms188388(v=sql.105).aspx
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped.
     [Blog]

  • Linked server error 7303

    Good morning,
    I created a linked server to
    access sql server 2008.
    At first everything worked properly,
    now returns the error 7303.
    I have already shared the folder with all
    of the database permits users full control
    of sql server users , users of
    the system, etc..
    Does anyone know how can you fix?
    I found a solution on the Internet,
    says to create an Admin user
    on the machine, and as a path
    to a temporary folder on your
    hard disk different from the profile, by entering
    the required permissions to SQL Server, but not
    seems to work after a while let us start, however,
    to return the error 7303.
    i found if i drop linked server , restart pc and reacreate it , it work , but when i restard pc all return error 7303.
    SQL Version : 10.0.2573
    Thanking you in advance,
    GR

    Hi Normand!,
    Access db its a 97 version converted into 2000.
    If i run a select statement(select * from TEST...TAB_EAN from SSMS error is(italian version):
    Il provider OLE DB "Microsoft.Jet.OLEDB.4.0" per il server collegato "TEST" ha restituito il messaggio "Errore non specificato.".
    Messaggio 7303, livello 16, stato 1, riga 1
    Impossibile inizializzare l'oggetto origine dei dati del provider OLE DB "Microsoft.Jet.OLEDB.4.0" per il server collegato "TEST".
    I've read the link you provide but without success.
    The linked server use the provider: Microsoft.Jet.OLEDB.4.0
    GR
    update:
    I tryed to use sql server with user Local System , i can create the linked server  and use it, but if i restart the pc and restart the sql server service, i have the the same error (7303)

  • SQL 2000 LINKED SERVER ERROR 7303 WITH SQL 2012 ON WINDOWS SERVER 2012 R2

    Hi all,
    I have a problem with SQL Server 2012 and a linked server for SQL 2000 database.
    System specification:
    Windows server 2012 64 bit
    SQL SERVER 2012 64 bit
    REMOTE SERVER SQL 2000 32 bit
    I've installed sql native client 32bit,create a SYSTEM DSN odbc connection with "SQL Server" driver named "MYSERVER".
    Create a linked server with the query below in SQL 2012:
    EXEC master.dbo.sp_addlinkedserver @server =N'MYSERVER', @srvproduct=N'MYSERVER', @provider=N'MSDASQL', @datasrc =N'MYSERVER', @location=N'System';
    EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'MySERVER',@useself=N'True',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL
    GO
    but when i browse the linked server i receved error 7303.

    You need 64-bit MSDASQL. It is the bitness of the server you are connecting from that matters. 64-bit executables cannot hook into 32-bit DLL.
    However, I suspect that you will not get things to work anyway. At least I have not seen anyone this far who has been able to set up a linked server from SQL 2012 to SQL 2000. I know that when I tried this, the following providers had this result:
    SQLNCLI11 - does not support connections to SQL 2000.
    SQLNCLI10 - failed with some obscure message that I don't recall.
    SQLNCLI - Don't recall that the problem was here.
    SQLOLEDB - SQLOLEDB is always replaced with the most recent version of SQLNCLI, so this fails because of lack of support.
    I don't think I got through all version of the ODBC drivers, though.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Linked Server error: Login Failed for user 'NT AUTHORITY\ANONYMOUS LOGON' between sql server 2005 32 bit and sql server 2012 64 bit

    Hi All,
    Here the linked server is created between sql server 2012 64 bit and sql server 2005 32 bit. I am getting the below error  when i try to access linked server from third server. I have created linked from Instance 1 to Instance 2. When i access it from
    instance 3 i am getting the below error. SPN setting has been done between these 2 servers. Also the option 'Trust the delegate' is enabled for the both the service account. 
    'Login Failed for user 'NT AUTHORITY\ANONYMOUS LOGON' 
    Appreciate your quick response. 
    Vikas.M.S

    Hello,
    Please read the following resources:
    http://www.databasejournal.com/features/mssql/article.php/3696506/Setting-Up-Delegation-for-Linked-Servers.htm
    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/ea26de43-4c6b-4991-86d7-e1578f107c92/linked-server-login-failed-for-user-nt-authorityanonymous-logon?forum=sqldataaccess
    Hope this helps.
    Regards,
    Alberto Morillo
    SQLCoffee.com

  • Like Server error Linked Server error: Login Failed for user 'NT AUTHORITY\ANONYMOUS LOGON'

     Getting error when trying to test the link server from remote ... Please help

    Hello,
    You've supplied us with little information other than what little is in the title. I'll refer you to a post I wrote about this. Please read
    through it and post back if it does not fix your issue. There are a few other resource links in the post to check out, but it's most likely related to a Kerberos/SPN issue.
    -Sean
    The views, opinions, and posts do not reflect those of my company and are solely my own. No warranty, service, or results are expressed or implied.

Maybe you are looking for

  • Intermedia index creation error

    Hi, I am getting the following error while importing a table with intermedia index on it. error occured in execution of ODCIINDEXCREATE routine. intermedia text error : Lost RPC connection to external procedure agent. at CTXSYS.DRUE at CTXSYS.TEXTIND

  • What is best practice for quicktime and windows8.1?

    i am looking for best practice on installing quicktime on win8.1 machines.  I only found discussion of installing in compatability mode.  Is that the best practice?

  • Turn off adaptive display for AC mode

    Hi, I am in curiosity of how I can turn off adaptive brightness display for external mode. http://i.imgur.com/nnDnd9Y.jpg I have already turned it off from Power Options in Control Panel. At first, this did not work alone to turn adaptive brightness

  • Manual Correction in PCA

    Hi All, I am correcting some wrong postings in PCA for 2007 using 1KEL. After the corrections when I run KE5T, the difference becomes zero which is correct. However, these postings also appear in the KE5T for 2008. 2KES to carry forward balance to 20

  • How do we arrange the Process chain in BI7

    Hi, When I tried to create a chain in BI 7 it is not seen in usual 3.x style i.e., process types linked by a link. so  how do I see the chain in the easy way. Thank you, Prasad