嗨,大家好我想根据条件跟踪条件运行此查询。如果参数日期小于01/01/16,它应该运行第一个CTE,否则运行第二个CTE谢谢
DECLARE @Date AS DATET
SET @Date = '2015-10-31'
;WITH OLDVTE AS (SELECT
SalesID,
City,
Amount,
SalesDate
FROM INFO.SALESOLD
),
NEWVTE AS (
SalesID,
City,
Amount,
SalesDate
FROM INFO.SALESNEW
)
SELECT
CASE WHEN GETDATE() <= @Date THEN
(SELECT * FROM OLDVTE)
ELSE
(SELECT * FROM NEWVTE)
END AS DD
答案 0 :(得分:2)
您可以尝试这样的事情:
DECLARE @Date AS DATE = {d'2015-10-31'}
DECLARE @CurrentDateTime DATETIME = GETDATE();
SELECT
SalesID,
City,
Amount,
SalesDate
FROM INFO.SALESOLD
WHERE @CurrentDateTime <= @Date
UNION ALL
SELECT
SalesID,
City,
Amount,
SalesDate
FROM INFO.SALESNEW
WHERE @CurrentDateTime > @Date
UNION ALL
会将两个结果合二为一。但实际上只有一个选择返回行...
我实际上怀疑,您针对GETDATE()
的过滤器会按预期运行...
答案 1 :(得分:1)
您可以尝试使用IF ELSE,如下所示。
DECLARE @Date AS DATETIME
SET @Date = '2015-10-31'
If GETDATE() <= @Date--will this clause work..
select * FROM INFO.SALESOLD
Else
select * FROM INFO.SALEnew
如果两个表包含任何公共字段并通过日期
,您也可以加入这两个表