声明varchar视图问题

时间:2013-02-19 14:51:07

标签: sql sql-server-2005

我将以下代码转换为视图。但是我无法在视图中声明varchar。

任何人都可以建议绕道而行吗?

Sql代码 -

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

谢谢,

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

如果您尝试将参数传递给VIEW,则无法执行此操作。在视图中选择时,只需使用分组依据进行以下视图,然后进行过滤。

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

答案 2 :(得分: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')