【问题标题】:SQL Server 2008 R2 stored proceduresSQL Server 2008 R2 存储过程
【发布时间】:2015-01-26 18:01:25
【问题描述】:

我在 SQL Server 2008 R2 中的缓慢改进遇到了问题,情况如下:

我想创建一个接受参数的存储过程,但我希望它以一种特殊的方式工作,这样如果我在表患者的搜索中传递 2 个参数,例如,如果我传递给它一个我想要的参数它在表学校中搜索等等。

任何帮助将不胜感激

谢谢

USE []
GO
/****** Object:  StoredProcedure [dbo].[proc_search_patient_ByID]    Script Date: 11/28/2014     07:47:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[proc_search_patient_ByID]
( 
@PatID_pk int ,
@Cntid  smallint,
@FamID int
)
AS
SET NOCOUNT ON
select  cntid AS Center , PatFName AS FirstName   , PatMName AS MiddleName , PatLName AS LastName   , PatMother AS MotherName   ,PatDOB AS DOB
from tbl 
where Cntid=@Cntid  and PatId_PK = @PatID_pk 

如果我提供 2 个参数,我希望我的程序以这种方式工作,但如果我提供 @FamID,我希望它完全在另一个表中搜索

【问题讨论】:

    标签: sql stored-procedures sql-server-2008-r2


    【解决方案1】:

    尝试为未使用的参数传递空值,并在存储过程中检查空值以切换表。

    CREATE PROCEDURE [dbo].[proc_search_patient_ByID]
    ( 
    @PatID_pk int ,
    @Cntid  smallint,
    @FamID int
    )
    AS
    SET NOCOUNT ON
    IF @Cntid IS NULL
    BEGIN
       --Use select stmt of a table
    END
    ELSE
       --Use select stmt of another table
    BEGIN
    END
    

    同样使用适当的参数切换表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多