Why do triggers try to insert a NULL value when using a field from an "inserted" table?

I need to synchronize changes made in MSSQL with a remote MySQL database. The changes that need to be synchronized are the addition of invoices and users to the system. It is expected that the remote server will always be available, so I am trying to set up a log table to store changes made in MSSQL.

Here is a complete working trigger for this:

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

It inserts a row into the sync_stack table every time a row is inserted into the dokument table. The "sql" column will contain SQL to create the same row in another (MySQL) database.

But this trigger does not work:

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 , "triggered", :

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', , .
  • , , .
  • NULL "sql" , , NULL "sql" .

, , ?

+3
2

NULL NULL:

select 'test' + NULL

null, - :

select isnull(column, '')

.

-1

, , , SQL, NULL. , COALESCE,

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

, nvarchar , ?

http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx

+8

All Articles