脚本存储过程的请求在进程查询中返回值

时间:2015-08-20 10:42:39

标签: stored-procedures

请求编写一个返回此内容的评分过程的脚本:

请求

  1. 当你传递null return all
  2. 和字段可以搜索
  3. 组合可以搜索
  4. 代码:

    @AccountNo varchar(30) = null,
    @DateLogged datetime = null,
    @ReferenceNo varchar (50) = null,
    @ErfNo varchar (45) = null,
    @Surname varchar (255) = null,
    @Name varchar (255) = null,
    @IdentityNo varchar (13) = null,
    
    ALTER PROCEDURE [dbo].[Search_case]
    (
         @AccountNo varchar(30) = null,
         @DateLogged datetime = null,
         @ReferenceNo varchar (50) = null,
         @ErfNo varchar (45) = null,
         @Surname varchar (255) = null,
         @Name varchar (255) = null,
         @ProcessInstanceId varchar (50) = null,
         @IdentityNo varchar (13) = null,
         @Summary varchar (max) = null,
         @Suspend varchar (10) = null,
         @Notes varchar (max) = null,
         @RatesNo varchar (30) = null,
         @Status varchar (50) = null
    )
    WITH EXECUTE AS OWNER, RECOMPILE
    AS
        SET NOCOUNT ON;
    
        SELECT
            dbo.Cases.DateLogged,
            dbo.Cases.ReferenceNo,
            dbo.Account.AccountNo,
            dbo.Customer.RatesNo,
            dbo.Customer.IdentityNo,
            dbo.Account.ErfNo,
            dbo.Customer.Surname,
            dbo.Customer.Name,
            dbo.Cases.ProcessInstanceId,
            dbo.Cases.Status,
            dbo.Cases.Summary,
            dbo.Cases.Suspend,
            dbo.Cases.Notes
        FROM
            [ICSM_CORRES].[dbo].[Cases]
        INNER JOIN 
            [dbo].Customer ON dbo.Cases.CustomerId = dbo.Cases.CustomerId
        INNER JOIN
            dbo.Account ON dbo.Customer.AccountNoId = dbo.Account.AccountNoId
        WHERE
            dbo.Account.AccountNo = @AccountNo
            OR dbo.Customer.RatesNo = @RatesNo
            OR dbo.Cases.DateLogged = @DateLogged
            OR dbo.Cases.ReferenceNo = @ReferenceNo
            OR dbo.Customer.IdentityNo = @IdentityNo
            OR dbo.Account.ErfNo = @ErfNo
            OR dbo.Customer.Surname = @Surname
            OR dbo.Customer.Name = @Name
            OR dbo.Cases.ProcessInstanceId = @ProcessInstanceId
            OR dbo.Cases.Status = @Status
            OR dbo.Cases.Summary = @Summary
            OR dbo.Cases.Suspend = @Suspend
            OR dbo.Cases.Notes = @Notes
    

1 个答案:

答案 0 :(得分:0)

  • “当你传递null return all”=> WHERE col = ISNULL(@argument, col)

  • 其他条件应该有效,但初始查询的性能不是最好的(多个连接和where子句中的多个OR)可能会导致全表扫描。

  • 您还可以为[dbo].Customer AS c等完全限定名称设置别名,然后将dbo.Customer.Surname简称为c.Surname

  • 您可能在JOIN ON(同一表格和列)中遇到错误[dbo].Customer ON dbo.Cases.CustomerId = dbo.Cases.CustomerId

ALTER PROCEDURE [dbo].[Search_case]
(
     @AccountNo varchar(30) = null,
     @DateLogged datetime = null,
     @ReferenceNo varchar (50) = null,
     @ErfNo varchar (45) = null,
     @Surname varchar (255) = null,
     @Name varchar (255) = null,
     @ProcessInstanceId varchar (50) = null,
     @IdentityNo varchar (13) = null,
     @Summary varchar (max) = null,
     @Suspend varchar (10) = null,
     @Notes varchar (max) = null,
     @RatesNo varchar (30) = null,
     @Status varchar (50) = null
)
WITH EXECUTE AS OWNER, RECOMPILE
AS
    SET NOCOUNT ON;

    SELECT
        dbo.Cases.DateLogged,
        dbo.Cases.ReferenceNo,
        dbo.Account.AccountNo,
        dbo.Customer.RatesNo,
        dbo.Customer.IdentityNo,
        dbo.Account.ErfNo,
        dbo.Customer.Surname,
        dbo.Customer.Name,
        dbo.Cases.ProcessInstanceId,
        dbo.Cases.Status,
        dbo.Cases.Summary,
        dbo.Cases.Suspend,
        dbo.Cases.Notes
    FROM
        [ICSM_CORRES].[dbo].[Cases]
    INNER JOIN 
        [dbo].Customer ON dbo.Cases.CustomerId = dbo.Cases.CustomerId
    INNER JOIN
        dbo.Account ON dbo.Customer.AccountNoId = dbo.Account.AccountNoId
    WHERE
        dbo.Account.AccountNo = ISNULL(@AccountNo, dbo.Account.AccountNo)
        OR dbo.Customer.RatesNo = ISNULL(@RatesNo, dbo.Customer.RatesNo)
        OR dbo.Cases.DateLogged = ISNULL(@DateLogged, dbo.Cases.DateLogged)
        OR dbo.Cases.ReferenceNo = ISNULL(@ReferenceNo, dbo.Cases.ReferenceNo)
        OR dbo.Customer.IdentityNo = ISNULL(@IdentityNo, dbo.Customer.IdentityNo)
        /* ... and so on */