IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Adaptive Server Enterprise Sybase Discussion :

[ASE]proc stock qui traite un tableau de données et les execute par batch


Sujet :

Adaptive Server Enterprise Sybase

  1. #1
    Membre à l'essai
    Inscrit en
    Décembre 2007
    Messages
    53
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 53
    Points : 23
    Points
    23
    Par défaut [ASE]proc stock qui traite un tableau de données et les execute par batch
    Bonjour

    Je suis nouveau sur les proc stock en sybase. Je voudrai créer une proc stoc qui va prendre en argument un tableau d'objet et extraire les infos en les inserant en base.Voici la proc qui insére les données

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
     
     
    CREATE PROCEDURE dbo.insertTrade(  
    @toto varchar(50), 
    @titi varchar(100), 
    @code varchar(33), 
    @date smalldatetime, 
    @amount numeric(15,5) 
    )  
     
    AS 
     
    declare @idFund numeric(7) 
    declare @idtoto numeric(7) 
    declare @idtiti numeric(7) 
    declare @idcompo numeric(7) 
    declare @idnewcompo numeric(7) 
    declare @localtran integer 
    declare @localerr integer 
     
    BEGIN   
     
    select @idtoto=IdTransferAgent from TRetroTransferAgent where Name=@toto 
    select @idtiti=IdSubAccount from TRetroSubAccount where Name=@titi and IdTransferAgent=@idtoto 
    select @idFund=u.idFund from TUdl u, TCountry c where  c.IdCountry=u.IdCountry and c.CountryCode = SUBSTRING(@code,1,2) and u.UdlMnemo = SUBSTRING(@code,4,CHAR_LENGTH(@code)) 
     
    if @idtoto is null or @idtiti is null or @idFund is null 
    begin 
    	select "toto or titi or Fund incorrect" as Result 
    	return 
    end 
    print 'Idtoto=%1!,Idtiti=%2!,idFund=%3!', @idtoto,@idtiti,@idFund 
     
     
    select @localtran = @@trancount 
    if @localtran =0 
    begin tran 
     
    select @idcompo=IdCompo from TRetroSubAccountCompo where IdSubAccount=@idtiti and date=@date 
    if @idcompo is null 
    begin 
    	print 'No compo at the trade date' 
     
    	--ajout d'une nouvelle compo en date de trade 
    	print 'Creating a new compo at the trade date' 
    	insert into TRetroSubAccountCompo(IdSubAccount,date) values (@idtiti,@date)  
    	select @localerr = @@error 
    	if @localerr != 0 
    	begin 
    		goto FIN 
    	end 
     
    	select @idnewcompo=IdCompo from TRetroSubAccountCompo where IdSubAccount=@idtiti and date=@date 
     
     
    	-- ajout des fonds de la derniere compo (si elle existe) a la nouvelle compo 
    	print 'Adding the underlyings in the new compo' 
    	insert into TRetroHierarchy select idFund, @idnewcompo, SharesNb from TRetroHierarchy
        where IdCompo in (
                            select IdCompo
                            from TRetroSubAccountCompo
                            where IdSubAccount=@idtiti
                            and date in (select max(date) from TRetroSubAccountCompo where IdSubAccount=@idtiti and date<@date)
                            ) 
    	select @localerr = @@error 
    	if @localerr != 0 
    	begin 
    		goto FIN 
    	end 
     
        select @idcompo=IdCompo from TRetroSubAccountCompo where IdSubAccount=@idtiti and date=@date 	 
    end 
    print 'IdCompo=%1!',@idcompo 
     
    -- pour toutes les compos de date >= date, ajout de @amout au SharesNb pour le fond en question. Cette étotope se fait en 2 parties 
    -- 1) ajout du fond dans toutes ces compos (avec un SharesNb=0) s'il n'en fait pas parti 
     
    print 'Adding the fund with ShareNb=0 to all the compo where the fund is not' 
    insert into TRetroHierarchy( IdCompo, idFund, SharesNb) 
    	select	distinct c.IdCompo, @idFund, 0 
    	from 	TRetroSubAccountCompo c 
    	where 	c.IdSubAccount=@idtiti and c.date>=@date 
    	and not exists( 
    		select h.idFund from TRetroHierarchy h where h.IdCompo=c.IdCompo and h.idFund=@idFund 
    	) 
    select @localerr = @@error 
    if @localerr != 0 
    begin 
    	goto FIN 
    end 
     
    -- 2) pour toutes ces compos et pour le fond en question faire SharesNb+=@amount 
     
    print 'Setting SharesNb=SharesNb+ the amount of the trade to the fund in compos where date>=date' 
    update TRetroHierarchy set SharesNb=SharesNb+@amount 
    where IdCompo in ( 
    	select c.IdCompo from TRetroSubAccountCompo c where c.IdSubAccount=@idtiti and c.date>=@date 
    	) 
    and idFund=@idFund 
    select @localerr = @@error 
     
     
    FIN: 
    if @localerr = 0 
    begin 
    	if @localtran =0 
    	begin 
    		commit tran 
    		print 'Done'
            select "Done" as Result  
    	end 
    end 
    else 
    begin 
    	if @localtran =0 
    	begin 
    		rollback tran
            select "Failed" as Result  
    		print 'Failed' 
    	end 
    end 
     
    return @localerr 
     
    END
    GO
    ACtuellement j'apelle cette proc pour toute insertion.

    CE que je voudrai c'est transformer cette proc pour lui passer un tableau d'objet qui va contenir mes paramétres d'netrée et pourvoir executer par batch avec des lots de 20000 ou 30000 objets.

    C'estdans un souci d'omptimisation et j'ai jamais eu à faire appel à des tableaux.
    Qui peut me donner une indocation

    thanks

  2. #2
    Membre chevronné

    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 307
    Détails du profil
    Informations personnelles :
    Âge : 64
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 307
    Points : 1 828
    Points
    1 828
    Par défaut
    Cela n'est pas possible directement.

    De façon indirecte, on peut utiliser une table temporaire pour passer un tableau de données à une proc:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    create table #param(id int, value char(10), ...)
    go
    insert #param values(1, 'toto', ...)
    insert #param values(2, 'tata', ...)
    go
    exec ma_proc
    go
    où ma_proc est définie comme ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    create proc ma_proc
    as
    insert ma_table ....
       select id, value, ... from #param
    go
    Exemple idiot - mais tu vois le principe...

    Michael
    Michael Peppler
    Membre de TeamSybase - www.teamsybase.com

    "A successful [software] tool is one that was used to do something undreamed of by its author." -- S. C. Johnson

  3. #3
    Membre à l'essai
    Inscrit en
    Décembre 2007
    Messages
    53
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 53
    Points : 23
    Points
    23
    Par défaut
    J ene suis pas sur de comprendre.

    donc si j'ai 10000 objets je veux juste appeller une seule fois ma proc e lui passer en paramétre les 10000 objets te aprés c'est à lui de faire une boulce pour executer les 10000 objets.

    AVce ton exemple je ne vois pas comment je mettrai les 10000 objets en paramétre.

    peux-tu stp être plus explicite

  4. #4
    Membre chevronné

    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 307
    Détails du profil
    Informations personnelles :
    Âge : 64
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 307
    Points : 1 828
    Points
    1 828
    Par défaut
    Il faut insérer les 1000 lignes (objets) dans la table temporaire. Ensuite la proc boucle sur la table temporaire.

    Note que je ne suis pas du tout sûr que ce soit une bonne technique - je te donne simplement la méthode pour le faire.

    Michael
    Michael Peppler
    Membre de TeamSybase - www.teamsybase.com

    "A successful [software] tool is one that was used to do something undreamed of by its author." -- S. C. Johnson

  5. #5
    Membre à l'essai
    Inscrit en
    Décembre 2007
    Messages
    53
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 53
    Points : 23
    Points
    23
    Par défaut
    Ok la table temporaire je la crée où? dans la proc elle même

  6. #6
    Candidat au Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2
    Points : 2
    Points
    2
    Par défaut tableau dans procedure stockée
    Bonjour,
    tu pourrais passer à ta procedure une chaine de caractères qui est la concaténation de toutes tes insertions et ensuite décortiquer ta chaine à l'interieur de ta procedure:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    create procedure INSERTION(
        @chaine varchar(128)
    -- @chaine = '1071,1913,1918,1909,787,2225,1210,1387,1915,1911,1907,1914'
    )
    as
    declare @strIndex numeric(18,0)
     
    select @strIndex = charindex(',',@chaine)
    insert into PRIX select  convert(numeric(18,0),substring(@chaine,1,@strIndex -1))
    select @chaine = substring(@chaine,@strIndex +1,char_length(@chaine)-@strIndex )
    select @strIndex = charindex(',',@chaine)
    insert into PRIX select  convert(numeric(18,0),substring(@chaine,1,@strIndex -1))
    -- faire une boucle

Discussions similaires

  1. Proc stockée qui appelle deux proc stockées et quelques scripts
    Par khalid76 dans le forum Développement
    Réponses: 0
    Dernier message: 07/11/2012, 18h46
  2. Réponses: 3
    Dernier message: 02/09/2009, 17h22
  3. proc stock qui plante:
    Par archcdp dans le forum PL/SQL
    Réponses: 3
    Dernier message: 16/07/2009, 11h02
  4. Réponses: 2
    Dernier message: 12/06/2006, 12h35
  5. Réponses: 2
    Dernier message: 16/10/2003, 17h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo