请使用SQL Server 2008 / 2008R2 / 2012语法!
ALTER TRIGGER WriteToLoggOnRep
ON dbo.UNITs
FOR UPDATE
AS
BEGIN
DECLARE @changed INT
DECLARE @departmentid INT
IF (UPDATE (DEPARTMENTID))
<!-- info -->
//if a departmentid was change on a unit, that is the case if a unit is sendt to maintenance.(and in other cases, change dep. etc)
//then a new deparmentid is set, the deparmentid set is the MAINTENANCEDEPARTMENTID for the current location
//This integer value will korrenspondere to a DEPARTMENID int the DEPARTMENTs table and
// a MAINTENANCEDEPARTMENTID in the LOCATIONs table.
// The challenge is to find out whether this id is actually a maintenance id/ maintenance department or not..
// ALL maintenance departments are named MAINTENANCE in the DEPARTMENTs table
// Departments are related/connected to location and not universal among locations
// All Locations have a "MAINTENANCE" and a "IN STOCK" department created initially
SELECT DEPARTMENNAME FROM DEPARTMENTs WHERE DEPARTMENTID = UPDATE.DEPARTMENTID INTO @DepartmentName;
IF(@DepartmentName = 'MAINTENANCE')
SELECT @changed = COUNT(*) FROM Inserted i INNER JOIN Deleted d
ON i.UNITID = d.UNITID WHERE ISNULL(i.UNITID,'') = @departmentid
IF(@changed > 0)
INSERT INTO LOGGs
(DATE, NOTE, UNITID)
SELECT CURRENT_TIMESTAMP,'SENDT TO Maintenance', UNITID
FROM inserted
END
END
<!----->
触发目的:
如果将单元发送到维护并写入一些信息,则写入logg:
(我也从保养中回来做类似的事情)
- curr Datetime
- “发送到维护”
- UNITID
(发送给维护的单位的ID
涉及的表格:
UNITs
UNITID (AI) (P)
SERIALNR
DEPARTMENTID*
DEPARTMENTs
DEPARTMENTID (AI) (P)
DEPARTMENTNAME (Of course, there are several departments called maintenance)
LOCATIONID*
LOCATIONs
LOCATIONID (AI) (P)
LOCATIONNAME
MAINTENANCEDEPARTMENTID (for that current location)
INSTOCKDEPARTMENTID (for that current location)
我需要从表DEPARTMENTs
,列DEPARTMENTID
(INT
auto inc。)中获取值。
对于
部门我希望并认为这是“接近”,但编译器在几个位置标记问题......
我还考虑将此写入包含在将该单元发送到维护的查询的日志中吗?
但是,有一个独立的触发器来修改它更好吗?
可能有更简单,更好的方法来解决这个问题?我愿意接受建议! 如果我没有提供足够的信息,请告诉我!
请使用SQL Server 2008 / 2008R2 / 2012语法!
答案 0 :(得分:0)
随便看一下代码,注意到这不是有效的SQL Server语法:
SELECT DEPARTMENNAME FROM DEPARTMENTs WHERE DEPARTMENTID = UPDATE.DEPARTMENTID INTO @DepartmentName;
试试这个:
SELECT @DepartmentName = DEPARTMENNAME
FROM DEPARTMENTs
WHERE DEPARTMENTID = UPDATE.DEPARTMENTID ;
这可能会解决您的问题。
答案 1 :(得分:0)
通过将此功能集成到存储函数中解决了这个问题,该函数是从将设备发送到维护的查询中调用的。