为什么我的SQL语句在使用IF时错了?
为什么我的SQL语句在使用IF时错了?
DECLARE @ii int
set @ii=3
SELECT TOP 3
b.ID
FROM dbo.WebSite_msii_Menu_Master a
INNER JOIN dbo.WebSite_msii_info b
ON a.ID = b.CateID
WHERE b.flagD = 0
AND a.CateNameCn LIKE '正则表达式 '
AND b.ID =
(
IF(SELECT @@RowCount) > 0 AND (SELECT @@RowCount) < @ii
SELECT TOP 1 ID from WEBSITE_MSII_INFO ORDER BY ID ASC
ELSE
SELECT TOP 1 ID from WEBSITE_MSII_INFO ORDER BY ID DESC
)
SELECT TOP 3
b.ID
FROM dbo.WebSite_msii_Menu_Master a
INNER JOIN dbo.WebSite_msii_info b
ON a.ID = b.CateID
WHERE b.flagD = 0
AND a.CateNameCn LIKE '正则表达式 '
AND b.ID IN
(
IF(SELECT @@RowCount) > 0 AND (SELECT @@RowCount) < @ii
SELECT TOP 2 ID from WEBSITE_MSII_INFO ORDER BY ID ASC
ELSE
SELECT TOP 3 ID from WEBSITE_MSII_INFO ORDER BY ID DESC
)
错误提示如下:
服务器: 消息 156,级别 15,状态 1,行 12
在关键字 'IF ' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 16
第 16 行: ') ' 附近有语法错误。
------解决方案-------------------- IF(SELECT @@RowCount) > 0 AND (SELECT @@RowCount) < @ii
SELECT TOP 1 ID from WEBSITE_MSII_INFO ORDER BY ID ASC
ELSE
SELECT TOP 1 ID from WEBSITE_MSII_INFO ORDER BY ID DESC
不能潜入select语句
------解决方案--------------------IF ((SELECT @@RowCount) > 0 AND (SELECT @@RowCount) < @ii)
BEGIN
...
END