1

我有下面的代码,我想把它变成一个视图。但是我不能在视图中声明 varchar。

任何人都可以建议解决这个问题吗?

Sql 代码 -

DECLARE @PROJECTNUMBER VarChar
SELECT     SUM(PAFeeAmount) FeeAmount
FROM          PA02101
WHERE      PAPROJNUMBER = @PROJECTNUMBER

谢谢,

4

3 回答 3

2

如果您需要变量,请改用表值函数:

http://msdn.microsoft.com/en-us/library/ms191165(v=sql.105).aspx

于 2013-02-19T14:53:02.857 回答
1

如果您尝试将参数传递给 a VIEW,则不能这样做。只需使用 Group By 执行您的视图,并在从视图中选择期间进行过滤。

CREATE VIEW myView
AS
   SELECT  PAPROJNUMBER, SUM(PAFeeAmount) FeeAmount
   FROM   PA02101
   GROUP BY PAPROJNUMBER


--Select like below. (may be passing it to a stored procedure)
declare @projectNumber varchar(50) --remember to give a length 

select * from myView
where projectNumber = @projectNumber
于 2013-02-19T14:53:10.570 回答
1

尝试创建一个inline table-valued函数。例子:

CREATE FUNCTION dbo.fxSample(@PROJECTNUMBER VARCHAR(20))
RETURNS TABLE
AS
RETURN
(
    SELECT  SUM(PAFeeAmount) FeeAmount
    FROM    PA02101
    WHERE   PAPROJNUMBER = @PROJECTNUMBER
)

-- Then call like this, just as if it's a table/view just with a parameter
SELECT * FROM dbo.fxSample('hello')
于 2013-02-19T14:55:41.713 回答