SQL中Union和if else 的问题
begin
declare @topCount int
declare @needCount int
SELECT @topCount = Count(*) from T_Article
end
select @needCount = 5-@topCount
SELECT ArticleID, IsTop, IsNotice, IsNews, ImageUrl, Title, [Content], ChannelId, ModuleId, AdminID, CONVERT(VARCHAR(10),AddDate,110) as AddDate
FROM T_Article
WHERE (IsNews = 1 AND IsTop=1)
UNION ALL
begin
if(@needCount > 0)
begin
SELECT TOP (@needCount) ArticleID, IsTop, IsNotice, IsNews, ImageUrl, Title, [Content], ChannelId, ModuleId, AdminID, CONVERT(VARCHAR(10),AddDate,110) as AddDate
FROM T_Article
WHERE (IsNews = 1 AND IsTop=0)
end
end
错误提示:
消息 156,级别 15,状态 1,第 15 行
关键字 'begin' 附近有语法错误。