SQL Server 2008存储过程中的默认参数值

时间:2012-05-30 20:43:57

标签: sql-server sql-server-2008 sql-server-2005 stored-procedures parameters

我想使用一个存储过程,它可以使一些参数为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

3 个答案:

答案 0 :(得分:2)

@nomgiac NULL时,看起来可能会遇到一些问题。在这种情况下,任何事情都不会满足= @nogiac

因为您的每个案例都以RETURN结尾,所以不需要使用else,这可能有助于提高可读性(您似乎使用该技术进行第一个,然后切换到使用其他后来的情况)。

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

虽然它并不总是在执行计划中表现最好,但你可能把整个事情写成一个单独的查询(在这种情况下,它就是一个内联表值函数的候选者,这对于代码重新来说非常好) -use因为它本身可以像连接中的视图或表一样使用。)

我不打算重新编写你的查询,但这个技术背后的基本思想是这样的:

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

组合具有连接和不连接的不同内容时唯一棘手的事情是您可能需要将显式内连接转换为左连接,然后使用where子句有效地将其转换为某些参数的内连接而不是为了其他人。

答案 1 :(得分:0)

代码看起来也不错 - 我无法运行存储过程,但从我看到的情况来看,我没有看到任何明显的错误或问题。

试一试!如果您遇到问题,请返回错误消息并再次询问!

答案 2 :(得分:0)

如果您想让参数可选,请使用它 抱歉,我没有编辑你的代码jst bcz of time

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