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

问题描述:

我想要使用一个存储过程,可以使空值的一些参数,然后测试它,如果该参数为空或不,并做一些编码:我只是想知道如果有在这个存储过程中的任何问题SQL Server 2008存储过程中的默认参数值

这是存储过程

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 [email protected] and [email protected] 
       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 [email protected] and ID_Entreprise in(select ID_Entreprise 
                  from dbo.Entreprise 
                  where [email protected]) 
                  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 
+1

这是一个存储过程** ** - 不是存储procuder –

+0

TNKS @marc_s ... fr音符和编辑..和soory fr我的坏语言 –

+0

如果您尝试运行它,会发生什么情况?你有一个编译器,让它做它的工作,让它告诉你是否有什么错误。 – Limey

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

因为你的每一个案件有RETURN结束,没有必要对其他使用,这可能有助于可读性(你好像使用该技术的第一个,然后切换到在后面的情况下使用else)。

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

虽然它并不总是执行计划中最好的执行方式,但您可以将整个事件作为单个查询来编写(在这种情况下,它就是内联表值函数的候选对象,这对于代码很有用重复使用,因为它可以自己象视图或联接等表)

我不打算重新写你的查询,但该技术背后的基本理念是这样使用:

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

将不同的东西联合在一起并且不联合时唯一棘手的问题是您可能需要将显式内部联接到左连接,然后有一个where子句,它有效地将它变成某个参数的内连接,而不是其他连接。

代码看起来OK,太 - 我不能运行存储的过程,但是从我所看到的,我没有看到任何明显的错误或问题。

试试看!如果遇到问题,请回复错误消息并重新提问!

+1

它工作正常... tnks duddy –

+0

我想在gridview ASP中使用此过程。NET VS在sqldatasource ,但它不能工作 –

+0

@Yassineedouiri:这似乎是一个全新的问题 - 问这里,我敢肯定有人可以帮助你! –

如果要进行参数可选的,所以用它 对不起,我没有编辑的时候你的代码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