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