Friday 19 July 2013

Create Stored Procedure using Case and Conditional Satatment in SQL Server 2008

Example:............for Stored Procedure...

Create proc [dbo].[sp_Order_Confirm]        
@Buy_Sell_Ind INT,        
@orderId NUMERIC(10,0),        
@price NUMERIC(9,2)=0,        
@code nCHAR(20),        
@cwhere VARCHAR(200)='',
@OptionType nchar(1)=''        
AS        
BEGIN        
 if @cwhere='RL'      
 begin      
 declare @sPrice numeric(9,2)    
 declare @bPrice numeric(9,2)  
 declare @ltp numeric(9,2)
 -------------------------------when price between Buy sell
   IF EXISTS(SELECT * FROM dbo.Product_Det WHERE ScripCode=@code AND @Buy_Sell_Ind=1 AND (convert(numeric(9,2),SellPrice)= convert(numeric(9,2),@price)OR convert(numeric(9,2),LastTradedPrice)= convert(numeric(9,2),@price))  OR ScripCode=@code AND @Buy_Sell_Ind=2 AND (convert(numeric(9,2),BuyPrice) = convert(numeric(9,2),@price)OR convert(numeric(9,2),LastTradedPrice) = convert(numeric(9,2),@price)) )        
   BEGIN      
   UPDATE dbo.ORDER_File SET confirmed='1' WHERE Order_Number=@orderId        
   END        
     -----------------------------------  end beween price
     
     ---------------------------------------------- buy confirmed when  price greater than sell price
     ----------------------------------------------sell confirmed when prive smaller than Buy price
       IF EXISTS(SELECT * FROM dbo.Product_Det WHERE ScripCode=@code AND @Buy_Sell_Ind=1 AND (convert(numeric(9,2),SellPrice) < convert(numeric(9,2),@price)OR convert(numeric(9,2),LastTradedPrice) < convert(numeric(9,2),@price)OR convert(numeric(9,2),@price)=convert(numeric(9,2),0))
       OR ScripCode=@code AND @Buy_Sell_Ind=2 AND (convert(numeric(9,2),BuyPrice) > convert(numeric(9,2),@price) OR convert(numeric(9,2),LastTradedPrice) > convert(numeric(9,2),@price) ))        
   BEGIN          
    SELECT @sPrice=CONVERT(NUMERIC(9,2),sellprice),@bPrice=CONVERT(NUMERIC(9,2),buyprice),@ltp=convert(numeric(9,2),LastTradedPrice) FROM dbo.Product_Det WHERE ScripCode=@code AND @Buy_Sell_Ind=1  OR ScripCode=@code AND @Buy_Sell_Ind=2    
 
    UPDATE dbo.ORDER_File SET confirmed='1' ,    
          price=case when ( @Buy_Sell_Ind=1 and @OptionType='') then @sPrice
          when (@Buy_Sell_Ind=1 and @OptionType='4') then @price    
          when (@Buy_Sell_Ind=2 and @OptionType='')  then @bPrice
          when (@Buy_Sell_Ind=2 and @OptionType='5') then  @price
      end    
     WHERE Order_Number=@orderId        
     END  
     --------------------------------  end smaller n greater than condition
     -------------------------------end smaller n greater than condition
       
    end    ---RL end  
   
   
   ------------------------trigger price condition
         
    else if @cwhere='SL'    
   begin      
    IF EXISTS(SELECT * FROM dbo.Product_Det WHERE ScripCode=@code AND @Buy_Sell_Ind=1 AND convert(numeric(9,2),SellPrice)=convert(numeric(9,2),@price) OR ScripCode=@code AND @Buy_Sell_Ind=2 AND convert(numeric(9,2),BuyPrice) = convert(numeric(9,2),@price)
)        
    BEGIN        
      UPDATE dbo.ORDER_File SET confirmed='1' WHERE Order_Number=@orderId        
    END      
 end      --end trigger
      ---------------------------------------------------------------  
END

No comments:

Post a Comment