0

null値でいくつかのパラメーターを作成し、パラメーターがnullかどうかをテストしてコーディングできるストアドプロシージャを使用したい:このストアドプロシージャに問題があるかどうかを知りたいだけです

これはストアドプロシージャです

   create proc rechercherGIACetAffiche @nomgiac varchar(20),@nom varchar(30) = null,@par varchar(50) = null
           as
            begin
            IF @nom is null and @par is null
                begin
                    select [ID_Dossier] as 'ID_Dossier'
                      ,[ID_Entreprise] as 'ID_Entreprise'
                      ,[Date_Depot] as 'Date_Dépôt'
                      ,[Type_Etude] as 'Type_Etude'
                      ,[Dernier_Type] as 'Dernier_Type'
                      ,[Eligibile] as 'Eligibilité'
                      ,[Fiche_Information] as 'Fiche_Information'
                      ,[Buletin_Adhesion] as 'Bulletin_d’adhésion'
                      ,[Fiche_Renseignment] as 'Fiche_Renseignment'
                      ,[Attestation] as 'Attestation'
                      ,[Date_Debut] as 'Date_Début'
                      ,[Date_Fin] as 'Date_Fin'
                      ,[ID_Cabinet] as 'ID_Cabinet'
                      ,[Montant_Demander] as 'Montant_Demander'
                      ,[Duree] as 'Durée'
                      ,[Porcentage_Taux] as 'Pourcentage,Taux' from Dossier where Nom_Giac = @nomgiac
                      return 
                end
            if  @par is not null and @nom='CNSS' 
              begin
              select d.[ID_Dossier] as 'ID_Dossier'
              ,d.[ID_Entreprise] as 'ID_Entreprise'
              ,[Date_Depot] as 'Date_Dépôt'
              ,[Type_Etude] as 'Type_Etude'
              ,[Dernier_Type] as 'Dernier_Type'
              ,[Eligibile] as 'Eligibilité'
              ,[Fiche_Information] as 'Fiche_Information'
              ,[Buletin_Adhesion] as 'Bulletin_d’adhésion'
              ,[Fiche_Renseignment] as 'Fiche_Renseignment'
              ,[Attestation] as 'Attestation'
              ,[Date_Debut] as 'Date_Début'
              ,[Date_Fin] as 'Date_Fin'
              ,[ID_Cabinet] as 'ID_Cabinet'
              ,[Montant_Demander] as 'Montant_Demander'
              ,[Duree] as 'Durée'
              ,[Porcentage_Taux] as 'Pourcentage,Taux' 
              from dbo.Dossier d inner join entreprise e on d.ID_Entreprise=e.ID_Entreprise
              where CNSS_Entreprise=@par and d.Nom_Giac=@nomgiac 
              return 
              end

            else if @par is not null and @nom='RS'  
              begin
              select [ID_Dossier] as 'ID_Dossier'
              ,[ID_Entreprise] as 'ID_Entreprise'
              ,[Date_Depot] as 'Date_Dépôt'
              ,[Type_Etude] as 'Type_Etude'
              ,[Dernier_Type] as 'Dernier_Type'
              ,[Eligibile] as 'Eligibilité'
              ,[Fiche_Information] as 'Fiche_Information'
              ,[Buletin_Adhesion] as 'Bulletin_d’adhésion'
              ,[Fiche_Renseignment] as 'Fiche_Renseignment'
              ,[Attestation] as 'Attestation'
              ,[Date_Debut] as 'Date_Début'
              ,[Date_Fin] as 'Date_Fin'
              ,[ID_Cabinet] as 'ID_Cabinet'
              ,[Montant_Demander] as 'Montant_Demander'
              ,[Duree] as 'Durée'
              ,[Porcentage_Taux] as 'Pourcentage,Taux'  
              from dbo.Dossier 
              where  Nom_Giac=@nomgiac and ID_Entreprise in( select ID_Entreprise
                                                             from dbo.Entreprise
                                                             where Raison_Social=@par) 
                                                             return                                            
              end
           else if @par is not null and @nom ='Date'
            begin
             declare @v smalldatetime,@b smalldatetime
              set @b=SUBSTRING(@par,1,4)
              set @v=SUBSTRING(@par,5,8)
            select [ID_Dossier] as 'ID_Dossier'
              ,[ID_Entreprise] as 'ID_Entreprise'
              ,[Date_Depot] as 'Date_Dépôt'
              ,[Type_Etude] as 'Type_Etude'
              ,[Dernier_Type] as 'Dernier_Type'
              ,[Eligibile] as 'Eligibilité'
              ,[Fiche_Information] as 'Fiche_Information'
              ,[Buletin_Adhesion] as 'Bulletin_d’adhésion'
              ,[Fiche_Renseignment] as 'Fiche_Renseignment'
              ,[Attestation] as 'Attestation'
              ,[Date_Debut] as 'Date_Début'
              ,[Date_Fin] as 'Date_Fin'
              ,[ID_Cabinet] as 'ID_Cabinet'
              ,[Montant_Demander] as 'Montant_Demander'
              ,[Duree] as 'Durée'
              ,[Porcentage_Taux] as 'Pourcentage,Taux'  
            from Dossier 
            where Date_Depot between @b and @v and Nom_Giac like @nomgiac
           return 
            end 
            end
4

3 に答える 3

2

の場合、問題が発生する可能性があるよう@nomgiacですNULL。その場合、満足するものは何もありません= @nogiac

各ケースは で終わるRETURNため、else を使用する必要はなく、読みやすさに役立つ可能性があります (最初のケースではその手法を使用し、後のケースでは else の使用に切り替えたようです)。

私は通常、型セレクターでパラメーターを再利用する手法が好きではありません-特に、あるケースで日付として使用しているため(変換が失敗した場合)。名前付き/オプションのパラメーターを使用する場合は、パラメーターを追加して、NULL.

また、実行計画で常に最高のパフォーマンスを発揮するとは限りませんが、おそらくこの全体を 1 つのクエリとして記述できます (その場合、インライン テーブル値関数の候補となり、コードの再利用に最適です)。結合などでビューやテーブルのように使用できます。)

クエリを書き直すつもりはありませんが、この手法の背後にある基本的な考え方は次のようなものです。

SELECT *
FROM tbl
WHERE (@param1 IS NULL OR @col1 = @param1)
    AND (@param2 IS NULL OR @col2 = @param2)

結合があるものとないものを組み合わせるときの唯一の注意点は、明示的な内部結合を左結合に変換し、それを特定のパラメーターではなく特定のパラメーターの内部結合に効果的に変換する where 句が必要になる場合があることです。その他。

于 2012-05-30T21:16:10.587 に答える
0

コードも問題ないように見えます。ストアド プロシージャを実行することはできませんが、明らかな間違いや問題は見当たりません。

試してみる!問題が発生した場合は、エラー メッセージに戻ってもう一度質問してください。

于 2012-05-30T21:00:06.287 に答える