我試圖從不是服務器的員工創建阻止訂單。這是我的diagram該項目。我的代碼,我創建運行正常,但我還可以憑誰不是在「服務器」練習SQL CHECK CONSTRAINT with FUNCTION
CREATE FUNCTION dbo.chk_server_position(@employee_ID int)
RETURNS varchar(50)
AS
BEGIN
DECLARE @position varchar(50);
DECLARE @employeeID int;
DECLARE @output varchar(50);
SELECT @position = employee.position, @employeeID = orderInfo.employee_ID
FROM employee, orderInfo
WHERE employee.employee_ID = orderInfo.employee_ID
IF (@position = 'server')
SET @output = 'true'
return @output;
END;
GO
ALTER TABLE orderInfo
ADD CONSTRAINT chk_position CHECK (dbo.chk_server_position(employee_ID) = 'true')
顯然,我已經改變了代碼,這
CREATE FUNCTION dbo.chk_server_position(@employee_ID int)
RETURNS varchar(50)
AS
BEGIN
DECLARE @position varchar(50);
DECLARE @employeeID int;
DECLARE @output varchar(50);
SET @employeeID = (SELECT employee_ID FROM orderInfo)
SET @position = (SELECT position FROM employee, orderInfo
WHERE employee.employee_ID = orderInfo.employee_ID AND employee.employee_ID = @employee_ID)
IF (@position = 'server')
SET @output = 'true'
return @output;
END;
GO
員工插入和現在,我得到錯誤信息 子查詢返回多個值。當子查詢遵循=,!=,<,< =,>,> =或當子查詢用作表達式時,這是不允許的。 運行
ALTER TABLE orderInfo
ADD CONSTRAINT chk_position CHECK (dbo.chk_server_position(employee_ID) = 'true')
哪種方法更接近導致什麼時候?
試過了,仍然無法正常工作,我仍然可以插入EMPLOYEE_ID爲了誰不是「服務器「 –