用FUNCTION练习SQL CHECK CONSTRAINT

时间:2015-11-29 10:36:31

标签: sql-server tsql

我正在尝试创建阻止来自非服务器的员工的订单。这是我的diagram项目。我创建的代码运行正常,但我仍然可以插入不是“服务器”的员工

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

现在我收到了错误消息 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。 在跑步时

ALTER TABLE orderInfo
ADD CONSTRAINT chk_position CHECK (dbo.chk_server_position(employee_ID) = 'true')

哪种方法更接近结果?

2 个答案:

答案 0 :(得分:0)

您的"最后一次选择&#34>您的记录总是被插入员工不是服务器,您错过了对给定员工的过滤。

You Haven在您的USP的Where子句中使用了@employee_ID参数。 修改你的where子句,如: CREATE FUNCTION dbo.chk_server_position(@employee_ID int) RETURNS varchar(50) AS BEGIN DECLARE @position varchar(50); DECLARE @output varchar(50) = 'false'; SELECT @position = position FROM employee inner join orderInfo ON employee.employee_ID = orderInfo.employee_ID WHERE employee.employee_ID = @employee_ID) IF (@position = 'server') SET @output = 'true' return @output; END; GO

更新: 为什么需要在USP中选择@EmployeeID?你可以写下面的内容:

{{1}}

答案 1 :(得分:0)

没关系,我用public void createExcel(){ try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); HSSFWorkbook workBook = new HSSFWorkbook(); HSSFSheet sheet = workBook.createSheet("hello world"); HSSFRow row = sheet.createRow((short) 0); HSSFCell cell; cell = row.createCell(0); cell.setCellValue(new HSSFRichTextString("Hello")); cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString("world")); workBook.write(bos); bos.flush(); FacesContext fc = FacesContext.getCurrentInstance(); downloadExcel(bos, fc); } catch (FileNotFoundException e) { log.debug("file not found ", e); } catch (IOException e){ log.debug("IOException ", e); } } public void downloadExcel(ByteArrayOutputStream baos, FacesContext fc){ HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse(); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=testxls.xls"); try { ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); out.close(); } catch (IOException e) { log.error("IOException ", e); } fc.responseComplete(); }

解决了这个问题
TRIGGER