Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL While loop

SQL While loop

Scheduled Pinned Locked Moved Database
database
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mphirana
    wrote on last edited by
    #1

    Hi I'm running the below query, I want to get records for multiple fundCodes in this case Bateleur and Tower Fund.I'm only getting records for Bateleur only from 2016-04-20 to 2016-04-29. Thank you

    Declare @FundCodes VarChar(Max)='Bateleur|Tower Fund',--Tower Fund
    @StartDate DateTime='2016/04/20',
    @EndDate DateTime='2016/04/29',
    @Zero integer=1,
    @LongShortAll integer = 0, --0 for all, 1 for long value, 2 for short value
    @PhysEff integer = 0 --0 for physical, 1 for Effective

    	--check the fulldate 
    	select fulldate into #TableDates 
    	from AssetData.dbo.Calendar cal where cal.FullDate between @StartDate and @EndDate
    	and IsWeekDay = 1 and IsHoliday = 0
    
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
         
    	declare @NextDate DateTime
    	set @NextDate = @StartDate
    	declare @TempValues table(FundCode varchar(200),EffectiveDate DateTime, Value Float)	
    
    	declare @fundcodestable table (ID int, fundcode varchar(300))
    	declare @Val float
    
    	--Date Counter
    	declare @DateCounter int = 0
    	select @DateCounter = count(Fulldate) from #TableDates
    	declare @Date date
    			
    	while @DateCounter <> 0 
    
    	begin
    
    	set @Date = (select top 1 fulldate from #TableDates order by 1)
    
    	--fundcode
    	insert into @fundcodestable
    	select	pn, s
    	from	StagedFundReportingData..FnxSplit('|', @fundcodes)
    
    	--fundcode Counter
    	Declare @counterFundId int = 0
    	select @counterFundId = count(fundcode) from @fundcodestable
    	declare @fundC varchar(300) 
    	
    	while @counterFundId <>0 and @NextDate<=@EndDate
    	begin
    		set @fundC = (select top 1 fundcode from @fundcodestable)
    			Set @Val = 0
    			if @Zero = 0
    			   begin
    					if @LongShortAll = 0 begin
    						exec GetFundValue @fundC, @NextDate, @Val output
    					end else begin
    						if @LongShortAll = 1 begin
    							if @PhysEff = 0 begin
    								exec GetFundValueLongOnly @fundC, @NextDate, @Val output
    							end else begin
    								exec GetFundUnderlyingEffectiveValueLongOnly @fundC, @NextDate, @Val output
    							end
    						end else begin
    							if @PhysEff = 0 begin
    								exec GetFundValueShortOnly @fundC, @NextDate, @Val output
    							end else begin
    								exec GetFundUnderlyingEffectiveValueShortOnly @fundC, @NextDate, @Val output
    							end
    						end
    					end
    				end
    				else
    				begin
    					If @LongShortAll = 0 begin
    						exec GetFundValue\_ZeroFutures @fundC, @NextDate, @Val output
    
    V J 2 Replies Last reply
    0
    • M Mphirana

      Hi I'm running the below query, I want to get records for multiple fundCodes in this case Bateleur and Tower Fund.I'm only getting records for Bateleur only from 2016-04-20 to 2016-04-29. Thank you

      Declare @FundCodes VarChar(Max)='Bateleur|Tower Fund',--Tower Fund
      @StartDate DateTime='2016/04/20',
      @EndDate DateTime='2016/04/29',
      @Zero integer=1,
      @LongShortAll integer = 0, --0 for all, 1 for long value, 2 for short value
      @PhysEff integer = 0 --0 for physical, 1 for Effective

      	--check the fulldate 
      	select fulldate into #TableDates 
      	from AssetData.dbo.Calendar cal where cal.FullDate between @StartDate and @EndDate
      	and IsWeekDay = 1 and IsHoliday = 0
      
      	-- SET NOCOUNT ON added to prevent extra result sets from
      	-- interfering with SELECT statements.
      	SET NOCOUNT ON;
           
      	declare @NextDate DateTime
      	set @NextDate = @StartDate
      	declare @TempValues table(FundCode varchar(200),EffectiveDate DateTime, Value Float)	
      
      	declare @fundcodestable table (ID int, fundcode varchar(300))
      	declare @Val float
      
      	--Date Counter
      	declare @DateCounter int = 0
      	select @DateCounter = count(Fulldate) from #TableDates
      	declare @Date date
      			
      	while @DateCounter <> 0 
      
      	begin
      
      	set @Date = (select top 1 fulldate from #TableDates order by 1)
      
      	--fundcode
      	insert into @fundcodestable
      	select	pn, s
      	from	StagedFundReportingData..FnxSplit('|', @fundcodes)
      
      	--fundcode Counter
      	Declare @counterFundId int = 0
      	select @counterFundId = count(fundcode) from @fundcodestable
      	declare @fundC varchar(300) 
      	
      	while @counterFundId <>0 and @NextDate<=@EndDate
      	begin
      		set @fundC = (select top 1 fundcode from @fundcodestable)
      			Set @Val = 0
      			if @Zero = 0
      			   begin
      					if @LongShortAll = 0 begin
      						exec GetFundValue @fundC, @NextDate, @Val output
      					end else begin
      						if @LongShortAll = 1 begin
      							if @PhysEff = 0 begin
      								exec GetFundValueLongOnly @fundC, @NextDate, @Val output
      							end else begin
      								exec GetFundUnderlyingEffectiveValueLongOnly @fundC, @NextDate, @Val output
      							end
      						end else begin
      							if @PhysEff = 0 begin
      								exec GetFundValueShortOnly @fundC, @NextDate, @Val output
      							end else begin
      								exec GetFundUnderlyingEffectiveValueShortOnly @fundC, @NextDate, @Val output
      							end
      						end
      					end
      				end
      				else
      				begin
      					If @LongShortAll = 0 begin
      						exec GetFundValue\_ZeroFutures @fundC, @NextDate, @Val output
      
      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      And what is your question?

      1 Reply Last reply
      0
      • M Mphirana

        Hi I'm running the below query, I want to get records for multiple fundCodes in this case Bateleur and Tower Fund.I'm only getting records for Bateleur only from 2016-04-20 to 2016-04-29. Thank you

        Declare @FundCodes VarChar(Max)='Bateleur|Tower Fund',--Tower Fund
        @StartDate DateTime='2016/04/20',
        @EndDate DateTime='2016/04/29',
        @Zero integer=1,
        @LongShortAll integer = 0, --0 for all, 1 for long value, 2 for short value
        @PhysEff integer = 0 --0 for physical, 1 for Effective

        	--check the fulldate 
        	select fulldate into #TableDates 
        	from AssetData.dbo.Calendar cal where cal.FullDate between @StartDate and @EndDate
        	and IsWeekDay = 1 and IsHoliday = 0
        
        	-- SET NOCOUNT ON added to prevent extra result sets from
        	-- interfering with SELECT statements.
        	SET NOCOUNT ON;
             
        	declare @NextDate DateTime
        	set @NextDate = @StartDate
        	declare @TempValues table(FundCode varchar(200),EffectiveDate DateTime, Value Float)	
        
        	declare @fundcodestable table (ID int, fundcode varchar(300))
        	declare @Val float
        
        	--Date Counter
        	declare @DateCounter int = 0
        	select @DateCounter = count(Fulldate) from #TableDates
        	declare @Date date
        			
        	while @DateCounter <> 0 
        
        	begin
        
        	set @Date = (select top 1 fulldate from #TableDates order by 1)
        
        	--fundcode
        	insert into @fundcodestable
        	select	pn, s
        	from	StagedFundReportingData..FnxSplit('|', @fundcodes)
        
        	--fundcode Counter
        	Declare @counterFundId int = 0
        	select @counterFundId = count(fundcode) from @fundcodestable
        	declare @fundC varchar(300) 
        	
        	while @counterFundId <>0 and @NextDate<=@EndDate
        	begin
        		set @fundC = (select top 1 fundcode from @fundcodestable)
        			Set @Val = 0
        			if @Zero = 0
        			   begin
        					if @LongShortAll = 0 begin
        						exec GetFundValue @fundC, @NextDate, @Val output
        					end else begin
        						if @LongShortAll = 1 begin
        							if @PhysEff = 0 begin
        								exec GetFundValueLongOnly @fundC, @NextDate, @Val output
        							end else begin
        								exec GetFundUnderlyingEffectiveValueLongOnly @fundC, @NextDate, @Val output
        							end
        						end else begin
        							if @PhysEff = 0 begin
        								exec GetFundValueShortOnly @fundC, @NextDate, @Val output
        							end else begin
        								exec GetFundUnderlyingEffectiveValueShortOnly @fundC, @NextDate, @Val output
        							end
        						end
        					end
        				end
        				else
        				begin
        					If @LongShortAll = 0 begin
        						exec GetFundValue\_ZeroFutures @fundC, @NextDate, @Val output
        
        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Just a suggestion. Print the results from the following

        insert into @fundcodestable
        select pn, s
        from StagedFundReportingData..FnxSplit('|', @fundcodes)

        M 1 Reply Last reply
        0
        • J jschell

          Just a suggestion. Print the results from the following

          insert into @fundcodestable
          select pn, s
          from StagedFundReportingData..FnxSplit('|', @fundcodes)

          M Offline
          M Offline
          Mphirana
          wrote on last edited by
          #4

          Thank you Jschell, I managed to fix the problem.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups