我正在创建一个存储过程,如下所示
ALTER PROCEDURE [dbo].[spSUPPLIERSOA]
@CHARTNUMBER1 INT = NULL,
@CHARTNUMBER2 INT = NULL,
@ASONDATE DATETIME = NULL
AS
BEGIN
IF OBJECT_id('vwSUPPLIERSOA') IS NOT NULL
DROP VIEW vwSUPPLIERSOA
GO
CREATE VIEW vwSUPPLIERSOA AS
SELECT GLJRNDTL.JOURNALCODE, GLJRNDTL.JOURNALNUM, GLJRNDTL.POSTEDDATE, APINVHED.CHARACTER01, APINVHED.INVOICENUM, APINVHED.INVOICEDATE, APINVHED.DUEDATE,
GLJRNDTL.TRANSAMT, APINVHED.PAYAMOUNTS, APINVHED.DOCPAYAMOUNTS, APINVHED.INVOICEBAL, APINVHED.DOCINVOICEBAL, APINVHED.CURRENCYCODE, APINVHED.EXCHANGERATE
FROM GLJRNDTL
LEFT OUTER JOIN APINVHED
ON APINVHED.INVOICENUM = GLJRNDTL.APINVOICENUM
WHERE GLJRNDTL.GLCHART BETWEEN @CHARTNUMBER1 AND @CHARTNUMBER2 AND GLJRNDTL.POSTEDDATE < @ASONDATE
END
我收到以下的错误信息
Msg 102, Level 15, State 1, Procedure spSUPPLIERSOA, Line 8
Incorrect syntax near 'vwSUPPLIERSOA'.
Msg 137, Level 15, State 2, Procedure vwSUPPLIERSOA, Line 7
Must declare the scalar variable "@CHARTNUMBER1".
请让我知道我哪里出错了。
答案 0 :(得分:0)
您无法在视图中使用变量。所以我看到你有这些选择:
如果它已经存在,你无论如何都会丢弃视图。然后,我无法真正看到这一观点。所以你可以这样做:
ALTER PROCEDURE [dbo].[spSUPPLIERSOA]
@CHARTNUMBER1 INT = NULL,
@CHARTNUMBER2 INT = NULL,
@ASONDATE DATETIME = NULL
AS
BEGIN
SELECT
GLJRNDTL.JOURNALCODE, GLJRNDTL.JOURNALNUM,
GLJRNDTL.POSTEDDATE, APINVHED.CHARACTER01,
APINVHED.INVOICENUM, APINVHED.INVOICEDATE, APINVHED.DUEDATE,
GLJRNDTL.TRANSAMT, APINVHED.PAYAMOUNTS, APINVHED.DOCPAYAMOUNTS,
APINVHED.INVOICEBAL, APINVHED.DOCINVOICEBAL, APINVHED.CURRENCYCODE,
APINVHED.EXCHANGERATE
FROM GLJRNDTL
LEFT OUTER JOIN APINVHED
ON APINVHED.INVOICENUM = GLJRNDTL.APINVOICENUM
WHERE GLJRNDTL.GLCHART BETWEEN @CHARTNUMBER1
AND @CHARTNUMBER2 AND GLJRNDTL.POSTEDDATE < @ASONDATE
END
您可以创建一个表值函数,以后可以选择。像这样:
CREATE FUNCTION funcSUPPLIERSOA
(
@CHARTNUMBER1 INT,
@CHARTNUMBER2 INT,
@ASONDATE DATETIME
)
RETURNS TABLE
AS
RETURN
SELECT
GLJRNDTL.JOURNALCODE, GLJRNDTL.JOURNALNUM,
GLJRNDTL.POSTEDDATE, APINVHED.CHARACTER01,
APINVHED.INVOICENUM, APINVHED.INVOICEDATE, APINVHED.DUEDATE,
GLJRNDTL.TRANSAMT, APINVHED.PAYAMOUNTS, APINVHED.DOCPAYAMOUNTS,
APINVHED.INVOICEBAL, APINVHED.DOCINVOICEBAL, APINVHED.CURRENCYCODE,
APINVHED.EXCHANGERATE
FROM GLJRNDTL
LEFT OUTER JOIN APINVHED
ON APINVHED.INVOICENUM = GLJRNDTL.APINVOICENUM
WHERE GLJRNDTL.GLCHART BETWEEN @CHARTNUMBER1 AND @CHARTNUMBER2
AND GLJRNDTL.POSTEDDATE < @ASONDATE
然后使用商店程序从中选择:
ALTER PROCEDURE [dbo].[spSUPPLIERSOA]
@CHARTNUMBER1 INT = NULL,
@CHARTNUMBER2 INT = NULL,
@ASONDATE DATETIME = NULL
AS
SELECT * FROM funcSUPPLIERSOA(@CHARTNUMBER1,@CHARTNUMBER2,@ASONDATE) AS tbl