我有一个sql查询,如果在查询执行后没有结果集,我想将值返回为“NULL”。我已经尝试过ISNULL属性,但是它产生了错误,
我的查询是:
SELECT ISNULL((SELECT TOP (100) PERCENT a.ReportDate, a.Scripcode, a.ISIN, a.Scripname, a.AccountNo, a.ClientLongName, a.GroupName, a.Event, a.TradedQty, a.UnreconQty, a.ExDateQty, a.RecDateQty, a.Diff, a.Rate, a.Ratio, a.TotalValue, a.Shares, a.StartDate, a.ExDate, a.RecordDate, a.PayDate, a.AgmDate, a.NSecName, a.NSecNo, a.Remark, b.GRUL_PROX_GRP_ID, b.GRUL_GROUPNO, b.CUSTOMERNO, b.GRUL_SHORTNAME, b.GRUP_SHORTCODE, b.CONF_TYP, b.SWIFT, b.FAX, b.COURIER, b.EMAIL, b.Account_Group, b.Excel_Word, b.Password, b.Sub_Account_Flag
FROM dbo.CRS_Entitlement_MasterDate AS a
INNER JOIN dbo.CRS_Entitlement_EntitlementComn AS b
ON a.AccountNo = b.CUSTOMERNO
WHERE (b.Account_Group = 'A')
AND (b.Excel_Word = 'E')
AND (b.SWIFT <> 'v')
AND (b.CONF_TYP <> 'N/A')
AND (b.FAX = 'v')
OR (b.Account_Group = 'A')
AND (b.Excel_Word = 'E')
AND (b.SWIFT <> 'v')
AND (b.CONF_TYP <> 'N/A')
AND (b.COURIER = 'v')
OR (b.Account_Group = 'A')
AND (b.Excel_Word = 'E')
AND (b.SWIFT <> 'v')
AND (b.CONF_TYP <> 'N/A')
AND (b.EMAIL = 'v')
ORDER BY a.AccountNo), 0) AS Expr1
请帮我解决这个问题,在此先感谢!!!
答案 0 :(得分:0)
像这样使用EXISTS
:
-- returns NULL when it doesn't exist
SELECT CASE WHEN EXISTS ( SELECT 1 AS val
WHERE 1 = 2 ) THEN 1
ELSE NULL
END RESULT;
-- returns something else, in this case 1 when it does exist
SELECT CASE WHEN EXISTS ( SELECT 1 AS val
WHERE 1 = 1 ) THEN 1
ELSE NULL
END RESULT;
EXISTS(Transact-SQL)
指定要测试行是否存在的子查询。
包含数据的示例:
CREATE TABLE #test ( id INT );
INSERT INTO #test
( id )
VALUES ( 1 ),
( 2 ),
( 3 );
-- returns data
IF ( EXISTS ( SELECT *
FROM #test
WHERE id > 0 ) )
SELECT *
FROM #test
WHERE id > 0;
ELSE
SELECT NULL;
-- returns NULL
IF ( EXISTS ( SELECT *
FROM #test
WHERE id > 5 ) )
SELECT *
FROM #test
WHERE id > 5;
ELSE
SELECT NULL AS Result;
DROP TABLE #test;
答案 1 :(得分:-1)
你不能在内部查询中放置多个列。对于ex select(100)%a.ReportDate ----将起作用