0

我想使用一个存储过程,它可以使一些参数为空值,然后测试它是否参数为空并进行一些编码:我只想知道这个存储过程是否有问题

这是存储过程

   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

看起来它在@nomgiacis时可能有一些问题NULL。在那种情况下,没有什么能满足= @nogiac

因为您的每个案例都以 a 结尾RETURN,所以不需要使用 else,这可能有助于提高可读性(您似乎在第一个案例中使用了该技术,然后在后面的案例中切换到使用 else)。

我通常不喜欢使用类型选择器重用参数的技术 - 特别是因为您在一种情况下将其用作日期(如果转换失败怎么办)。如果您要使用命名/可选参数,只需添加更多参数并让它们成为NULL.

虽然它并不总是在执行计划中表现最好,但您可能会将整个事情写成一个查询(在这种情况下,它是内联表值函数的候选者,这对于代码重用非常有用它本身可以用作连接中的视图或表格等)

我不会重新编写您的查询,但该技术背后的基本思想是这样的:

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 回答
0

如果你想让参数可选所以使用它对不起我没有编辑你的代码 jst bcz 时间

CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL, @AddressLine1 nvarchar(60) = NULL
AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City = ISNULL(@City,City)
AND AddressLine1 LIKE '%' + ISNULL(@AddressLine1 ,AddressLine1) + '%'
GO
于 2014-12-15T07:21:07.410 回答