2012-09-13 126 views
1

我想爲此sql語句創建一個以前工作正常的視圖。但是當我將它創建爲視圖時,出現一個錯誤提示,"View's SELECT contains a variable or parameter."任何人都可以告訴我錯誤在哪裏,因爲我對MySQL中的視圖很陌生。謝謝!在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語句創建視圖時的錯誤。

+0

的可能重複[燦我用MySQL中的參數創建視圖?](http://stackoverflow.com/questions/2281890/can-i-create-view-with-parameter-in-mysql) – zerkms

回答

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 
+0

select語句提示錯誤,指出「未知列a.AllocationDate在where子句中」對此有何想法? – Philemon

+0

您是否使用我的查詢查看?你會注意到,我已經在a.AllocationDate和a.LocationName的視圖定義查詢中添加了2列,因爲你需要它們在where子句 – InSane

+0

哦好吧注意到了。謝謝 – Philemon

1

VIEW s 不能像存儲過程和函數那樣有參數。 SO用戶以前已回覆此問題:view with parameter

+0

好奇你爲什麼不用d關閉投票重複鏈接。 – zerkms