1

我想为这个以前可以正常工作的 sql 语句创建一个视图。但是当我将它创建到一个视图中时,一个错误提示显示,"View's SELECT contains a variable or parameter."谁能告诉我错误在哪里,因为我对 MySQL 中的视图很陌生。谢谢!

CREATE VIEW `satsschema`.`viewTimeBreak` AS 
SELECT a.EmpName, CONCAT(b.StartTime, '-', b.EndTime) AS ShiftTime, CONCAT(a.EmpTime,     '-', ADDTIME(a.EmpTime, '0 1:0:0.000000')) AS BreakTime, a.Break, 
                     a.EmployeeOnBreak, '' AS SignIn, '' AS SignOut
FROM            satsschema.employeeslot a INNER JOIN
                     satsschema.ufis b ON b.UFISID = a.UFISEmpGroup
WHERE        (a.AllocationDate = @AllocationDate) AND (a.LocationName = @LocationName)   AND (a.Break = 1)
GROUP BY a.EmpName

这部分显示了我尝试使用 sql 语句创建视图时的错误。

4

2 回答 2

1

VIEWs不能像存储过程和函数一样有参数。SO用户之前已经回答了这个问题:view with parameter

于 2012-09-13T03:08:02.427 回答
0

您不能在视图本身的查询定义中使用 @AllocationDate 和 @LocationName 之类的变量。

相反,您可以做的是

CREATE VIEW `satsschema`.`viewTimeBreak` AS 
SELECT 
    a.EmpName, 
    CONCAT(b.StartTime, '-', b.EndTime) AS ShiftTime, CONCAT(a.EmpTime,     '-', ADDTIME(a.EmpTime, '0 1:0:0.000000')) AS BreakTime, 
    a.Break, 
    a.EmployeeOnBreak, '' AS SignIn, 
    '' AS SignOut,
    a.AllocationDate,
    a.LocationName 
FROM            
    satsschema.employeeslot a 
    INNER JOIN satsschema.ufis b 
            ON b.UFISID = a.UFISEmpGroup

然后把使用视图时的条件

所以你的 SELECT 语句将是

SELECT * FROM `satsschema`.`viewTimeBreak` a 
WHERE 
(a.AllocationDate = @AllocationDate) 
AND (a.LocationName = @LocationName)   
AND (a.Break = 1)
GROUP BY a.EmpName
于 2012-09-13T03:09:04.900 回答