ÿþ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 ] . [ C o m m a n d E x e c u t e ]  
  
 @ C o m m a n d   n v a r c h a r ( m a x ) ,  
 @ C o m m a n d T y p e   n v a r c h a r ( m a x ) ,  
 @ M o d e   i n t ,  
 @ C o m m e n t   n v a r c h a r ( m a x )   =   N U L L ,  
 @ D a t a b a s e N a m e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ S c h e m a N a m e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ O b j e c t N a m e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ O b j e c t T y p e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ I n d e x N a m e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ I n d e x T y p e   i n t   =   N U L L ,  
 @ S t a t i s t i c s N a m e   n v a r c h a r ( m a x )   =   N U L L ,  
 @ P a r t i t i o n N u m b e r   i n t   =   N U L L ,  
 @ E x t e n d e d I n f o   x m l   =   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 ) ,  
 @ E x e c u t e   n v a r c h a r ( m a x )  
  
 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  
  
     S E T   L O C K _ T I M E O U T   3 6 0 0 0 0 0  
  
     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   @ 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   @ E r r o r M e s s a g e O r i g i n a l   n v a r c h a r ( m a x )  
  
     D E C L A R E   @ S t a r t T i m e   d a t e t i m e  
     D E C L A R E   @ E n d T i m e   d a t e t i m e  
  
     D E C L A R E   @ S t a r t T i m e S e c   d a t e t i m e  
     D E C L A R E   @ E n d T i m e S e c   d a t e t i m e  
  
     D E C L A R E   @ I D   i n t  
  
     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  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   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   R e t u r n C o d e  
     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   R e t u r n C o d e  
     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   @ C o m m a n d   I S   N U L L   O R   @ C o m m a n d   =   ' '  
     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 m a n d   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 m a n d T y p e   I S   N U L L   O R   @ C o m m a n d T y p e   =   ' '   O R   L E N ( @ C o m m a n d T y p e )   >   6 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 m a n d 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   @ M o d e   N O T   I N ( 1 , 2 )   O R   @ M o d 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   @ M o d 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   @ 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   @ R e t u r n C o d e   =   @ E r r o r  
         G O T O   R e t u r n C o d e  
     E N D  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   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 T i m e   =   G E T D A T E ( )  
     S E T   @ S t a r t T i m e S e c   =   C O N V E R T ( d a t e t i m e , C O N V E R T ( n v a r c h a r , @ S t a r t T i m e , 1 2 0 ) , 1 2 0 )  
  
     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 , @ S t a r t T i m e S e c , 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   +   ' C o m m a n d :   '   +   @ C o m m a n d  
     I F   @ C o m m e n t   I S   N O 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   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )   +   ' C o m m e n t :   '   +   @ C o m m e n t  
     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  
  
     I F   @ L o g T o T a b l e   =   ' Y '  
     B E G I N  
         I N S E R T   I N T O   d b o . C o m m a n d L o g   ( D a t a b a s e N a m e ,   S c h e m a N a m e ,   O b j e c t N a m e ,   O b j e c t T y p e ,   I n d e x N a m e ,   I n d e x T y p e ,   S t a t i s t i c s N a m e ,   P a r t i t i o n N u m b e r ,   E x t e n d e d I n f o ,   C o m m a n d T y p e ,   C o m m a n d ,   S t a r t T i m e )  
         V A L U E S   ( @ D a t a b a s e N a m e ,   @ S c h e m a N a m e ,   @ O b j e c t N a m e ,   @ O b j e c t T y p e ,   @ I n d e x N a m e ,   @ I n d e x T y p e ,   @ S t a t i s t i c s N a m e ,   @ P a r t i t i o n N u m b e r ,   @ E x t e n d e d I n f o ,   @ C o m m a n d T y p e ,   @ C o m m a n d ,   @ S t a r t T i m e )  
     E N D  
  
     S E T   @ I D   =   S C O P E _ I D E N T I T Y ( )  
  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
     - - / /   E x e c u t e   c o m m a n d                                                                                                                                                         / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     I F   @ M o d e   =   1   A N D   @ E x e c u t e   =   ' Y '  
     B E G I N  
         E X E C U T E ( @ C o m m a n d )  
         S E T   @ E r r o r   =   @ @ E R R O R  
         S E T   @ R e t u r n C o d e   =   @ E r r o r  
     E N D  
  
     I F   @ M o d e   =   2   A N D   @ E x e c u t e   =   ' Y '  
     B E G I N  
         B E G I N   T R Y  
             E X E C U T E ( @ C o m m a n d )  
         E N D   T R Y  
         B E G I N   C A T C H  
             S E T   @ E r r o r   =   E R R O R _ N U M B E R ( )  
             S E T   @ R e t u r n C o d e   =   @ E r r o r  
             S E T   @ E r r o r M e s s a g e O r i g i n a l   =   E R R O R _ M E S S A G E ( )  
             S E T   @ E r r o r M e s s a g e   =   ' M s g   '   +   C A S T ( @ E r r o r   A S   n v a r c h a r )   +   ' ,   '   +   I S N U L L ( @ E r r o r M e s s a g e O r i g i n a l , ' ' )  
             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  
         E N D   C A T C H  
     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                                                                                                                                   / / - -  
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     S E T   @ E n d T i m e   =   G E T D A T E ( )  
     S E T   @ E n d T i m e S e c   =   C O N V E R T ( d a t e t i m e , C O N V E R T ( v a r c h a r , @ E n d T i m e , 1 2 0 ) , 1 2 0 )  
  
     S E T   @ E n d M e s s a g e   =   ' O u t c o m e :   '   +   C A S E   W H E N   @ E x e c u t e   =   ' N '   T H E N   ' N o t   E x e c u t e d '   W H E N   @ E r r o r   =   0   T H E N   ' S u c c e e d e d '   E L S E   ' F a i l e d '   E N D   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ E n d M e s s a g e   =   @ E n d M e s s a g e   +   ' D u r a t i o n :   '   +   C A S E   W H E N   D A T E D I F F ( s s , @ S t a r t T i m e S e c ,   @ E n d T i m e S e c ) / ( 2 4 * 3 6 0 0 )   >   0   T H E N   C A S T ( D A T E D I F F ( s s , @ S t a r t T i m e S e c ,   @ E n d T i m e S e c ) / ( 2 4 * 3 6 0 0 )   A S   n v a r c h a r )   +   ' . '   E L S E   ' '   E N D   +   C O N V E R T ( n v a r c h a r , @ E n d T i m e S e c   -   @ S t a r t T i m e S e c , 1 0 8 )   +   C H A R ( 1 3 )   +   C H A R ( 1 0 )  
     S E T   @ E n d M e s s a g e   =   @ 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 , @ E n d T i m e S e c , 1 2 0 )   +   C H A R ( 1 3 )   +   C H A R ( 1 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   @ L o g T o T a b l e   =   ' Y '  
     B E G I N  
         U P D A T E   d b o . C o m m a n d L o g  
         S E T   E n d T i m e   =   @ E n d T i m e ,  
                 E r r o r N u m b e r   =   C A S E   W H E N   @ E x e c u t e   =   ' N '   T H E N   N U L L   E L S E   @ E r r o r   E N D ,  
                 E r r o r M e s s a g e   =   @ E r r o r M e s s a g e O r i g i n a l  
         W H E R E   I D   =   @ I D  
     E N D  
  
     R e t u r n C o d e :  
     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  
 
