Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamic SQL query in a cursor
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00485420
Message ID:
00486362
Views:
13
>I think you can do it as three different UPDATE statement. I don't have SQL Server at hand to work things out. The sticker will probably be Process #2 which wants a record number. But you might be able to change the schema to get a replacement.
>
>This is a off-the-cuff attempt at process #1 but I'm not sure the math is right.
>
>
UPDATE the_table
>SET cum = (
> SELECT CASE COUNT(*)
>  WHEN 1 THEN ren
>  ELSE SUM( ((1 + Cumprec) * (1 + Ren)) - 1) END
> FROM the_table t
> WHERE t.year <= the_table.year AND t.month < the_table.month )
>
>
>-Mike

Since CumPrec is not a field but the result of a calculation from the previous record, I'm not sure I can do it using an UPDATE query.

Here is my new SP whihc gives the good results. Is there something I can optimize? (Consider that I am not as good with T-SQL that I am with VB!)
CREATE PROCEDURE dbo.spCalculRendement
    (@NomTable varchar(30),
     @NomChamp varchar(30))
AS

-- Fetched variables 
DECLARE @Annee       smallint,
        @Mois        smallint,
        @Rendement   float,
        @Cumule      float,
        @Annuel      float,
        @Annualise   float

-- Variables to calculate the result of the last 12 months 
DECLARE @Mois01      float,
        @Mois02      float,
        @Mois03      float,
        @Mois04      float,
        @Mois05      float,
        @Mois06      float,
        @Mois07      float,
        @Mois08      float,
        @Mois09      float,
        @Mois10      float,
        @Mois11      float,
        @Mois12      float,
        @MoisCurrent smallint

DECLARE @CumulePrec  float,          -- Previous amount "Cumulé"
        @RecNumber   int,            -- Record number
        @strSQL      nvarchar(200)   -- SQL Query


-- Initialize variables 
SET @RecNumber = 0
SET @Mois01 = 1
SET @Mois02 = 1
SET @Mois03 = 1
SET @Mois04 = 1
SET @Mois05 = 1
SET @Mois06 = 1
SET @Mois07 = 1
SET @Mois08 = 1
SET @Mois09 = 1
SET @Mois10 = 1
SET @Mois11 = 1
SET @Mois12 = 1
SET @MoisCurrent = 0


-- =========================================================================
-- Reset columns
-- =========================================================================
SELECT @strSQL = 'UPDATE ' + @NomTable +
                 ' SET ' + @NomChamp + 'Cum = 0, ' +
                           @NomChamp + 'Ann = 0, ' +
                           @NomChamp + 'Annuel = 0 '
EXEC sp_executesql  @strSQL



-- =========================================================================
-- Open Cursor
-- =========================================================================
EXEC('DECLARE ms_crs_1 CURSOR GLOBAL DYNAMIC FOR
     SELECT Annee, Mois, ' + @NomChamp + ',
                         ' + @NomChamp + 'Cum ' +  ',
                         ' + @NomChamp + 'Ann ' +  ',
                         ' + @NomChamp + 'Annuel'  +  '
     FROM  ' + @NomTable + '
     WHERE ' + @NomChamp + ' <> 0
     ORDER BY Annee, Mois
     FOR UPDATE')
OPEN ms_crs_1
FETCH ms_crs_1 INTO @Annee,
                    @Mois,
                    @Rendement,
                    @Cumule,
                    @Annualise,
                    @Annuel


-- =========================================================================
-- LOOP
-- =========================================================================

WHILE @@fetch_status >= 0
BEGIN

        -- ===================================================================
        -- == Process "Cumulé"
        -- ===================================================================
        If @RecNumber = 0
	        SET @Cumule = @Rendement
        Else
	        SET @Cumule = ((1 + @CumulePrec) * (1 + @Rendement)) - 1

        SET @RecNumber = @RecNumber + 1
        SET @CumulePrec = @Cumule

        -- ===================================================================
        -- == Process "Annualisé"
        -- ===================================================================
        SET @Annualise = POWER((1 + @CumulePrec), (CAST(1 AS REAL) / (CAST(@RecNumber  as Real) / 12))) - 1


        -- ===================================================================
        -- == Process "Annuel"
        -- ===================================================================
        SET @MoisCurrent = @MoisCurrent + 1
        IF @MoisCurrent > 12 	set @MoisCurrent = 1

        if @MoisCurrent = 1 	set @mois01 = @Rendement + 1
        if @MoisCurrent = 2	set @mois02 = @Rendement + 1
        if @MoisCurrent = 3	set @mois03 = @Rendement + 1
        if @MoisCurrent = 4	set @mois04 = @Rendement + 1
        if @MoisCurrent = 5	set @mois05 = @Rendement + 1
        if @MoisCurrent = 6	set @mois06 = @Rendement + 1
        if @MoisCurrent = 7	set @mois07 = @Rendement + 1
        if @MoisCurrent = 8	set @mois08 = @Rendement + 1
        if @MoisCurrent = 9	set @mois09 = @Rendement + 1
        if @MoisCurrent = 10	set @mois10 = @Rendement + 1
        if @MoisCurrent = 11	set @mois11 = @Rendement + 1
        if @MoisCurrent = 12	set @mois12 = @Rendement + 1

        SET @Annuel = (@Mois01  * @Mois02  * @Mois03  * @Mois04  * @Mois05  * @Mois06  * 
        	       @Mois07  * @Mois08  * @Mois09  * @Mois10  * @Mois11  * @Mois12  ) - 1

        -- ===================================================================
        -- == Update fields
        -- ===================================================================
        SET @strSQL = 'UPDATE ' + @NomTable + 
                      ' SET ' + @NomChamp + 'Cum = ' + CAST(@Cumule AS VARCHAR(25)) + ', ' +
                                @NomChamp + 'Ann = ' + CAST(@Annualise AS VARCHAR(25)) + ', ' +
                                @NomChamp + 'Annuel = ' + CAST(@Annuel AS VARCHAR(25)) +
                      ' WHERE CURRENT OF ms_crs_1' 
        EXEC sp_executesql  @strSQL

        -- Fetch the next record
        FETCH ms_crs_1 INTO @Annee,
                            @Mois,
                            @Rendement,
                            @Cumule,
                            @Annualise,
                            @Annuel
END

-- =========================================================================
-- Close the cursor
-- =========================================================================
CLOSE ms_crs_1
DEALLOCATE ms_crs_1

RETURN (0) 
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform