tsql - 通过嵌套的 select case 语句过滤存储过程记录

标签 tsql stored-procedures sql-server-2012 case-statement

我需要进一步优化来自 this post 的存储过程结果集,我需要过滤结果集以仅显示 emailaddr 为 NULL 的记录(意味着仅显示 Invoice_DeliveryType 值为 'N' 的记录)。

在众多查询中,我尝试过:

select 
    Invoice_ID, 'Unknown' as Invoice_Status, 
    case when Invoice_Printed is null then '' else 'Y' end as Invoice_Printed, 
    case when Invoice_DeliveryDate is null then '' else 'Y' end as Invoice_Delivered, 
    (case when Invoice_DeliveryType <> 'USPS' then ''
          when exists (Select 1
                   from dbo.Client c
                   Where c.Client_ID = SUBSTRING(i.Invoice_ID, 1, 6) and
                         c.emailaddr is not null
                  )
          then 'Y'
          else 'N'
     end)
    Invoice_ContactLName + ', ' + Invoice_ContactFName as ContactName, 
from 
    dbo.Invoice
left outer join 
    dbo.fnInvoiceCurrentStatus() on Invoice_ID = CUST_InvoiceID 
where 
    CUST_StatusID = 7 
    AND Invoice_ID = dbo.Client.Client_ID
    AND dbo.client.emailaddr is NULL
order by 
    Inv_Created  

但是我得到一个错误

The conversion of the nvarchar value '20111028995999' overflowed an int column

如何让存储过程只返回带有 DeliveryType = 'N' 的记录?

最佳答案

尝试将存储过程结果选择到临时表中,然后选择 * 来自#TempTable

关于tsql - 通过嵌套的 select case 语句过滤存储过程记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25837540/

相关文章:

sql server 2000将datetime转换为hhmm

sql - 使用SQL过滤存储过程的结果

sql-server-2012 - 使用 LAG 获取上一行更新值而不使用递归 CTE

mysql - 游标获取返回 Null

sql - SQL Server 中的随机数生成

sql-server - 在sql server中计算 parking 时间

sql-server - SQL Server 2005 - 如何比较字段值,如果不同则返回计数,每次出现

sql - 在 T-SQL 中使用任意数量的参数

sql - SQL Server 2000 中的非系统数据库

mysql - mysql SP 中使用数组参数时出错