ÿþS E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 C R E A T E   P R O C E D U R E   [ d b o ] . [ D a t a b a s e B a c k u p ]  
  
 @ D a t a b a s e s   n v a r c h a r ( m a x ) ,  
 @ D i r e c t o r y   n v a r c h a r ( m a x )   =   N U L L ,  
 @ B a c k u p T y p e   n v a r c h a r ( m a x ) ,  
 @ V e r i f y   n v a r c h a r ( m a x )   =   ' N ' ,  
 @ C l e a n u p T i m e   i n t   =   N U L L ,  
 @ C o m p r e s s   n v a r c h a r ( m a x )   =   N U L L ,  
 @ C o p y O n l y   n v a r c h a r ( m a x )   =   ' N ' ,  
 @ C h a n g e B a c k u p T y p e   n v a r c h a r ( m a x )   =   ' N ' ,  
 @ B a c k u p S o f t w a r e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ C h e c k S u m   n v a r c h a r ( m a x )   =   ' N ' ,  
 @ B l o c k S i z e   i n t   =   N U L L ,  
 @ B u f f e r C o u n t   i n t   =   N U L L ,  
 @ M a x T r a n s f e r S i z e   i n t   =   N U L L ,  
 @ N u m b e r O f F i l e s   i n t   =   1 ,  
 @ C o m p r e s s i o n L e v e l   i n t   =   N U L L ,  
 @ D e s c r i p t i o n   n v a r c h a r ( m a x )   =   N U L L ,  
 @ T h r e a d s   i n t   =   N U L L ,  
 @ T h r o t t l e   i n t   =   N U L L ,  
 @ E n c r y p t   n v a r c h a r ( m a x )   =   ' N ' ,  
 @ E n c r y p t i o n T y p e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ E n c r y p t i o n K e y   n v a r c h a r ( m a x )   =   N U L L ,  
 @ L o g T o T a b l e   n v a r c h a r ( m a x )   =   ' N ' ,  
 @ E x e c u t e   n v a r c h a r ( m a x )   =   ' Y '  
  
 A S  
  
 B E G I N  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   S o u r c e :   h t t p : / / o l a . h a l l e n g r e n . c o m                                                                                                                     / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     S E T   N O C O U N T   O N  
  
     D E C L A R E   @ S t a r t M e s s a g e   n v a r c h a r ( m a x )  
     D E C L A R E   @ E n d M e s s a g e   n v a r c h a r ( m a x )  
     D E C L A R E   @ D a t a b a s e M e s s a g e   n v a r c h a r ( m a x )  
     D E C L A R E   @ E r r o r M e s s a g e   n v a r c h a r ( m a x )  
  
     D E C L A R E   @ V e r s i o n   n u m e r i c ( 1 8 , 1 0 )  
  
     D E C L A R E   @ D e f a u l t D i r e c t o r y   n v a r c h a r ( 4 0 0 0 )  
     D E C L A R E   @ C h e c k D i r e c t o r y   n v a r c h a r ( 4 0 0 0 )  
  
     D E C L A R E   @ C u r r e n t I D   i n t  
     D E C L A R E   @ C u r r e n t D a t a b a s e I D   i n t  
     D E C L A R E   @ C u r r e n t D a t a b a s e N a m e   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t B a c k u p T y p e   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t F i l e E x t e n s i o n   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t F i l e N u m b e r   i n t  
     D E C L A R E   @ C u r r e n t D i f f e r e n t i a l L S N   n u m e r i c ( 2 5 , 0 )  
     D E C L A R E   @ C u r r e n t L o g L S N   n u m e r i c ( 2 5 , 0 )  
     D E C L A R E   @ C u r r e n t L a t e s t B a c k u p   d a t e t i m e  
     D E C L A R E   @ C u r r e n t D a t a b a s e N a m e F S   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t D i r e c t o r y   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t F i l e P a t h   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t D a t e   d a t e t i m e  
     D E C L A R E   @ C u r r e n t C l e a n u p D a t e   d a t e t i m e  
     D E C L A R E   @ C u r r e n t I s D a t a b a s e A c c e s s i b l e   b i t  
     D E C L A R E   @ C u r r e n t M i r r o r i n g R o l e   n v a r c h a r ( m a x )  
  
     D E C L A R E   @ C u r r e n t C o m m a n d 0 1   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t C o m m a n d 0 2   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t C o m m a n d 0 3   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t C o m m a n d 0 4   n v a r c h a r ( m a x )  
  
     D E C L A R E   @ C u r r e n t C o m m a n d O u t p u t 0 1   i n t  
     D E C L A R E   @ C u r r e n t C o m m a n d O u t p u t 0 2   i n t  
     D E C L A R E   @ C u r r e n t C o m m a n d O u t p u t 0 3   i n t  
     D E C L A R E   @ C u r r e n t C o m m a n d O u t p u t 0 4   i n t  
  
     D E C L A R E   @ C u r r e n t C o m m a n d T y p e 0 1   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t C o m m a n d T y p e 0 2   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t C o m m a n d T y p e 0 3   n v a r c h a r ( m a x )  
     D E C L A R E   @ C u r r e n t C o m m a n d T y p e 0 4   n v a r c h a r ( m a x )  
  
     D E C L A R E   @ D i r e c t o r y I n f o   T A B L E   ( F i l e E x i s t s   b i t ,  
                                                                 F i l e I s A D i r e c t o r y   b i t ,  
                                                                 P a r e n t D i r e c t o r y E x i s t s   b i t )  
  
     D E C L A R E   @ t m p D a t a b a s e s   T A B L E   ( I D   i n t   I D E N T I T Y   P R I M A R Y   K E Y ,  
                                                               D a t a b a s e N a m e   n v a r c h a r ( m a x ) ,  
                                                               C o m p l e t e d   b i t )  
  
     D E C L A R E   @ C u r r e n t F i l e s   T A B L E   ( C u r r e n t F i l e P a t h   n v a r c h a r ( m a x ) )  
  
     D E C L A R E   @ E r r o r   i n t  
     D E C L A R E   @ R e t u r n C o d e   i n t  
  
     S E T   @ E r r o r   =   0  
     S E T   @ R e t u r n C o d e   =   0  
  
     S E T   @ V e r s i o n   =   C A S T ( L E F T ( C A S T ( S E R V E R P R O P E R T Y ( ' P r o d u c t V e r s i o n ' )   A S   n v a r c h a r ( m a x ) ) , C H A R I N D E X ( ' . ' , C A S T ( S E R V E R P R O P E R T Y ( ' P r o d u c t V e r s i o n ' )   A S   n v a r c h a r ( m a x ) ) )   -   1 )   +   ' . '   +   R E P L A C E ( R I G H T ( C A S T ( S E R V E R P R O P E R T Y ( ' P r o d u c t V e r s i o n ' )   A S   n v a r c h a r ( m a x ) ) ,   L E N ( C A S T ( S E R V E R P R O P E R T Y ( ' P r o d u c t V e r s i o n ' )   A S   n v a r c h a r ( m a x ) ) )   -   C H A R I N D E X ( ' . ' , C A S T ( S E R V E R P R O P E R T Y ( ' P r o d u c t V e r s i o n ' )   A S   n v a r c h a r ( m a x ) ) ) ) , ' . ' , ' ' )   A S   n u m e r i c ( 1 8 , 1 0 ) )  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   L o g   i n i t i a l   i n f o r m a t i o n                                                                                                                                         / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     S E T   @ S t a r t M e s s a g e   =   ' D a t e T i m e :   '   +   C O N V E R T ( n v a r c h a r , G E T D A T E ( ) , 1 2 0 )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' S e r v e r :   '   +   C A S T ( S E R V E R P R O P E R T Y ( ' S e r v e r N a m e ' )   A S   n v a r c h a r )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' V e r s i o n :   '   +   C A S T ( S E R V E R P R O P E R T Y ( ' P r o d u c t V e r s i o n ' )   A S   n v a r c h a r )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' E d i t i o n :   '   +   C A S T ( S E R V E R P R O P E R T Y ( ' E d i t i o n ' )   A S   n v a r c h a r )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' P r o c e d u r e :   '   +   Q U O T E N A M E ( D B _ N A M E ( D B _ I D ( ) ) )   +   ' . '   +   ( S E L E C T   Q U O T E N A M E ( s c h e m a s . n a m e )   F R O M   s y s . s c h e m a s   s c h e m a s   I N N E R   J O I N   s y s . o b j e c t s   o b j e c t s   O N   s c h e m a s . [ s c h e m a _ i d ]   =   o b j e c t s . [ s c h e m a _ i d ]   W H E R E   [ o b j e c t _ i d ]   =   @ @ P R O C I D )   +   ' . '   +   Q U O T E N A M E ( O B J E C T _ N A M E ( @ @ P R O C I D ) )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' P a r a m e t e r s :   @ D a t a b a s e s   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ D a t a b a s e s , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ D i r e c t o r y   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ D i r e c t o r y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ B a c k u p T y p e   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ B a c k u p T y p e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ V e r i f y   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ V e r i f y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ C l e a n u p T i m e   =   '   +   I S N U L L ( C A S T ( @ C l e a n u p T i m e   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ C o m p r e s s   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ C o m p r e s s , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ C o p y O n l y   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ C o p y O n l y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ C h a n g e B a c k u p T y p e   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ C h a n g e B a c k u p T y p e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ B a c k u p S o f t w a r e   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ B a c k u p S o f t w a r e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ C h e c k S u m   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ C h e c k S u m , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ B l o c k S i z e   =   '   +   I S N U L L ( C A S T ( @ B l o c k S i z e   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ B u f f e r C o u n t   =   '   +   I S N U L L ( C A S T ( @ B u f f e r C o u n t   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ M a x T r a n s f e r S i z e   =   '   +   I S N U L L ( C A S T ( @ M a x T r a n s f e r S i z e   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ N u m b e r O f F i l e s   =   '   +   I S N U L L ( C A S T ( @ N u m b e r O f F i l e s   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ C o m p r e s s i o n L e v e l   =   '   +   I S N U L L ( C A S T ( @ C o m p r e s s i o n L e v e l   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ D e s c r i p t i o n   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ D e s c r i p t i o n , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ T h r e a d s   =   '   +   I S N U L L ( C A S T ( @ T h r e a d s   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ T h r o t t l e   =   '   +   I S N U L L ( C A S T ( @ T h r o t t l e   A S   n v a r c h a r ) , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ E n c r y p t   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ E n c r y p t , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ E n c r y p t i o n T y p e   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ E n c r y p t i o n T y p e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ E n c r y p t i o n K e y   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ E n c r y p t i o n K e y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ L o g T o T a b l e   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ L o g T o T a b l e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' ,   @ E x e c u t e   =   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ E x e c u t e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ S t a r t M e s s a g e   =   @ S t a r t M e s s a g e   +   ' S o u r c e :   h t t p : / / o l a . h a l l e n g r e n . c o m '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
     S E T   @ S t a r t M e s s a g e   =   R E P L A C E ( @ S t a r t M e s s a g e , ' % ' , ' % % ' )  
     R A I S E R R O R ( @ S t a r t M e s s a g e , 1 0 , 1 )   W I T H   N O W A I T  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   C h e c k   c o r e   r e q u i r e m e n t s                                                                                                                                         / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     I F   S E R V E R P R O P E R T Y ( ' E n g i n e E d i t i o n ' )   =   5  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' S Q L   A z u r e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ E r r o r   < >   0  
     B E G I N  
         S E T   @ R e t u r n C o d e   =   @ E r r o r  
         G O T O   L o g g i n g  
     E N D  
  
     I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   o b j e c t s   I N N E R   J O I N   s y s . s c h e m a s   s c h e m a s   O N   o b j e c t s . [ s c h e m a _ i d ]   =   s c h e m a s . [ s c h e m a _ i d ]   W H E R E   o b j e c t s . [ t y p e ]   =   ' P '   A N D   s c h e m a s . [ n a m e ]   =   ' d b o '   A N D   o b j e c t s . [ n a m e ]   =   ' C o m m a n d E x e c u t e ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   s t o r e d   p r o c e d u r e   C o m m a n d E x e c u t e   i s   m i s s i n g .   D o w n l o a d   h t t p : / / o l a . h a l l e n g r e n . c o m / s c r i p t s / C o m m a n d E x e c u t e . s q l . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   o b j e c t s   I N N E R   J O I N   s y s . s c h e m a s   s c h e m a s   O N   o b j e c t s . [ s c h e m a _ i d ]   =   s c h e m a s . [ s c h e m a _ i d ]   W H E R E   o b j e c t s . [ t y p e ]   =   ' P '   A N D   s c h e m a s . [ n a m e ]   =   ' d b o '   A N D   o b j e c t s . [ n a m e ]   =   ' C o m m a n d E x e c u t e '   A N D   O B J E C T _ D E F I N I T I O N ( o b j e c t s . [ o b j e c t _ i d ] )   N O T   L I K E   ' % @ L o g T o T a b l e % ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   s t o r e d   p r o c e d u r e   C o m m a n d E x e c u t e   n e e d s   t o   b e   u p d a t e d .   D o w n l o a d   h t t p : / / o l a . h a l l e n g r e n . c o m / s c r i p t s / C o m m a n d E x e c u t e . s q l . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   o b j e c t s   I N N E R   J O I N   s y s . s c h e m a s   s c h e m a s   O N   o b j e c t s . [ s c h e m a _ i d ]   =   s c h e m a s . [ s c h e m a _ i d ]   W H E R E   o b j e c t s . [ t y p e ]   =   ' T F '   A N D   s c h e m a s . [ n a m e ]   =   ' d b o '   A N D   o b j e c t s . [ n a m e ]   =   ' D a t a b a s e S e l e c t ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   f u n c t i o n   D a t a b a s e S e l e c t   i s   m i s s i n g .   D o w n l o a d   h t t p : / / o l a . h a l l e n g r e n . c o m / s c r i p t s / D a t a b a s e S e l e c t . s q l . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ L o g T o T a b l e   =   ' Y '   A N D   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   o b j e c t s   I N N E R   J O I N   s y s . s c h e m a s   s c h e m a s   O N   o b j e c t s . [ s c h e m a _ i d ]   =   s c h e m a s . [ s c h e m a _ i d ]   W H E R E   o b j e c t s . [ t y p e ]   =   ' U '   A N D   s c h e m a s . [ n a m e ]   =   ' d b o '   A N D   o b j e c t s . [ n a m e ]   =   ' C o m m a n d L o g ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   t a b l e   C o m m a n d L o g   i s   m i s s i n g .   D o w n l o a d   h t t p : / / o l a . h a l l e n g r e n . c o m / s c r i p t s / C o m m a n d L o g . s q l . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ E r r o r   < >   0  
     B E G I N  
         S E T   @ R e t u r n C o d e   =   @ E r r o r  
         G O T O   L o g g i n g  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   S e l e c t   d a t a b a s e s                                                                                                                                                       / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     I F   @ D a t a b a s e s   I S   N U L L   O R   @ D a t a b a s e s   =   ' '  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ D a t a b a s e s   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I N S E R T   I N T O   @ t m p D a t a b a s e s   ( D a t a b a s e N a m e ,   C o m p l e t e d )  
     S E L E C T   D a t a b a s e N a m e   A S   D a t a b a s e N a m e ,  
                   0   A S   C o m p l e t e d  
     F R O M   d b o . D a t a b a s e S e l e c t   ( @ D a t a b a s e s )  
     O R D E R   B Y   D a t a b a s e N a m e   A S C  
  
     I F   @ @ E R R O R   < >   0  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' E r r o r   s e l e c t i n g   d a t a b a s e s . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     S E T   @ E r r o r M e s s a g e   =   ' '  
     S E L E C T   @ E r r o r M e s s a g e   =   @ E r r o r M e s s a g e   +   Q U O T E N A M E ( D a t a b a s e N a m e )   +   ' ,   '  
     F R O M   @ t m p D a t a b a s e s  
     W H E R E   R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( D a t a b a s e N a m e , ' \ ' , ' ' ) , ' / ' , ' ' ) , ' : ' , ' ' ) , ' * ' , ' ' ) , ' ? ' , ' ' ) , ' " ' , ' ' ) , ' < ' , ' ' ) , ' > ' , ' ' ) , ' | ' , ' ' ) , '   ' , ' ' )   =   ' '  
     O R D E R   B Y   D a t a b a s e N a m e   A S C  
     I F   @ @ R O W C O U N T   >   0  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   n a m e s   o f   t h e   f o l l o w i n g   d a t a b a s e s   a r e   n o t   s u p p o r t e d ;   '   +   L E F T ( @ E r r o r M e s s a g e , L E N ( @ E r r o r M e s s a g e ) - 1 )   +   ' . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     S E T   @ E r r o r M e s s a g e   =   ' ' ;  
     W I T H   t m p D a t a b a s e s C T E  
     A S  
     (  
     S E L E C T   n a m e   A S   D a t a b a s e N a m e ,  
                   U P P E R ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( n a m e , ' \ ' , ' ' ) , ' / ' , ' ' ) , ' : ' , ' ' ) , ' * ' , ' ' ) , ' ? ' , ' ' ) , ' " ' , ' ' ) , ' < ' , ' ' ) , ' > ' , ' ' ) , ' | ' , ' ' ) , '   ' , ' ' ) )   A S   D a t a b a s e N a m e F S  
     F R O M   s y s . d a t a b a s e s  
     )  
     S E L E C T   @ E r r o r M e s s a g e   =   @ E r r o r M e s s a g e   +   Q U O T E N A M E ( D a t a b a s e N a m e )   +   ' ,   '  
     F R O M   t m p D a t a b a s e s C T E  
     W H E R E   D a t a b a s e N a m e F S   I N ( S E L E C T   D a t a b a s e N a m e F S   F R O M   t m p D a t a b a s e s C T E   G R O U P   B Y   D a t a b a s e N a m e F S   H A V I N G   C O U N T ( * )   >   1 )  
     A N D   D a t a b a s e N a m e F S   I N ( S E L E C T   U P P E R ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( D a t a b a s e N a m e   C O L L A T E   D A T A B A S E _ D E F A U L T , ' \ ' , ' ' ) , ' / ' , ' ' ) , ' : ' , ' ' ) , ' * ' , ' ' ) , ' ? ' , ' ' ) , ' " ' , ' ' ) , ' < ' , ' ' ) , ' > ' , ' ' ) , ' | ' , ' ' ) , '   ' , ' ' ) )   F R O M   @ t m p D a t a b a s e s )  
     A N D   D a t a b a s e N a m e F S   < >   ' '  
     O R D E R   B Y   D a t a b a s e N a m e F S   A S C ,   D a t a b a s e N a m e   A S C  
     I F   @ @ R O W C O U N T   >   0  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   n a m e s   o f   t h e   f o l l o w i n g   d a t a b a s e s   a r e   n o t   u n i q u e   i n   t h e   f i l e   s y s t e m ;   '   +   L E F T ( @ E r r o r M e s s a g e , L E N ( @ E r r o r M e s s a g e ) - 1 )   +   ' . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   G e t   d e f a u l t   b a c k u p   d i r e c t o r y                                                                                                                               / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     I F   @ D i r e c t o r y   I S   N U L L  
     B E G I N  
         E X E C U T E   [ m a s t e r ] . d b o . x p _ i n s t a n c e _ r e g r e a d   N ' H K E Y _ L O C A L _ M A C H I N E ' ,   N ' S O F T W A R E \ M i c r o s o f t \ M S S Q L S e r v e r \ M S S Q L S e r v e r ' ,   N ' B a c k u p D i r e c t o r y ' ,   @ D e f a u l t D i r e c t o r y   O U T P U T  
         S E T   @ D i r e c t o r y   =   @ D e f a u l t D i r e c t o r y  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   G e t   d e f a u l t   c o m p r e s s i o n                                                                                                                                         / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     I F   @ C o m p r e s s   I S   N U L L  
     B E G I N  
         S E L E C T   @ C o m p r e s s   =   C A S E  
         W H E N   @ B a c k u p S o f t w a r e   I S   N U L L   A N D   E X I S T S ( S E L E C T   *   F R O M   s y s . c o n f i g u r a t i o n s   W H E R E   n a m e   =   ' b a c k u p   c o m p r e s s i o n   d e f a u l t '   A N D   v a l u e _ i n _ u s e   =   1 )   T H E N   ' Y '  
         W H E N   @ B a c k u p S o f t w a r e   I S   N U L L   A N D   N O T   E X I S T S ( S E L E C T   *   F R O M   s y s . c o n f i g u r a t i o n s   W H E R E   n a m e   =   ' b a c k u p   c o m p r e s s i o n   d e f a u l t '   A N D   v a l u e _ i n _ u s e   =   1 )   T H E N   ' N '  
         W H E N   @ B a c k u p S o f t w a r e   I S   N O T   N U L L   A N D   ( @ C o m p r e s s i o n L e v e l   I S   N U L L   O R   @ C o m p r e s s i o n L e v e l   >   0 )     T H E N   ' Y '  
         W H E N   @ B a c k u p S o f t w a r e   I S   N O T   N U L L   A N D   @ C o m p r e s s i o n L e v e l   =   0     T H E N   ' N '  
         E N D  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   C h e c k   d i r e c t o r y                                                                                                                                                         / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     I F   N O T   ( @ D i r e c t o r y   L I K E   ' _ : '   O R   @ D i r e c t o r y   L I K E   ' _ : \ % '   O R   @ D i r e c t o r y   L I K E   ' \ \ % \ % ' )   O R   @ D i r e c t o r y   I S   N U L L   O R   L E F T ( @ D i r e c t o r y , 1 )   =   '   '   O R   R I G H T ( @ D i r e c t o r y , 1 )   =   '   '  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ D i r e c t o r y   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     S E T   @ C h e c k D i r e c t o r y   =   @ D i r e c t o r y  
  
     I N S E R T   I N T O   @ D i r e c t o r y I n f o   ( F i l e E x i s t s ,   F i l e I s A D i r e c t o r y ,   P a r e n t D i r e c t o r y E x i s t s )  
     E X E C U T E   [ m a s t e r ] . d b o . x p _ f i l e e x i s t   @ C h e c k D i r e c t o r y  
  
     I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   @ D i r e c t o r y I n f o   W H E R E   F i l e E x i s t s   =   0   A N D   F i l e I s A D i r e c t o r y   =   1   A N D   P a r e n t D i r e c t o r y E x i s t s   =   1 )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   d i r e c t o r y   d o e s   n o t   e x i s t . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   C h e c k   i n p u t   p a r a m e t e r s                                                                                                                                           / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     I F   @ B a c k u p T y p e   N O T   I N   ( ' F U L L ' , ' D I F F ' , ' L O G ' )   O R   @ B a c k u p T y p e   I S   N U L L  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ B a c k u p T y p e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ V e r i f y   N O T   I N   ( ' Y ' , ' N ' )   O R   @ V e r i f y   I S   N U L L   O R   ( @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '   A N D   @ E n c r y p t   =   ' Y '   A N D   @ V e r i f y   =   ' Y ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ V e r i f y   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ C l e a n u p T i m e   <   0  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ C l e a n u p T i m e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ C o m p r e s s   N O T   I N   ( ' Y ' , ' N ' )   O R   @ C o m p r e s s   I S   N U L L   O R   ( @ C o m p r e s s   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   I S   N U L L   A N D   N O T   ( ( @ V e r s i o n   > =   1 0   A N D   @ V e r s i o n   <   1 0 . 5   A N D   S E R V E R P R O P E R T Y ( ' E n g i n e E d i t i o n ' )   =   3 )   O R   ( @ V e r s i o n   > =   1 0 . 5   A N D   ( S E R V E R P R O P E R T Y ( ' E n g i n e E d i t i o n ' )   =   3   O R   S E R V E R P R O P E R T Y ( ' E d i t i o n I D ' )   =   - 1 5 3 4 7 2 6 7 6 0 ) ) ) )   O R   ( @ C o m p r e s s   =   ' N '   A N D   @ B a c k u p S o f t w a r e   I S   N O T   N U L L   A N D   ( @ C o m p r e s s i o n L e v e l   I S   N U L L   O R   @ C o m p r e s s i o n L e v e l   > =   1 ) )   O R   ( @ C o m p r e s s   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   I S   N O T   N U L L   A N D   @ C o m p r e s s i o n L e v e l   =   0 )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ C o m p r e s s   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ C o m p r e s s   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   I S   N U L L   A N D   N O T   ( ( @ V e r s i o n   > =   1 0   A N D   @ V e r s i o n   <   1 0 . 5   A N D   S E R V E R P R O P E R T Y ( ' E n g i n e E d i t i o n ' )   =   3 )   O R   ( @ V e r s i o n   > =   1 0 . 5   A N D   ( S E R V E R P R O P E R T Y ( ' E n g i n e E d i t i o n ' )   =   3   O R   S E R V E R P R O P E R T Y ( ' E d i t i o n I D ' )   =   - 1 5 3 4 7 2 6 7 6 0 ) ) )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' B a c k u p   c o m p r e s s i o n   i s   o n l y   s u p p o r t e d   i n   S Q L   S e r v e r   2 0 0 8   E n t e r p r i s e   a n d   D e v e l o p e r   E d i t i o n   a n d   i n   S Q L   S e r v e r   2 0 0 8   R 2   S t a n d a r d ,   E n t e r p r i s e ,   D e v e l o p e r   a n d   D a t a c e n t e r   E d i t i o n . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ C o p y O n l y   N O T   I N   ( ' Y ' , ' N ' )   O R   @ C o p y O n l y   I S   N U L L  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ C o p y O n l y   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ C h a n g e B a c k u p T y p e   N O T   I N   ( ' Y ' , ' N ' )   O R   @ C h a n g e B a c k u p T y p e   I S   N U L L  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ C h a n g e B a c k u p T y p e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ B a c k u p S o f t w a r e   N O T   I N   ( ' L I T E S P E E D ' , ' S Q L B A C K U P ' , ' H Y P E R B A C ' , ' S Q L S A F E ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ B a c k u p S o f t w a r e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '   A N D   N O T   E X I S T S   ( S E L E C T   *   F R O M   [ m a s t e r ] . s y s . o b j e c t s   W H E R E   [ t y p e ]   =   ' X '   A N D   [ n a m e ]   =   ' x p _ b a c k u p _ d a t a b a s e ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' L i t e S p e e d   i s   n o t   i n s t a l l e d .   D o w n l o a d   h t t p : / / w w w . q u e s t . c o m / l i t e s p e e d - f o r - s q l - s e r v e r / . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '   A N D   N O T   E X I S T S   ( S E L E C T   *   F R O M   [ m a s t e r ] . s y s . o b j e c t s   W H E R E   [ t y p e ]   =   ' X '   A N D   [ n a m e ]   =   ' s q l b a c k u p ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' S Q L B a c k u p   i s   n o t   i n s t a l l e d .   D o w n l o a d   h t t p : / / w w w . r e d - g a t e . c o m / p r o d u c t s / d b a / s q l - b a c k u p / . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '   A N D   N O T   E X I S T S   ( S E L E C T   *   F R O M   [ m a s t e r ] . s y s . o b j e c t s   W H E R E   [ t y p e ]   =   ' X '   A N D   [ n a m e ]   =   ' x p _ s s _ b a c k u p ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' S Q L s a f e   i s   n o t   i n s t a l l e d .   D o w n l o a d   h t t p : / / w w w . i d e r a . c o m / P r o d u c t s / S Q L - S e r v e r / S Q L - s a f e - b a c k u p / . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ C h e c k S u m   N O T   I N   ( ' Y ' , ' N ' )   O R   @ C h e c k S u m   I S   N U L L  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ C h e c k S u m   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ B l o c k S i z e   N O T   I N   ( 5 1 2 , 1 0 2 4 , 2 0 4 8 , 4 0 9 6 , 8 1 9 2 , 1 6 3 8 4 , 3 2 7 6 8 , 6 5 5 3 6 )   O R   ( @ B l o c k S i z e   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P ' )   O R   ( @ B l o c k S i z e   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ B l o c k S i z e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ B u f f e r C o u n t   < =   0   O R   @ B u f f e r C o u n t   >   2 1 4 7 4 8 3 6 4 7   O R   ( @ B u f f e r C o u n t   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P ' )   O R   ( @ B u f f e r C o u n t   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ B u f f e r C o u n t   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ M a x T r a n s f e r S i z e   <   6 5 5 3 6   O R   @ M a x T r a n s f e r S i z e   >   4 1 9 4 3 0 4   O R   @ M a x T r a n s f e r S i z e   %   6 5 5 3 6   >   0   O R   ( @ M a x T r a n s f e r S i z e   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P ' )   O R   ( @ M a x T r a n s f e r S i z e   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ M a x T r a n s f e r S i z e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ N u m b e r O f F i l e s   <   1   O R   @ N u m b e r O f F i l e s   >   6 4   O R   ( @ N u m b e r O f F i l e s   >   3 2   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P ' )   O R   @ N u m b e r O f F i l e s   I S   N U L L  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ N u m b e r O f F i l e s   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   ( @ B a c k u p S o f t w a r e   I S   N U L L   A N D   @ C o m p r e s s i o n L e v e l   I S   N O T   N U L L )   O R   ( @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '   A N D   @ C o m p r e s s i o n L e v e l   I S   N O T   N U L L )   O R   ( @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '   A N D   ( @ C o m p r e s s i o n L e v e l   <   0   O R   @ C o m p r e s s i o n L e v e l   >   1 0 ) )   O R   ( @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '   A N D   ( @ C o m p r e s s i o n L e v e l   <   0   O R   @ C o m p r e s s i o n L e v e l   >   4 ) )   O R   ( @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '   A N D   ( @ C o m p r e s s i o n L e v e l   <   1   O R   @ C o m p r e s s i o n L e v e l   >   4 ) )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ C o m p r e s s i o n L e v e l   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   L E N ( @ D e s c r i p t i o n )   >   2 5 5   O R   ( @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '   A N D   L E N ( @ D e s c r i p t i o n )   >   1 2 8 )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ D e s c r i p t i o n   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ T h r e a d s   I S   N O T   N U L L   A N D   ( @ B a c k u p S o f t w a r e   N O T   I N ( ' L I T E S P E E D ' , ' S Q L B A C K U P ' , ' S Q L S A F E ' )   O R   @ B a c k u p S o f t w a r e   I S   N U L L )   O R   @ T h r e a d s   <   2   O R   @ T h r e a d s   >   3 2  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ T h r e a d s   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ T h r o t t l e   I S   N O T   N U L L   A N D   ( @ B a c k u p S o f t w a r e   N O T   I N ( ' L I T E S P E E D ' )   O R   @ B a c k u p S o f t w a r e   I S   N U L L )   O R   @ T h r o t t l e   <   1   O R   @ T h r o t t l e   >   1 0 0  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ T h r o t t l e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ E n c r y p t   N O T   I N ( ' Y ' , ' N ' )   O R   @ E n c r y p t   I S   N U L L   O R   ( @ E n c r y p t   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   I S   N U L L )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ E n c r y p t   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   ( @ E n c r y p t i o n T y p e   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   I S   N U L L )   O R   ( @ E n c r y p t i o n T y p e   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C ' )   O R   ( @ E n c r y p t i o n T y p e   I S   N O T   N U L L   A N D   @ E n c r y p t   =   ' N ' )   O R   ( ( @ E n c r y p t i o n T y p e   N O T   I N ( ' R C 2 - 4 0 ' , ' R C 2 - 5 6 ' , ' R C 2 - 1 1 2 ' , ' R C 2 - 1 2 8 ' , ' 3 D E S - 1 6 8 ' , ' R C 4 - 1 2 8 ' , ' A E S - 1 2 8 ' , ' A E S - 1 9 2 ' , ' A E S - 2 5 6 ' )   O R   @ E n c r y p t i o n T y p e   I S   N U L L )   A N D   @ E n c r y p t   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D ' )   O R   ( ( @ E n c r y p t i o n T y p e   N O T   I N ( ' A E S - 1 2 8 ' , ' A E S - 2 5 6 ' )   O R   @ E n c r y p t i o n T y p e   I S   N U L L )   A N D   @ E n c r y p t   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P ' )   O R   ( ( @ E n c r y p t i o n T y p e   N O T   I N ( ' A E S - 1 2 8 ' , ' A E S - 2 5 6 ' )   O R   @ E n c r y p t i o n T y p e   I S   N U L L )   A N D   @ E n c r y p t   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E ' )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ E n c r y p t i o n T y p e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   ( @ E n c r y p t i o n K e y   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   I S   N U L L )   O R   ( @ E n c r y p t i o n K e y   I S   N O T   N U L L   A N D   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C ' )   O R   ( @ E n c r y p t i o n K e y   I S   N O T   N U L L   A N D   @ E n c r y p t   =   ' N ' )   O R   ( @ E n c r y p t i o n K e y   I S   N U L L   A N D   @ E n c r y p t   =   ' Y '   A N D   @ B a c k u p S o f t w a r e   I N ( ' L I T E S P E E D ' , ' S Q L B A C K U P ' , ' S Q L S A F E ' ) )  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ E n c r y p t i o n K e y   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ L o g T o T a b l e   N O T   I N ( ' Y ' , ' N ' )   O R   @ L o g T o T a b l e   I S   N U L L  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ L o g T o T a b l e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ E x e c u t e   N O T   I N ( ' Y ' , ' N ' )   O R   @ E x e c u t e   I S   N U L L  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   v a l u e   f o r   p a r a m e t e r   @ E x e c u t e   i s   n o t   s u p p o r t e d . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ E r r o r   =   @ @ E R R O R  
     E N D  
  
     I F   @ E r r o r   < >   0  
     B E G I N  
         S E T   @ E r r o r M e s s a g e   =   ' T h e   d o c u m e n t a t i o n   i s   a v a i l a b l e   o n   h t t p : / / o l a . h a l l e n g r e n . c o m / D o c u m e n t a t i o n . h t m l . '   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         R A I S E R R O R ( @ E r r o r M e s s a g e , 1 6 , 1 )   W I T H   N O W A I T  
         S E T   @ R e t u r n C o d e   =   @ E r r o r  
         G O T O   L o g g i n g  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   E x e c u t e   b a c k u p   c o m m a n d s                                                                                                                                         / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     W H I L E   E X I S T S   ( S E L E C T   *   F R O M   @ t m p D a t a b a s e s   W H E R E   C o m p l e t e d   =   0 )  
     B E G I N  
  
         S E L E C T   T O P   1   @ C u r r e n t I D   =   I D ,  
                                   @ C u r r e n t D a t a b a s e N a m e   =   D a t a b a s e N a m e  
         F R O M   @ t m p D a t a b a s e s  
         W H E R E   C o m p l e t e d   =   0  
         O R D E R   B Y   I D   A S C  
  
         S E T   @ C u r r e n t D a t a b a s e I D   =   D B _ I D ( @ C u r r e n t D a t a b a s e N a m e )  
  
         I F   E X I S T S   ( S E L E C T   *   F R O M   s y s . d a t a b a s e _ r e c o v e r y _ s t a t u s   W H E R E   d a t a b a s e _ i d   =   @ C u r r e n t D a t a b a s e I D   A N D   d a t a b a s e _ g u i d   I S   N O T   N U L L )  
         B E G I N  
             S E T   @ C u r r e n t I s D a t a b a s e A c c e s s i b l e   =   1  
         E N D  
         E L S E  
         B E G I N  
             S E T   @ C u r r e n t I s D a t a b a s e A c c e s s i b l e   =   0  
         E N D  
  
         S E L E C T   @ C u r r e n t M i r r o r i n g R o l e   =   m i r r o r i n g _ r o l e _ d e s c  
         F R O M   s y s . d a t a b a s e _ m i r r o r i n g  
         W H E R E   d a t a b a s e _ i d   =   @ C u r r e n t D a t a b a s e I D  
  
         S E L E C T   @ C u r r e n t D i f f e r e n t i a l L S N   =   d i f f e r e n t i a l _ b a s e _ l s n  
         F R O M   s y s . m a s t e r _ f i l e s  
         W H E R E   d a t a b a s e _ i d   =   @ C u r r e n t D a t a b a s e I D  
         A N D   [ t y p e ]   =   0  
         A N D   [ f i l e _ i d ]   =   1  
  
         - -   W o r k a r o u n d   f o r   a   b u g   i n   S Q L   S e r v e r   2 0 0 5  
         I F   @ V e r s i o n   > =   9   A N D   @ V e r s i o n   <   1 0  
         A N D   ( S E L E C T   d i f f e r e n t i a l _ b a s e _ l s n   F R O M   s y s . m a s t e r _ f i l e s   W H E R E   d a t a b a s e _ i d   =   @ C u r r e n t D a t a b a s e I D   A N D   [ t y p e ]   =   0   A N D   [ f i l e _ i d ]   =   1 )   =   ( S E L E C T   d i f f e r e n t i a l _ b a s e _ l s n   F R O M   s y s . m a s t e r _ f i l e s   W H E R E   d a t a b a s e _ i d   =   D B _ I D ( ' m o d e l ' )   A N D   [ t y p e ]   =   0   A N D   [ f i l e _ i d ]   =   1 )  
         A N D   ( S E L E C T   d i f f e r e n t i a l _ b a s e _ g u i d   F R O M   s y s . m a s t e r _ f i l e s   W H E R E   d a t a b a s e _ i d   =   @ C u r r e n t D a t a b a s e I D   A N D   [ t y p e ]   =   0   A N D   [ f i l e _ i d ]   =   1 )   =   ( S E L E C T   d i f f e r e n t i a l _ b a s e _ g u i d   F R O M   s y s . m a s t e r _ f i l e s   W H E R E   d a t a b a s e _ i d   =   D B _ I D ( ' m o d e l ' )   A N D   [ t y p e ]   =   0   A N D   [ f i l e _ i d ]   =   1 )  
         A N D   ( S E L E C T   d i f f e r e n t i a l _ b a s e _ t i m e   F R O M   s y s . m a s t e r _ f i l e s   W H E R E   d a t a b a s e _ i d   =   @ C u r r e n t D a t a b a s e I D   A N D   [ t y p e ]   =   0   A N D   [ f i l e _ i d ]   =   1 )   I S   N U L L  
         B E G I N  
             S E T   @ C u r r e n t D i f f e r e n t i a l L S N   =   N U L L  
         E N D  
  
         - -   I f   a   V S S   s n a p s h o t   h a s   b e e n   t a k e n   s i n c e   t h e   l a s t   f u l l   b a c k u p ,   a   d i f f e r e n t i a l   b a c k u p   c a n n o t   b e   p e r f o r m e d  
         I F   E X I S T S   ( S E L E C T   *   F R O M   m s d b . d b o . b a c k u p s e t   W H E R E   d a t a b a s e _ n a m e   =   @ C u r r e n t D a t a b a s e N a m e   A N D   [ t y p e ]   =   ' D '   A N D   i s _ s n a p s h o t   =   1   A N D   c h e c k p o i n t _ l s n   =   @ C u r r e n t D i f f e r e n t i a l L S N )  
         B E G I N  
             S E T   @ C u r r e n t D i f f e r e n t i a l L S N   =   N U L L  
         E N D  
  
         S E L E C T   @ C u r r e n t L o g L S N   =   l a s t _ l o g _ b a c k u p _ l s n  
         F R O M   s y s . d a t a b a s e _ r e c o v e r y _ s t a t u s  
         W H E R E   d a t a b a s e _ i d   =   @ C u r r e n t D a t a b a s e I D  
  
         S E T   @ C u r r e n t B a c k u p T y p e   =   @ B a c k u p T y p e  
  
         I F   @ C h a n g e B a c k u p T y p e   =   ' Y '  
         B E G I N  
             I F   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   A N D   D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' R e c o v e r y ' )   < >   ' S I M P L E '   A N D   @ C u r r e n t L o g L S N   I S   N U L L   A N D   @ C u r r e n t D a t a b a s e N a m e   < >   ' m a s t e r '  
             B E G I N  
                 S E T   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '  
             E N D  
             I F   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   A N D   @ C u r r e n t D i f f e r e n t i a l L S N   I S   N U L L   A N D   @ C u r r e n t D a t a b a s e N a m e   < >   ' m a s t e r '  
             B E G I N  
                 S E T   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '  
             E N D  
         E N D  
  
         I F   @ C u r r e n t B a c k u p T y p e   =   ' L O G '  
         B E G I N  
             S E L E C T   @ C u r r e n t L a t e s t B a c k u p   =   M A X ( b a c k u p _ f i n i s h _ d a t e )  
             F R O M   m s d b . d b o . b a c k u p s e t  
             W H E R E   [ t y p e ]   I N ( ' D ' , ' I ' )  
             A N D   i s _ c o p y _ o n l y   =   0  
             A N D   i s _ s n a p s h o t   =   0  
             A N D   i s _ d a m a g e d   =   0  
             A N D   d a t a b a s e _ n a m e   =   @ C u r r e n t D a t a b a s e N a m e  
         E N D  
  
         - -   S e t   d a t a b a s e   m e s s a g e  
         S E T   @ D a t a b a s e M e s s a g e   =   ' D a t e T i m e :   '   +   C O N V E R T ( n v a r c h a r , G E T D A T E ( ) , 1 2 0 )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' D a t a b a s e :   '   +   Q U O T E N A M E ( @ C u r r e n t D a t a b a s e N a m e )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' S t a t u s :   '   +   C A S T ( D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' S t a t u s ' )   A S   n v a r c h a r )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' M i r r o r i n g   r o l e :   '   +   I S N U L L ( @ C u r r e n t M i r r o r i n g R o l e , ' N / A ' )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' S t a n d b y :   '   +   C A S E   W H E N   D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' I s I n S t a n d B y ' )   =   1   T H E N   ' Y e s '   E L S E   ' N o '   E N D   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' U p d a t e a b i l i t y :   '   +   C A S T ( D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' U p d a t e a b i l i t y ' )   A S   n v a r c h a r )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' U s e r   a c c e s s :   '   +   C A S T ( D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' U s e r A c c e s s ' )   A S   n v a r c h a r )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' I s   a c c e s s i b l e :   '   +   C A S E   W H E N   @ C u r r e n t I s D a t a b a s e A c c e s s i b l e   =   1   T H E N   ' Y e s '   E L S E   ' N o '   E N D   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' R e c o v e r y   m o d e l :   '   +   C A S T ( D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' R e c o v e r y ' )   A S   n v a r c h a r )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' D i f f e r e n t i a l   b a s e   L S N :   '   +   I S N U L L ( C A S T ( @ C u r r e n t D i f f e r e n t i a l L S N   A S   n v a r c h a r ) , ' N / A ' )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
         S E T   @ D a t a b a s e M e s s a g e   =   @ D a t a b a s e M e s s a g e   +   ' L a s t   l o g   b a c k u p   L S N :   '   +   I S N U L L ( C A S T ( @ C u r r e n t L o g L S N   A S   n v a r c h a r ) , ' N / A ' )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   '   '  
         S E T   @ D a t a b a s e M e s s a g e   =   R E P L A C E ( @ D a t a b a s e M e s s a g e , ' % ' , ' % % ' )  
         R A I S E R R O R ( @ D a t a b a s e M e s s a g e , 1 0 , 1 )   W I T H   N O W A I T  
  
         I F   D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' S t a t u s ' )   =   ' O N L I N E '  
         A N D   N O T   ( D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' U s e r A c c e s s ' )   =   ' S I N G L E _ U S E R '   A N D   @ C u r r e n t I s D a t a b a s e A c c e s s i b l e   =   0 )  
         A N D   D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' I s I n S t a n d B y ' )   =   0  
         A N D   N O T   ( @ C u r r e n t B a c k u p T y p e   =   ' L O G '   A N D   ( D A T A B A S E P R O P E R T Y E X ( @ C u r r e n t D a t a b a s e N a m e , ' R e c o v e r y ' )   =   ' S I M P L E '   O R   @ C u r r e n t L o g L S N   I S   N U L L ) )  
         A N D   N O T   ( @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   A N D   @ C u r r e n t D i f f e r e n t i a l L S N   I S   N U L L )  
         A N D   N O T   ( @ C u r r e n t B a c k u p T y p e   I N ( ' D I F F ' , ' L O G ' )   A N D   @ C u r r e n t D a t a b a s e N a m e   =   ' m a s t e r ' )  
         B E G I N  
  
             - -   S e t   v a r i a b l e s  
             S E T   @ C u r r e n t D a t e   =   G E T D A T E ( )  
  
             I F   @ C l e a n u p T i m e   I S   N U L L   O R   ( @ C u r r e n t B a c k u p T y p e   =   ' L O G '   A N D   @ C u r r e n t L a t e s t B a c k u p   I S   N U L L )  
             B E G I N  
                 S E T   @ C u r r e n t C l e a n u p D a t e   =   N U L L  
             E N D  
             E L S E  
             I F   @ C u r r e n t B a c k u p T y p e   =   ' L O G '  
             B E G I N  
                 S E T   @ C u r r e n t C l e a n u p D a t e   =   ( S E L E C T   M I N ( [ D a t e ] )   F R O M ( S E L E C T   D A T E A D D ( h h , - ( @ C l e a n u p T i m e ) , @ C u r r e n t D a t e )   A S   [ D a t e ]   U N I O N   S E L E C T   @ C u r r e n t L a t e s t B a c k u p   A S   [ D a t e ] )   D a t e s )  
             E N D  
             E L S E  
             B E G I N  
                 S E T   @ C u r r e n t C l e a n u p D a t e   =   D A T E A D D ( h h , - ( @ C l e a n u p T i m e ) , @ C u r r e n t D a t e )  
             E N D  
  
             S E T   @ C u r r e n t D a t a b a s e N a m e F S   =   R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( R E P L A C E ( @ C u r r e n t D a t a b a s e N a m e , ' \ ' , ' ' ) , ' / ' , ' ' ) , ' : ' , ' ' ) , ' * ' , ' ' ) , ' ? ' , ' ' ) , ' " ' , ' ' ) , ' < ' , ' ' ) , ' > ' , ' ' ) , ' | ' , ' ' ) , '   ' , ' ' )  
  
             S E L E C T   @ C u r r e n t F i l e E x t e n s i o n   =   C A S E  
             W H E N   @ B a c k u p S o f t w a r e   I S   N U L L   A N D   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   T H E N   ' b a k '  
             W H E N   @ B a c k u p S o f t w a r e   I S   N U L L   A N D   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   T H E N   ' b a k '  
             W H E N   @ B a c k u p S o f t w a r e   I S   N U L L   A N D   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' t r n '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   T H E N   ' b a k '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   T H E N   ' b a k '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' t r n '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   T H E N   ' s q b '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   T H E N   ' s q b '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' s q b '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   A N D   @ E n c r y p t   =   ' N '   T H E N   ' h b c '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   A N D   @ E n c r y p t   =   ' N '   T H E N   ' h b c '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   A N D   @ E n c r y p t   =   ' N '   T H E N   ' h b c '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   A N D   @ E n c r y p t   =   ' Y '   T H E N   ' h b e '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   A N D   @ E n c r y p t   =   ' Y '   T H E N   ' h b e '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   A N D   @ E n c r y p t   =   ' Y '   T H E N   ' h b e '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   T H E N   ' s a f e '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   T H E N   ' s a f e '  
             W H E N   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '   A N D   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' s a f e '  
             E N D  
  
             S E T   @ C u r r e n t D i r e c t o r y   =   @ D i r e c t o r y   +   C A S E   W H E N   R I G H T ( @ D i r e c t o r y , 1 )   =   ' \ '   T H E N   ' '   E L S E   ' \ '   E N D   +   R E P L A C E ( C A S T ( S E R V E R P R O P E R T Y ( ' s e r v e r n a m e ' )   A S   n v a r c h a r ) , ' \ ' , ' $ ' )   +   ' \ '   +   @ C u r r e n t D a t a b a s e N a m e F S   +   ' \ '   +   U P P E R ( @ C u r r e n t B a c k u p T y p e )   +   C A S E   W H E N   @ C o p y O n l y   =   ' Y '   T H E N   ' _ C O P Y _ O N L Y '   E L S E   ' '   E N D  
  
             S E T   @ C u r r e n t F i l e N u m b e r   =   0  
  
             W H I L E   @ C u r r e n t F i l e N u m b e r   <   @ N u m b e r O f F i l e s  
             B E G I N  
                 S E T   @ C u r r e n t F i l e N u m b e r   =   @ C u r r e n t F i l e N u m b e r   +   1  
  
                 S E T   @ C u r r e n t F i l e P a t h   =   @ C u r r e n t D i r e c t o r y   +   ' \ '   +   R E P L A C E ( C A S T ( S E R V E R P R O P E R T Y ( ' s e r v e r n a m e ' )   A S   n v a r c h a r ) , ' \ ' , ' $ ' )   +   ' _ '   +   @ C u r r e n t D a t a b a s e N a m e F S   +   ' _ '   +   U P P E R ( @ C u r r e n t B a c k u p T y p e )   +   C A S E   W H E N   @ C o p y O n l y   =   ' Y '   T H E N   ' _ C O P Y _ O N L Y '   E L S E   ' '   E N D   +   ' _ '   +   R E P L A C E ( R E P L A C E ( R E P L A C E ( ( C O N V E R T ( n v a r c h a r , @ C u r r e n t D a t e , 1 2 0 ) ) , ' - ' , ' ' ) , '   ' , ' _ ' ) , ' : ' , ' ' )   +   C A S E   W H E N   @ N u m b e r O f F i l e s   >   1   A N D   @ N u m b e r O f F i l e s   < =   9   T H E N   ' _ '   +   C A S T ( @ C u r r e n t F i l e N u m b e r   A S   n v a r c h a r )   W H E N   @ N u m b e r O f F i l e s   > =   1 0   T H E N   ' _ '   +   R I G H T ( ' 0 '   +   C A S T ( @ C u r r e n t F i l e N u m b e r   A S   n v a r c h a r ) , 2 )   E L S E   ' '   E N D   +   ' . '   +   @ C u r r e n t F i l e E x t e n s i o n  
  
                 I F   L E N ( @ C u r r e n t F i l e P a t h )   >   2 5 9  
                 B E G I N  
                     S E T   @ C u r r e n t F i l e P a t h   =   @ C u r r e n t D i r e c t o r y   +   ' \ '   +   R E P L A C E ( C A S T ( S E R V E R P R O P E R T Y ( ' s e r v e r n a m e ' )   A S   n v a r c h a r ) , ' \ ' , ' $ ' )   +   ' _ '   +   L E F T ( @ C u r r e n t D a t a b a s e N a m e F S , C A S E   W H E N   ( L E N ( @ C u r r e n t D a t a b a s e N a m e F S )   +   2 5 9   -   L E N ( @ C u r r e n t F i l e P a t h )   -   3 )   <   2 0   T H E N   2 0   E L S E   ( L E N ( @ C u r r e n t D a t a b a s e N a m e F S )   +   2 5 9   -   L E N ( @ C u r r e n t F i l e P a t h )   -   3 )   E N D )   +   ' . . . '   +   ' _ '   +   U P P E R ( @ C u r r e n t B a c k u p T y p e )   +   C A S E   W H E N   @ C o p y O n l y   =   ' Y '   T H E N   ' _ C O P Y _ O N L Y '   E L S E   ' '   E N D   +   ' _ '   +   R E P L A C E ( R E P L A C E ( R E P L A C E ( ( C O N V E R T ( n v a r c h a r , @ C u r r e n t D a t e , 1 2 0 ) ) , ' - ' , ' ' ) , '   ' , ' _ ' ) , ' : ' , ' ' )   +   C A S E   W H E N   @ N u m b e r O f F i l e s   >   1   A N D   @ N u m b e r O f F i l e s   < =   9   T H E N   ' _ '   +   C A S T ( @ C u r r e n t F i l e N u m b e r   A S   n v a r c h a r )   W H E N   @ N u m b e r O f F i l e s   > =   1 0   T H E N   ' _ '   +   R I G H T ( ' 0 '   +   C A S T ( @ C u r r e n t F i l e N u m b e r   A S   n v a r c h a r ) , 2 )   E L S E   ' '   E N D   +   ' . '   +   @ C u r r e n t F i l e E x t e n s i o n  
                 E N D  
  
                 I N S E R T   I N T O   @ C u r r e n t F i l e s   ( C u r r e n t F i l e P a t h )  
                 S E L E C T   @ C u r r e n t F i l e P a t h  
             E N D  
  
             - -   C r e a t e   d i r e c t o r y  
             S E T   @ C u r r e n t C o m m a n d T y p e 0 1   =   ' x p _ c r e a t e _ s u b d i r '  
             S E T   @ C u r r e n t C o m m a n d 0 1   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ c r e a t e _ s u b d i r   N ' ' '   +   R E P L A C E ( @ C u r r e n t D i r e c t o r y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   c r e a t i n g   d i r e c t o r y . ' ' ,   1 6 ,   1 ) '  
             E X E C U T E   @ C u r r e n t C o m m a n d O u t p u t 0 1   =   [ d b o ] . [ C o m m a n d E x e c u t e ]   @ C o m m a n d   =   @ C u r r e n t C o m m a n d 0 1 ,   @ C o m m a n d T y p e   =   @ C u r r e n t C o m m a n d T y p e 0 1 ,   @ M o d e   =   1 ,   @ D a t a b a s e N a m e   =   @ C u r r e n t D a t a b a s e N a m e ,   @ L o g T o T a b l e   =   @ L o g T o T a b l e ,   @ E x e c u t e   =   @ E x e c u t e  
             S E T   @ E r r o r   =   @ @ E R R O R  
             I F   @ E r r o r   < >   0   S E T   @ C u r r e n t C o m m a n d O u t p u t 0 1   =   @ E r r o r  
             I F   @ C u r r e n t C o m m a n d O u t p u t 0 1   < >   0   S E T   @ R e t u r n C o d e   =   @ C u r r e n t C o m m a n d O u t p u t 0 1  
  
             - -   P e r f o r m   a   b a c k u p  
             I F   @ C u r r e n t C o m m a n d O u t p u t 0 1   =   0  
             B E G I N  
                 I F   @ B a c k u p S o f t w a r e   I S   N U L L  
                 B E G I N  
                     S E L E C T   @ C u r r e n t C o m m a n d T y p e 0 2   =   C A S E  
                     W H E N   @ C u r r e n t B a c k u p T y p e   I N ( ' D I F F ' , ' F U L L ' )   T H E N   ' B A C K U P _ D A T A B A S E '  
                     W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' B A C K U P _ L O G '  
                     E N D  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   C A S E  
                     W H E N   @ C u r r e n t B a c k u p T y p e   I N ( ' D I F F ' , ' F U L L ' )   T H E N   ' B A C K U P   D A T A B A S E   '   +   Q U O T E N A M E ( @ C u r r e n t D a t a b a s e N a m e )   +   '   T O '  
                     W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' B A C K U P   L O G   '   +   Q U O T E N A M E ( @ C u r r e n t D a t a b a s e N a m e )   +   '   T O '  
                     E N D  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   D I S K   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   < >   @ N u m b e r O f F i l e s   T H E N   ' , '   E L S E   ' '   E N D  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   W I T H   '  
                     I F   @ C h e c k S u m   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' C H E C K S U M '  
                     I F   @ C h e c k S u m   =   ' N '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' N O _ C H E C K S U M '  
                     I F   @ C o m p r e s s   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   C O M P R E S S I O N '  
                     I F   @ C o m p r e s s   =   ' N '   A N D   @ V e r s i o n   > =   1 0   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   N O _ C O M P R E S S I O N '  
                     I F   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   D I F F E R E N T I A L '  
                     I F   @ C o p y O n l y   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   C O P Y _ O N L Y '  
                     I F   @ B l o c k S i z e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   B L O C K S I Z E   =   '   +   C A S T ( @ B l o c k S i z e   A S   n v a r c h a r )  
                     I F   @ B u f f e r C o u n t   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   B U F F E R C O U N T   =   '   +   C A S T ( @ B u f f e r C o u n t   A S   n v a r c h a r )  
                     I F   @ M a x T r a n s f e r S i z e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   M A X T R A N S F E R S I Z E   =   '   +   C A S T ( @ M a x T r a n s f e r S i z e   A S   n v a r c h a r )  
                     I F   @ D e s c r i p t i o n   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   D E S C R I P T I O N   =   N ' ' '   +   R E P L A C E ( @ D e s c r i p t i o n , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '  
                 B E G I N  
                     S E L E C T   @ C u r r e n t C o m m a n d T y p e 0 2   =   C A S E  
                     W H E N   @ C u r r e n t B a c k u p T y p e   I N ( ' D I F F ' , ' F U L L ' )   T H E N   ' x p _ b a c k u p _ d a t a b a s e '  
                     W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' x p _ b a c k u p _ l o g '  
                     E N D  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   C A S E  
                     W H E N   @ C u r r e n t B a c k u p T y p e   I N ( ' D I F F ' , ' F U L L ' )   T H E N   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ b a c k u p _ d a t a b a s e   @ d a t a b a s e   =   N ' ' '   +   R E P L A C E ( @ C u r r e n t D a t a b a s e N a m e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ b a c k u p _ l o g   @ d a t a b a s e   =   N ' ' '   +   R E P L A C E ( @ C u r r e n t D a t a b a s e N a m e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     E N D  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ f i l e n a m e   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ w i t h   =   ' ' '  
                     I F   @ C h e c k S u m   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' C H E C K S U M '  
                     I F   @ C h e c k S u m   =   ' N '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' N O _ C H E C K S U M '  
                     I F   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   D I F F E R E N T I A L '  
                     I F   @ C o p y O n l y   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   C O P Y _ O N L Y '  
                     I F   @ B l o c k S i z e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   B L O C K S I Z E   =   '   +   C A S T ( @ B l o c k S i z e   A S   n v a r c h a r )  
                     I F   @ B u f f e r C o u n t   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   B U F F E R C O U N T   =   '   +   C A S T ( @ B u f f e r C o u n t   A S   n v a r c h a r )  
                     I F   @ M a x T r a n s f e r S i z e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   M A X T R A N S F E R S I Z E   =   '   +   C A S T ( @ M a x T r a n s f e r S i z e   A S   n v a r c h a r )  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ' ' '  
                     I F   @ C o m p r e s s i o n L e v e l   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ c o m p r e s s i o n l e v e l   =   '   +   C A S T ( @ C o m p r e s s i o n L e v e l   A S   n v a r c h a r )  
                     I F   @ T h r e a d s   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ t h r e a d s   =   '   +   C A S T ( @ T h r e a d s   A S   n v a r c h a r )  
                     I F   @ T h r o t t l e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ t h r o t t l e   =   '   +   C A S T ( @ T h r o t t l e   A S   n v a r c h a r )  
                     I F   @ D e s c r i p t i o n   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ d e s c   =   N ' ' '   +   R E P L A C E ( @ D e s c r i p t i o n , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
  
                     I F   @ E n c r y p t i o n T y p e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ c r y p t l e v e l   =   '   +   C A S E  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' R C 2 - 4 0 '   T H E N   ' 0 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' R C 2 - 5 6 '   T H E N   ' 1 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' R C 2 - 1 1 2 '   T H E N   ' 2 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' R C 2 - 1 2 8 '   T H E N   ' 3 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' 3 D E S - 1 6 8 '   T H E N   ' 4 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' R C 4 - 1 2 8 '   T H E N   ' 5 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' A E S - 1 2 8 '   T H E N   ' 6 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' A E S - 1 9 2 '   T H E N   ' 7 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' A E S - 2 5 6 '   T H E N   ' 8 '  
                     E N D  
  
                     I F   @ E n c r y p t i o n K e y   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ e n c r y p t i o n k e y   =   N ' ' '   +   R E P L A C E ( @ E n c r y p t i o n K e y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   p e r f o r m i n g   L i t e S p e e d   b a c k u p . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 2   =   ' s q l b a c k u p '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   C A S E  
                     W H E N   @ C u r r e n t B a c k u p T y p e   I N ( ' D I F F ' , ' F U L L ' )   T H E N   ' B A C K U P   D A T A B A S E   '   +   Q U O T E N A M E ( @ C u r r e n t D a t a b a s e N a m e )   +   '   T O '  
                     W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' B A C K U P   L O G   '   +   Q U O T E N A M E ( @ C u r r e n t D a t a b a s e N a m e )   +   '   T O '  
                     E N D  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   D I S K   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   < >   @ N u m b e r O f F i l e s   T H E N   ' , '   E L S E   ' '   E N D  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   W I T H   '  
                     I F   @ C h e c k S u m   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' C H E C K S U M '  
                     I F   @ C h e c k S u m   =   ' N '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' N O _ C H E C K S U M '  
                     I F   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   D I F F E R E N T I A L '  
                     I F   @ C o p y O n l y   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   C O P Y _ O N L Y '  
                     I F   @ C o m p r e s s i o n L e v e l   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   C O M P R E S S I O N   =   '   +   C A S T ( @ C o m p r e s s i o n L e v e l   A S   n v a r c h a r )  
                     I F   @ T h r e a d s   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   T H R E A D C O U N T   =   '   +   C A S T ( @ T h r e a d s   A S   n v a r c h a r )  
                     I F   @ D e s c r i p t i o n   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   D E S C R I P T I O N   =   N ' ' '   +   R E P L A C E ( @ D e s c r i p t i o n , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
  
                     I F   @ E n c r y p t i o n T y p e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   K E Y S I Z E   =   '   +   C A S E  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' A E S - 1 2 8 '   T H E N   ' 1 2 8 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' A E S - 2 5 6 '   T H E N   ' 2 5 6 '  
                     E N D  
  
                     I F   @ E n c r y p t i o n K e y   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   P A S S W O R D   =   N ' ' '   +   R E P L A C E ( @ E n c r y p t i o n K e y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . s q l b a c k u p   N ' ' - S Q L   " '   +   R E P L A C E ( @ C u r r e n t C o m m a n d 0 2 , ' ' ' ' , ' ' ' ' ' ' )   +   ' " ' ' '   +   '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   p e r f o r m i n g   S Q L B a c k u p   b a c k u p . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 2   =   ' B A C K U P _ D A T A B A S E '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   C A S E  
                     W H E N   @ C u r r e n t B a c k u p T y p e   I N ( ' D I F F ' , ' F U L L ' )   T H E N   ' B A C K U P   D A T A B A S E   '   +   Q U O T E N A M E ( @ C u r r e n t D a t a b a s e N a m e )   +   '   T O '  
                     W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' B A C K U P   L O G   '   +   Q U O T E N A M E ( @ C u r r e n t D a t a b a s e N a m e )   +   '   T O '  
                     E N D  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   D I S K   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   < >   @ N u m b e r O f F i l e s   T H E N   ' , '   E L S E   ' '   E N D  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   W I T H   '  
                     I F   @ C h e c k S u m   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' C H E C K S U M '  
                     I F   @ C h e c k S u m   =   ' N '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' N O _ C H E C K S U M '  
                     I F   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   D I F F E R E N T I A L '  
                     I F   @ C o p y O n l y   =   ' Y '   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   C O P Y _ O N L Y '  
                     I F   @ B l o c k S i z e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   B L O C K S I Z E   =   '   +   C A S T ( @ B l o c k S i z e   A S   n v a r c h a r )  
                     I F   @ B u f f e r C o u n t   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   B U F F E R C O U N T   =   '   +   C A S T ( @ B u f f e r C o u n t   A S   n v a r c h a r )  
                     I F   @ M a x T r a n s f e r S i z e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   M A X T R A N S F E R S I Z E   =   '   +   C A S T ( @ M a x T r a n s f e r S i z e   A S   n v a r c h a r )  
                     I F   @ D e s c r i p t i o n   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   D E S C R I P T I O N   =   N ' ' '   +   R E P L A C E ( @ D e s c r i p t i o n , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 2   =   ' x p _ s s _ b a c k u p '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ s s _ b a c k u p   @ d a t a b a s e   =   N ' ' '   +   R E P L A C E ( @ C u r r e n t D a t a b a s e N a m e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   =   1   T H E N   ' @ f i l e n a m e '   E L S E   ' @ b a c k u p f i l e '   E N D   +   '   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ b a c k u p t y p e   =   '   +   C A S E   W H E N   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   T H E N   ' ' ' F u l l ' ' '   W H E N   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   T H E N   ' ' ' D i f f e r e n t i a l ' ' '   W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' ' ' L o g ' ' '   E N D  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ c h e c k s u m   =   '   +   C A S E   W H E N   @ C h e c k S u m   =   ' Y '   T H E N   ' 1 '   W H E N   @ C h e c k S u m   =   ' N '   T H E N   ' 0 '   E N D  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ c o p y o n l y   =   '   +   C A S E   W H E N   @ C o p y O n l y   =   ' Y '   T H E N   ' 1 '   W H E N   @ C o p y O n l y   =   ' N '   T H E N   ' 0 '   E N D  
                     I F   @ C o m p r e s s i o n L e v e l   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ c o m p r e s s i o n l e v e l   =   '   +   C A S T ( @ C o m p r e s s i o n L e v e l   A S   n v a r c h a r )  
                     I F   @ T h r e a d s   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ t h r e a d s   =   '   +   C A S T ( @ T h r e a d s   A S   n v a r c h a r )  
                     I F   @ D e s c r i p t i o n   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ d e s c   =   N ' ' '   +   R E P L A C E ( @ D e s c r i p t i o n , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
  
                     I F   @ E n c r y p t i o n T y p e   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ e n c r y p t i o n t y p e   =   N ' ' '   +   C A S E  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' A E S - 1 2 8 '   T H E N   ' A E S 1 2 8 '  
                     W H E N   @ E n c r y p t i o n T y p e   =   ' A E S - 2 5 6 '   T H E N   ' A E S 2 5 6 '  
                     E N D   +   ' ' ' '  
  
                     I F   @ E n c r y p t i o n K e y   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   ' ,   @ e n c r y p t e d b a c k u p p a s s w o r d   =   N ' ' '   +   R E P L A C E ( @ E n c r y p t i o n K e y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     S E T   @ C u r r e n t C o m m a n d 0 2   =   @ C u r r e n t C o m m a n d 0 2   +   '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   p e r f o r m i n g   S Q L s a f e   b a c k u p . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 E X E C U T E   @ C u r r e n t C o m m a n d O u t p u t 0 2   =   [ d b o ] . [ C o m m a n d E x e c u t e ]   @ C o m m a n d   =   @ C u r r e n t C o m m a n d 0 2 ,   @ C o m m a n d T y p e   =   @ C u r r e n t C o m m a n d T y p e 0 2 ,   @ M o d e   =   1 ,   @ D a t a b a s e N a m e   =   @ C u r r e n t D a t a b a s e N a m e ,   @ L o g T o T a b l e   =   @ L o g T o T a b l e ,   @ E x e c u t e   =   @ E x e c u t e  
                 S E T   @ E r r o r   =   @ @ E R R O R  
                 I F   @ E r r o r   < >   0   S E T   @ C u r r e n t C o m m a n d O u t p u t 0 2   =   @ E r r o r  
                 I F   @ C u r r e n t C o m m a n d O u t p u t 0 2   < >   0   S E T   @ R e t u r n C o d e   =   @ C u r r e n t C o m m a n d O u t p u t 0 2  
             E N D  
  
             - -   V e r i f y   t h e   b a c k u p  
             I F   @ C u r r e n t C o m m a n d O u t p u t 0 2   =   0   A N D   @ V e r i f y   =   ' Y '  
             B E G I N  
                 I F   @ B a c k u p S o f t w a r e   I S   N U L L  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 3   =   ' R E S T O R E _ V E R I F Y O N L Y '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   ' R E S T O R E   V E R I F Y O N L Y   F R O M '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   '   D I S K   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   < >   @ N u m b e r O f F i l e s   T H E N   ' , '   E L S E   ' '   E N D  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 3   =   ' x p _ r e s t o r e _ v e r i f y o n l y '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ r e s t o r e _ v e r i f y o n l y '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   '   @ f i l e n a m e   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   < >   @ N u m b e r O f F i l e s   T H E N   ' , '   E L S E   ' '   E N D  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     I F   @ E n c r y p t i o n K e y   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   ' ,   @ e n c r y p t i o n k e y   =   N ' ' '   +   R E P L A C E ( @ E n c r y p t i o n K e y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   v e r i f y i n g   L i t e S p e e d   b a c k u p . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 3   =   ' s q l b a c k u p '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   ' R E S T O R E   V E R I F Y O N L Y   F R O M '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   '   D I S K   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   < >   @ N u m b e r O f F i l e s   T H E N   ' , '   E L S E   ' '   E N D  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     I F   @ E n c r y p t i o n K e y   I S   N O T   N U L L   S E T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   '   W I T H   P A S S W O R D   =   N ' ' '   +   R E P L A C E ( @ E n c r y p t i o n K e y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . s q l b a c k u p   N ' ' - S Q L   " '   +   R E P L A C E ( @ C u r r e n t C o m m a n d 0 3 , ' ' ' ' , ' ' ' ' ' ' )   +   ' " ' ' '   +   '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   v e r i f y i n g   S Q L B a c k u p   b a c k u p . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 3   =   ' R E S T O R E _ V E R I F Y O N L Y '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   ' R E S T O R E   V E R I F Y O N L Y   F R O M '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   '   D I S K   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   < >   @ N u m b e r O f F i l e s   T H E N   ' , '   E L S E   ' '   E N D  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 3   =   ' x p _ s s _ v e r i f y '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ s s _ v e r i f y   @ d a t a b a s e   =   N ' ' '   +   R E P L A C E ( @ C u r r e n t D a t a b a s e N a m e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
  
                     S E L E C T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   ' ,   '   +   C A S E   W H E N   R O W _ N U M B E R ( )   O V E R   ( O R D E R   B Y   C u r r e n t F i l e P a t h   A S C )   =   1   T H E N   ' @ f i l e n a m e '   E L S E   ' @ b a c k u p f i l e '   E N D   +   '   =   N ' ' '   +   R E P L A C E ( C u r r e n t F i l e P a t h , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' '  
                     F R O M   @ C u r r e n t F i l e s  
                     O R D E R   B Y   C u r r e n t F i l e P a t h   A S C  
  
                     S E T   @ C u r r e n t C o m m a n d 0 3   =   @ C u r r e n t C o m m a n d 0 3   +   '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   v e r i f y i n g   S Q L s a f e   b a c k u p . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 E X E C U T E   @ C u r r e n t C o m m a n d O u t p u t 0 3   =   [ d b o ] . [ C o m m a n d E x e c u t e ]   @ C o m m a n d   =   @ C u r r e n t C o m m a n d 0 3 ,   @ C o m m a n d T y p e   =   @ C u r r e n t C o m m a n d T y p e 0 3 ,   @ M o d e   =   1 ,   @ D a t a b a s e N a m e   =   @ C u r r e n t D a t a b a s e N a m e ,   @ L o g T o T a b l e   =   @ L o g T o T a b l e ,   @ E x e c u t e   =   @ E x e c u t e  
                 S E T   @ E r r o r   =   @ @ E R R O R  
                 I F   @ E r r o r   < >   0   S E T   @ C u r r e n t C o m m a n d O u t p u t 0 3   =   @ E r r o r  
                 I F   @ C u r r e n t C o m m a n d O u t p u t 0 3   < >   0   S E T   @ R e t u r n C o d e   =   @ C u r r e n t C o m m a n d O u t p u t 0 3  
             E N D  
  
             - -   D e l e t e   o l d   b a c k u p   f i l e s  
             I F   ( @ C u r r e n t C o m m a n d O u t p u t 0 2   =   0   A N D   @ V e r i f y   =   ' N '   A N D   @ C u r r e n t C l e a n u p D a t e   I S   N O T   N U L L )  
             O R   ( @ C u r r e n t C o m m a n d O u t p u t 0 2   =   0   A N D   @ V e r i f y   =   ' Y '   A N D   @ C u r r e n t C o m m a n d O u t p u t 0 3   =   0   A N D   @ C u r r e n t C l e a n u p D a t e   I S   N O T   N U L L )  
             B E G I N  
                 I F   @ B a c k u p S o f t w a r e   I S   N U L L  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 4   =   ' x p _ d e l e t e _ f i l e '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 4   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ d e l e t e _ f i l e   0 ,   N ' ' '   +   R E P L A C E ( @ C u r r e n t D i r e c t o r y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ,   ' ' '   +   @ C u r r e n t F i l e E x t e n s i o n   +   ' ' ' ,   ' ' '   +   C O N V E R T ( n v a r c h a r ( 1 9 ) , @ C u r r e n t C l e a n u p D a t e , 1 2 6 )   +   ' ' '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   d e l e t i n g   f i l e s . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' L I T E S P E E D '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 4   =   ' x p _ s l s s q l m a i n t '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 4   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ s l s s q l m a i n t   N ' ' - M A I N T D E L   - D E L F O L D E R   " '   +   R E P L A C E ( @ C u r r e n t D i r e c t o r y , ' ' ' ' , ' ' ' ' ' ' )   +   ' "   - D E L E X T E N S I O N   " '   +   @ C u r r e n t F i l e E x t e n s i o n   +   ' "   - D E L U N I T   " '   +   C A S T ( D A T E D I F F ( m i , @ C u r r e n t C l e a n u p D a t e , G E T D A T E ( ) )   +   1   A S   n v a r c h a r )   +   ' "   - D E L U N I T T Y P E   " m i n u t e s "   - D E L U S E A G E ' '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   d e l e t i n g   L i t e S p e e d   b a c k u p   f i l e s . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' S Q L B A C K U P '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 4   =   ' s q b u t i l i t y '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 4   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . s q b u t i l i t y   1 0 3 2 ,   N ' ' '   +   R E P L A C E ( @ C u r r e n t D a t a b a s e N a m e , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ,   N ' ' '   +   R E P L A C E ( @ C u r r e n t D i r e c t o r y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ,   ' ' '   +   C A S E   W H E N   @ C u r r e n t B a c k u p T y p e   =   ' F U L L '   T H E N   ' D '   W H E N   @ C u r r e n t B a c k u p T y p e   =   ' D I F F '   T H E N   ' I '   W H E N   @ C u r r e n t B a c k u p T y p e   =   ' L O G '   T H E N   ' L '   E N D   +   ' ' ' ,   ' ' '   +   C A S T ( D A T E D I F F ( h h , @ C u r r e n t C l e a n u p D a t e , G E T D A T E ( ) )   +   1   A S   n v a r c h a r )   +   ' h ' ' ,   '   +   I S N U L L ( ' ' ' '   +   R E P L A C E ( @ E n c r y p t i o n K e y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ' , ' N U L L ' )   +   '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   d e l e t i n g   S Q L B a c k u p   b a c k u p   f i l e s . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' H Y P E R B A C '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 4   =   ' x p _ d e l e t e _ f i l e '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 4   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ d e l e t e _ f i l e   0 ,   N ' ' '   +   R E P L A C E ( @ C u r r e n t D i r e c t o r y , ' ' ' ' , ' ' ' ' ' ' )   +   ' ' ' ,   ' ' '   +   @ C u r r e n t F i l e E x t e n s i o n   +   ' ' ' ,   ' ' '   +   C O N V E R T ( n v a r c h a r ( 1 9 ) , @ C u r r e n t C l e a n u p D a t e , 1 2 6 )   +   ' ' '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   d e l e t i n g   f i l e s . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 I F   @ B a c k u p S o f t w a r e   =   ' S Q L S A F E '  
                 B E G I N  
                     S E T   @ C u r r e n t C o m m a n d T y p e 0 4   =   ' x p _ s s _ d e l e t e '  
  
                     S E T   @ C u r r e n t C o m m a n d 0 4   =   ' D E C L A R E   @ R e t u r n C o d e   i n t   E X E C U T E   @ R e t u r n C o d e   =   [ m a s t e r ] . d b o . x p _ s s _ d e l e t e   @ f i l e n a m e   =   N ' ' '   +   R E P L A C E ( @ C u r r e n t D i r e c t o r y , ' ' ' ' , ' ' ' ' ' ' )   +   ' \ * . '   +   @ C u r r e n t F i l e E x t e n s i o n   +   ' ' ' ,   @ a g e   =   ' ' '   +   C A S T ( D A T E D I F F ( m i , @ C u r r e n t C l e a n u p D a t e , G E T D A T E ( ) )   +   1   A S   n v a r c h a r )   +   ' M i n u t e s ' '   I F   @ R e t u r n C o d e   < >   0   R A I S E R R O R ( ' ' E r r o r   d e l e t i n g   S Q L s a f e   b a c k u p   f i l e s . ' ' ,   1 6 ,   1 ) '  
                 E N D  
  
                 E X E C U T E   @ C u r r e n t C o m m a n d O u t p u t 0 4   =   [ d b o ] . [ C o m m a n d E x e c u t e ]   @ C o m m a n d   =   @ C u r r e n t C o m m a n d 0 4 ,   @ C o m m a n d T y p e   =   @ C u r r e n t C o m m a n d T y p e 0 4 ,   @ M o d e   =   1 ,   @ D a t a b a s e N a m e   =   @ C u r r e n t D a t a b a s e N a m e ,   @ L o g T o T a b l e   =   @ L o g T o T a b l e ,   @ E x e c u t e   =   @ E x e c u t e  
                 S E T   @ E r r o r   =   @ @ E R R O R  
                 I F   @ E r r o r   < >   0   S E T   @ C u r r e n t C o m m a n d O u t p u t 0 4   =   @ E r r o r  
                 I F   @ C u r r e n t C o m m a n d O u t p u t 0 4   < >   0   S E T   @ R e t u r n C o d e   =   @ C u r r e n t C o m m a n d O u t p u t 0 4  
             E N D  
  
         E N D  
  
         - -   U p d a t e   t h a t   t h e   d a t a b a s e   i s   c o m p l e t e d  
         U P D A T E   @ t m p D a t a b a s e s  
         S E T   C o m p l e t e d   =   1  
         W H E R E   I D   =   @ C u r r e n t I D  
  
         - -   C l e a r   v a r i a b l e s  
         S E T   @ C u r r e n t I D   =   N U L L  
         S E T   @ C u r r e n t D a t a b a s e I D   =   N U L L  
         S E T   @ C u r r e n t D a t a b a s e N a m e   =   N U L L  
         S E T   @ C u r r e n t B a c k u p T y p e   =   N U L L  
         S E T   @ C u r r e n t F i l e E x t e n s i o n   =   N U L L  
         S E T   @ C u r r e n t F i l e N u m b e r   =   N U L L  
         S E T   @ C u r r e n t D i f f e r e n t i a l L S N   =   N U L L  
         S E T   @ C u r r e n t L o g L S N   =   N U L L  
         S E T   @ C u r r e n t L a t e s t B a c k u p   =   N U L L  
         S E T   @ C u r r e n t D a t a b a s e N a m e F S   =   N U L L  
         S E T   @ C u r r e n t D i r e c t o r y   =   N U L L  
         S E T   @ C u r r e n t F i l e P a t h   =   N U L L  
         S E T   @ C u r r e n t D a t e   =   N U L L  
         S E T   @ C u r r e n t C l e a n u p D a t e   =   N U L L  
         S E T   @ C u r r e n t I s D a t a b a s e A c c e s s i b l e   =   N U L L  
         S E T   @ C u r r e n t M i r r o r i n g R o l e   =   N U L L  
  
         S E T   @ C u r r e n t C o m m a n d 0 1   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d 0 2   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d 0 3   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d 0 4   =   N U L L  
  
         S E T   @ C u r r e n t C o m m a n d O u t p u t 0 1   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d O u t p u t 0 2   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d O u t p u t 0 3   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d O u t p u t 0 4   =   N U L L  
  
         S E T   @ C u r r e n t C o m m a n d T y p e 0 1   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d T y p e 0 2   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d T y p e 0 3   =   N U L L  
         S E T   @ C u r r e n t C o m m a n d T y p e 0 4   =   N U L L  
  
         D E L E T E   F R O M   @ C u r r e n t F i l e s  
  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   L o g   c o m p l e t i n g   i n f o r m a t i o n                                                                                                                                   / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     L o g g i n g :  
     S E T   @ E n d M e s s a g e   =   ' D a t e T i m e :   '   +   C O N V E R T ( n v a r c h a r , G E T D A T E ( ) , 1 2 0 )  
     S E T   @ E n d M e s s a g e   =   R E P L A C E ( @ E n d M e s s a g e , ' % ' , ' % % ' )  
     R A I S E R R O R ( @ E n d M e s s a g e , 1 0 , 1 )   W I T H   N O W A I T  
  
     I F   @ R e t u r n C o d e   < >   0  
     B E G I N  
         R E T U R N   @ R e t u r n C o d e  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
 E N D  
  
 G O  
 
