3

我必须将 MSSQL 中所做的更改与远程 MySQL 数据库同步。要同步的更改是向系统添加发票和用户。远程服务器预计不会始终可访问,因此我正在尝试设置一种日志表来存储在 MSSQL 中所做的更改。

这是一个完全有效的触发器:

CREATE TRIGGER [dbo].[dokument_insert]
   ON [dbo].[dokument]
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;
    INSERT INTO [bcg_ekodu].[dbo].[sync_stack] (event,sql, table_name, import_priority)
        SELECT 
            'INSERT',
            'INSERT INTO bills SET
                date = "'+CONVERT(VARCHAR(19),dok_kuup,120)+'", 
                total = "'+CAST(kokkusum AS nvarchar)+'",
                number = "'+RTRIM(dok_nr)+'", 
                created = "'+CONVERT(VARCHAR(19),savetime,120)+'", 
                rounded = "'+CAST(ymardus AS nvarchar)+'",
                currency = "'+CAST(valuuta AS nvarchar)+'", 
                due_date = "'+CONVERT(VARCHAR(19),tasupaev,120)+'", 
                pk_joosep = "'+CAST(dok_kood AS nvarchar)+'",
                joosep_hankija = "'+CAST(hankija AS nvarchar)+'";
             UPDATE
                bills, users, companies 
             SET 
                bills.user_id = users.id,
                bills.imported = NOW()
             WHERE
                bills.imported IS NULL
                AND companies.id = users.company_id
                AND companies.pk_joosep = 10
                AND bills.user_id = users.pk_joosep',
            'bills',
            '200'
        FROM inserted
END

每次将一行插入“dokument”表时,它都会在“sync_stack”表中插入一行。'sql' 列将包含一个 SQL,用于在另一个 (MySQL) 数据库中创建相同类型的行。

但是这个触发器不起作用:

CREATE TRIGGER [dbo].[klient_insert]
   ON [dbo].[klient]
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;
    INSERT INTO [bcg_ekodu].[dbo].[sync_stack] (event,sql, table_name, import_priority)
        SELECT 
            'INSERT',
            'INSERT INTO users SET
                username =10'+CAST(kl_kood as nvarchar)+',
                password = NULL,
                name ="'+LTRIM(RTRIM(kl_nimi))+'",  
                email ="'+CAST(LTRIM(RTRIM(kl_email)) as nvarchar)+'",  
                reference_no ="'+CAST(LTRIM(RTRIM(kl_viide)) as nvarchar)+'",   
                phone ="'+CAST(LTRIM(RTRIM(kl_tel1)) as nvarchar)+'",   
                logins ="'+CAST(0 as nvarchar)+'",  
                last_login = NULL,  
                created ="'+CONVERT(VARCHAR(19),savetime,120)+'",   
                updated = NULL, 
                deleted ="0",   
                address ="'+CAST(LTRIM(RTRIM(kl_aadr1)) as nvarchar)+'",    
                pk_joosep ="'+CAST(kl_kood as nvarchar)+'"',
            'users',
            '210'
        FROM inserted
END

虽然执行上述 SQL 以创建该触发器可以正常完成,但当我尝试将一些行插入“触发”表时,我收到以下错误:

No row was updated.

The data in row 175 was not committed.
Error Source: .Net SqlClient Data Provider.
Error Message: Cannot insert the value NULL into column 'sql', table 'mydb.dbo.sync_stack'; column does not allow nulls. INSERT fails.

The statement has been terminated.

Correct the errors and retry or press ESC to cancel the change(s).
  • 如果我删除此触发器,则不会发生此错误。
  • 如果我只为“sql”列插入纯文本,它会按预期工作。
  • 如果我使用插入行中的任何字段,即使只是一个文本字段,它也会再次失败。
  • 如果我在 'sql' 列中允许 NULL 值,插入行会成功,但我在 'sql' 列中得到一个 NULL 值。

如何使第二个触发器也按预期工作?

4

2 回答 2

8

我怀疑您要连接到 SQL 语句中的插入值中至少有一个是NULL. 您可以通过使用来规避这种情况COALESCE,例如

username =10'+COALESCE(CAST(kl_kood as nvarchar), '')+',

当然,你不应该在nvarchar没有指定长度的情况下声明,对吧?

于 2012-04-17T21:52:26.107 回答
-1

将任何值连接到 NULL 是 NULL:

select 'test' + NULL

结果为空,您应该对列使用类似的内容:

select isnull(column, '')

这将导致一个空字符串。

于 2012-04-17T21:50:24.980 回答