1

请使用 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

<!----->

触发器的目的:如果一个单元被发送到维护,则写入日志并用一些信息标记它:(我也在为维护返回做一个类似的)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、列DEPARTMENTIDINTauto inc.)中获取一个值

对于那个部门

我希望并认为这是“接近”,但编译器在多个位置标记问题......

我还考虑过将此写入日志的内容包含在将设备发送到维护的查询中?

但是,最好有一个独立的触发器来修改它?

很可能有一种更简单更好的方法来解决这个问题?我愿意接受建议!如果我没有提供足够的信息,请告诉我!

请使用 SQL Server 2008 / 2008R2 / 2012 语法!

4

2 回答 2

0

通过将其集成到存储函数中解决了该问题,该函数从将单元发送到维护的查询中调用。

于 2013-08-08T06:45:32.000 回答
0

随便看一眼代码就会发现这不是有效的 SQL Server 语法:

SELECT DEPARTMENNAME FROM DEPARTMENTs WHERE DEPARTMENTID = UPDATE.DEPARTMENTID INTO @DepartmentName;

尝试这个:

SELECT @DepartmentName = DEPARTMENNAME
FROM DEPARTMENTs
WHERE DEPARTMENTID = UPDATE.DEPARTMENTID ;

这可能会解决你的问题。

于 2013-08-03T14:09:49.697 回答