从存储过程中的视图进行选择时,无法使用ORDER BY

时间:2012-08-26 09:37:31

标签: sql-server sql-server-2005 stored-procedures view

我有一个视图,虽然我可以执行此语句而没有任何错误:

SELECT * from vwShippingNoticeBase
order by InvoiceNum

当我尝试用完全相同的语句创建存储过程时,我收到错误:

create PROCEDURE [dbo].[upSELECT_shippingNoticeOrderByInvoiceNum]
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM vwShippingNoticeBase
order by invoiceNum

错误:

  

Msg 102,Level 15,State 1,Procedure upSELECT_shippingNoticeByDateRange,Line 7
  'invoiceNum'附近的语法不正确。

很奇怪!

1 个答案:

答案 0 :(得分:3)

如何在存储过程中添加END ....

CREAT PROCEDURE [dbo].[upSELECT_shippingNoticeOrderByInvoiceNum]
AS
BEGIN
   SET NOCOUNT ON;

   SELECT * FROM vwShippingNoticeBase
   ORDER BY invoiceNum
END   -- <<<==== you're missing the END for the BEGIN you have!