What happens if you simply call your working stored procedure from a trigger (via EXEC ) instead of including the procedure code in the trigger?
One note is the place in the code:
-- Collect field values that were just inserted SELECT @EMAIL = rtrim(OEORDH1.SHPEMAILC), @SHPCONTACT = rtrim(SHPCONTACT), @ORDNUMBER = rtrim(ORDNUMBER), @LOCATION = LOCATION, @EXPDATE = EXPDATE, @SHPPHONEC = rtrim(OEORDH1.SHPPHONEC), @SHPNAME = SHPNAME, @DESCR = rtrim([DESC]), @CODEEMPL = rtrim(ARSAP.CODEEMPL) -- FROM inserted FROM dbo.OEORDH JOIN dbo.OEORDH1 on dbo.OEORDH.ORDUNIQ = dbo.OEORDH1.ORDUNIQ JOIN dbo.ARSAP on dbo.OEORDH.SALESPER1 = dbo.ARSAP.CODESLSP WHERE ORDNUMBER = @ORDNUM
You commented on FROM inserted and try to read the values โโfrom the table directly.
When the startup code is run, the transaction has not yet completed, so most likely you should read the values โโfrom the inserted table. This SELECT will probably not find the row with the given @ORDNUM , and the variables remain NULL .
Side note. Triggers in SQL Server run once per statement, not once per line. Your trigger should work correctly, even if the inserted table has multiple rows. Right now, your trigger will select only one ORDNUMBER , even if multiple rows have been inserted into the table. Most likely, this is not what you want.
How to debug a trigger?
One simple and very simple way is to create a table for logging and add many INSERT that will write the values โโof all variables to this table. Then you can read the magazines and see what happens.
source share