【发布时间】:2014-09-14 20:10:17
【问题描述】:
我需要从 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
但我得到一个错误
nvarchar 值 '20111028995999' 的转换溢出了一个 int 列
如何让存储过程只返回 DeliveryType = 'N' 的记录?
【问题讨论】:
-
Invoice_ID = dbo.Client.Client_ID和dbo.client.emailaddr is NULL谓词没有任何意义:在该级别的查询中没有dbo.Client。 -
此外,查询中的某些列不符合表引用的条件并不能更好地理解问题。
标签: tsql stored-procedures sql-server-2012 case-statement