插入触发器:需要将输入的字符串分成两个不同的字段

问题描述 投票:1回答:1

我的数据库正在由连接到PLC的kepware填充。我的kepware标签名称为“ H-T1XX2.Distro_PLC.St 10循环计时器”,我想根据时段(“。”)进行拆分。“ H-T1XX2'= WorkCenter,而“ St 10循环计时器” =工作站。我们将提供任何帮助。

USE [StationCycleTime]
GO
/****** Object:  Trigger [MES].[plcPRODInsert]    Script Date: 1/24/2020 11:04:58 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[plcInsert]
ON [dbo].[tbl_CycleTime]
AFTER INSERT
AS
BEGIN
    declare @WC varchar(10)
    declare @ST varchar(20)
    declare @InStr varchar(50)
    declare @MaxID varchar(50)

    SELECT @MaxID = n.ID
    FROM [dbo].[tbl_CycleTime] n
    WHERE n.[ID] = MAX(ID)

    SELECT @InStr = m.Station
    FROM [dbo].[tbl_CycleTime] m
    WHERE m.[ID] = @MaxID

    SET @WC = (SELECT PARSENAME(@InStr,3))
    SET @ST = (SELECT PARSENAME(@InStr,1))

    SET NOCOUNT ON;
    UPDATE  [dbo].[tbl_CycleTime]
    SET [WorkCenter] = @WC, [Station] = @ST 
    WHERE [ID] = (
        SELECT MAX(ID) from [dbo].[tbl_CycleTime])
END

Error I am receiving

sql sql-server
1个回答
0
投票

您可以尝试这个。

 UPDATE  [dbo].[tbl_CycleTime]
    SET [WorkCenter] = @WC, [Station] = @ST 
    WHERE [ID] = (
        SELECT TOP 1 ID from [dbo].[tbl_CycleTime] ORDER BY ID DESC)

推荐问答