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