SQL Server:如何在CTE中使用CASE时引用别名 - 数据表服务器端查询

时间:2016-11-28 23:48:48

标签: sql tsql stored-procedures datatables

我正在SQL Server中编写一个存储过程,以便使用CASE从多个表中获取jQuery数据表的数据。

这是我的查询

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[getAllQuotes]
    @iDisplayLength int,
    @iDisplayStart int,
    @SortCol int,
    @SortDir nvarchar(10),
    @Search nvarchar(255),
    @Status nvarchar(20) = NULL
AS
BEGIN
    Declare @FirstRec int, @LastRec int

    Set @FirstRec = @iDisplayStart;
    Set @LastRec = @iDisplayStart + @iDisplayLength;

    WITH CTE_Quotes as
    (
        SELECT 
            ROW_NUMBER() OVER (Order by case when (@SortCol = 0 and @SortDir = 'asc')
                                                then QuoteID
                                        end asc,
                                        case when (@SortCol = 0 and @SortDir = 'desc')
                                                then QuoteID
                                        end desc,
                                        case when (@SortCol = 1 and @SortDir = 'asc')
                                                then QuoteDateTime
                                        end asc,
                                        case when (@SortCol = 1 and @SortDir = 'desc')
                                                then QuoteDateTime
                                        end desc,
                                        case when (@SortCol = 2 and @SortDir = 'asc')
                                                then CustomerType
                                         end asc,
        case when (@SortCol = 2 and @SortDir='desc')
            then CustomerType
        end desc,
        case when (@SortCol = 3 and @SortDir='asc')
            then CompanyName
        end asc,
        case when (@SortCol = 3 and @SortDir='desc')
            then CompanyName
        end desc,
        case when (@SortCol = 4 and @SortCol='asc')
            then ContactName
        end asc,
        case when (@SortCol = 4 and @SortCol='desc')
            then ContactName
        end desc,
        case when (@SortCol = 5 and @SortCol='asc')
            then Total
        end asc,
        case when (@SortCol = 5 and @SortCol='desc')
            then Total
        end desc,
        case when (@SortCol = 6 and @SortCol='asc')
            then QuoteBy
        end asc,
        case when (@SortCol = 6 and @SortCol='desc')
            then QuoteBy
        end desc,
        case when (@SortCol = 7 and @SortCol='asc')
            then Status
        end asc,
        case when (@SortCol = 7 and @SortCol='desc')
            then Status
        end desc
    )
    as RowNum,
    COUNT(*) over() AS TotalCount,
    QuoteID, QuoteDateTime, CustomerType = 
    CASE
        WHEN Flag = 'QuoteDB' THEN 'New Customer'
        WHEN Flag = 'LiveDB' THEN 'Existing Customer'
    END , CompanyName =  
    CASE
        WHEN Flag = 'QuoteDB' THEN (SELECT CompanyName FROM dbo.Quote_Companies QCP WHERE QCP.CompanyID = QT.CompanyID)
        WHEN Flag = 'LiveDB' THEN (SELECT CompanyName FROM dbo.Companies CP WHERE CP.CompanyID = QT.CompanyID)
    END,
    ContactName =
    CASE
        WHEN Flag = 'QuoteDB' THEN (SELECT FirstName + ' ' + LastName FROM dbo.Quote_Contacts QCC WHERE QCC.ContactID = QT.ContactID)
        WHEN Flag = 'LiveDB' THEN (SELECT FirstName + ' ' + LastName FROM dbo.Contacts CT WHERE CT.ContactID = QT.ContactID) 
    END,
    Total, QuoteBy, Status
    FROM dbo.QUOTE QT
    WHERE (@Search IS NULL
            Or QuoteID LIKE '%' + @Search + '%'
            or QuoteDateTime LIKE '%' + @Search + '%'
            or CustomerType LIKE '%' + @Search + '%'
            or CompanyName LIKE '%' + @Search + '%'
            or ContactName LIKE '%' + @Search + '%'
            or Total LIKE '%' + @Search + '%'
            or QuoteBy LIKE '%' + @Search + '%'
            or Status LIKE '%' + @Search + '%'
    )
    AND Status = @Status
)
SELECT * 
FROM CTE_Quotes
WHERE RowNum > @FirstRec AND RowNum < @LastRec

但是在尝试创建时会出现以下错误

  

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 94
  列名称'CustomerType'无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 95
  列名称'CompanyName'无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 96
  列名称“ContactName”无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 37
  列名称'CustomerType'无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 40
  列名称'CustomerType'无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 43
  列名称'CompanyName'无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 46
  列名称'CompanyName'无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,49行
  列名称“ContactName”无效。

     

Msg 207,Level 16,State 1,Procedure getAllQuotes,Line 52
  列名称“ContactName”无效。

任何人都可以告诉我如何解决这些错误吗?

我不确定如何引用别名。

由于

1 个答案:

答案 0 :(得分:0)

使用cross apply介绍CompanyName和其他表达式,您可以从其他子句中引用它们。

另外,如果你有sql server 2012或更新版本,你不必在cte中使用row_number()来分页你的结果。您可以在order by子句中使用offset-fetch和整个case语句。