【发布时间】:2018-05-23 21:07:55
【问题描述】:
我有一个存储过程,它给了我一个意外的令牌;当我尝试执行此语句时,ORDER 期待分号。
从 #temp 中选择年份,其中年份不为空 ORDER BY year DESC;
如果我删除 ORDER BY year DESC; 程序正常运行。
我已经尝试了所有可能的方法来按降序对结果表进行排序。我对 SQL 相当陌生,所以我确信它很简单。 TIA。
// --------- 完整的存储过程 ------ //
ALTER PROCEDURE GetYearForExhaustCatalog
(
CatCodeString Memo,
Year CHAR ( 4 ) OUTPUT
)
BEGIN
/*
EXECUTE PROCEDURE GetYearForExhaustCatalog('(e.catalogcode= ''2182'')');
EXECUTE PROCEDURE GetYearForExhaustCatalog('');
*/
DECLARE @CatCodeString string;
DECLARE @SQL string;
@CatCodeString = (SELECT CatCodeString FROM __input);
if @CatCodeString IS NULL or @CatCodeString = '' then
select e2.year,
(SELECT top 1 e2.year
FROM eenginecatalog e LEFT JOIN exhaustengine e2 ON e2.app_no=e.app_no)
as year
into #temp
from Exhaustengine e2;
select year from #temp where year is not null
GROUP BY year
ORDER BY year DESC;
else
@SQL =
'select e2.year, '+
'(SELECT top 1 e2.year '+
'FROM eenginecatalog e LEFT JOIN exhaustengine e2 ON e2.app_no=e.app_no and '+
@CatCodeString +' ) '+
'as year '+
'into #temp '+
'from Exhaustengine e2; '+
'select year from #temp where year is not null '+
'GROUP BY year '+
'ORDER BY year DESC ';
execute immediate @SQL;
end;
insert into __output
select year from #temp where year is not null ORDER BY year;
drop table #temp;
END;
【问题讨论】:
标签: sql stored-procedures advantage-database-server