gpt4 book ai didi

sql-server-2008 - 尽管满足条件但 T-SQL IF block 不执行

转载 作者:行者123 更新时间:2023-12-03 23:37:24 26 4
gpt4 key购买 nike

我正在处理以下 SQL 语句。我已将其缩减为说明问题所需的部分。

DECLARE @mwareId as int = 9647,
@startDate as datetime = '2011-07-20',
@endDate as datetime = '2011-07-20'

IF OBJECT_ID('tempdb..#tmpInvoiceList', 'U') IS NOT NULL DROP TABLE #tmpInvoiceList

-- Get base invoice list for customer
SELECT invoiceId
,invoiceNumber
,customerId
,customerName
,customerCode
,createDate
,lastModifiedDate
,invoiceDate
,totalInvoiceAmount
,statusId
,isPaid
INTO #tmpInvoiceList
FROM Invoice.Invoice

-- Apply date range if applicable
IF ( @startDate != NULL AND @endDate != NULL )
BEGIN
DELETE FROM #tmpInvoiceList
WHERE invoiceDate NOT BETWEEN @startDate AND @endDate
END

SELECT * FROM #tmpInvoiceList

我将@startDate 和@endDate 变量设置为日期值。问题是将日期范围应用于临时表的 if block 没有执行,尽管这两个变量都不为空,我找不到原因。

最佳答案

据我所知,TSQL 不支持 != 运算符进行空值检查。试试这个:

IF ( @startDate IS NOT NULL AND @endDate IS NOT NULL )

关于sql-server-2008 - 尽管满足条件但 T-SQL IF block 不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6915362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com