3

I've got a set of triggers in my table in SQL Server and when I execute the queries in SQL Server Management Studio they work fine. But when they're executed from my php files they doesn't take effect. I used SQL Server Profiler and the trigger gets to the last line of execution that is the insert in my log table but then after this I get an Attention error. Here's my trigger for a delete statement:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- Batch submitted through debugger: SQLQuery25.sql|7|0|C:\Users\ADMINI~1\AppData\Local\Temp\3\~vsB4EE.sql

ALTER TRIGGER [dbo].[OperationStructureFields_delete]
ON [dbo].[OperationStructureFields]
FOR DELETE
AS
BEGIN
    DECLARE
        @id INT,
        @result varchar(MAX),
        @user varchar(MAX),
        @LoopCounter INT = 1, 
        @MAX INT, 
        @Column NVARCHAR(100),
        @Type NVARCHAR(100),
        @Value NVARCHAR(100),
        @ValueXML xml,
        @Sql NVARCHAR(MAX),
        @Tmp NVARCHAR(MAX),
        @LoopCounter2 INT = 1,
        @MAX2 INT,
        @Message nvarchar(2048)
    SELECT @user = system_user
    SELECT @MAX = MAX(ORDINAL_POSITION) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'OperationStructureFields'
    SELECT @MAX2 = MAX(ID) FROM deleted
    SELECT @LoopCounter2 = MIN(ID) FROM deleted
    Select * into #deleted from deleted
    WHILE(@LoopCounter2 <= @MAX2)
    BEGIN
        SET @LoopCounter = 1
        SET @result = '{'
        WHILE(@LoopCounter <= @MAX)
        BEGIN
           SELECT @Column = COLUMN_NAME, @Type = DATA_TYPE
           FROM INFORMATION_SCHEMA.COLUMNS WHERE ORDINAL_POSITION = @LoopCounter and TABLE_NAME = 'OperationStructureFields'
           if (@Type = 'xml')
           BEGIN
            SET @Sql = 'SELECT @ValueXML = ' + @Column + ' FROM #deleted where ID=' + CONVERT(varchar(200),@LoopCounter2,0)
            exec sp_executesql @Sql, N'@ValueXML xml out', @ValueXML out
            SET @Value = CONVERT(VARCHAR(MAX),@ValueXML,0)
           END
           ELSE
           BEGIN
             SET @Sql = 'SELECT @Value = ' + @Column + ' FROM #deleted where ID=' + CONVERT(varchar(200),@LoopCounter2,0)
             exec sp_executesql @Sql, N'@Value varchar(MAX) out', @Value out 
           END    
           IF (@Value is not null or @Value != '') and  (@Type = 'datetime2' or @Type = 'datetime' or @Type = 'date')
           BEGIN
                IF @result = '{'
                BEGIN
                    SET @result = @result + ' "' + @Column + '":"' + CONVERT(VARCHAR(20),@Value,120) + '"'
                END
                ELSE
                BEGIN
                    SET @result = @result + ', "' + @Column + '":"' + CONVERT(VARCHAR(20),@Value,120) + '"'
                END
           END
           ELSE IF (@Value is not null or @Value != '')
           BEGIN
                IF @result = '{'
                BEGIN
                    SET @result = @result + ' "' + @Column + '":"' + @Value + '"'
                END
                ELSE
                BEGIN
                    SET @result = @result + ', "' + @Column + '":"' + @Value + '"'
                END
           END
           SET @LoopCounter  = @LoopCounter + 1 
        END
        SET @result = @result + '}'
        INSERT INTO sys_logs (username,datahora,tabela,[object_id],[action],oldvalue) values (@user,GETDATE(),'OperationStructureFields',@LoopCounter2,'DELETE',@result)
        delete from #deleted where ID = @LoopCounter2
        select @LoopCounter2 = MIN(ID) from #deleted where ID > @LoopCounter2  
    END
END

And a print of my SQL Server Profiler:

Prt Screen do SQL Server Profiler

The PHP code is:

$res = sqlsrv_query($connection, $_sql, array(), array('Scrollable' => 'buffered'));

and the variable $_sql has the following value:

DELETE FROM OperationStructureFields WHERE ID= '66817'

And it doesn't return any errors.

The PHP version is 5.5.16.

I executed the SELECT @@OPTIONS inside my php file and got the following options:

ANSI_WARNINGS
ANSI_PADDING
ANSI_NULLS
QUOTED_IDENTIFIER
ANSI_NULL_DFLT_ON
CONCAT_NULL_YIELDS_NULL

The only difference to the SQL Server Management Studio is the option ARITHABORT that is ON in the SQL Server Management Studio.

Do you have any ideia what may be causing this?

UPDATES:

I believe it may be something on my PHP settings. In my phpinfo() I've got the following settings for sqlsrv:

phpinfo();

Also in SQL Server Profiler I've got the following definitions in Audit Login:

-- network protocol: LPC
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed
patricia
  • 1,075
  • 1
  • 16
  • 44
  • I don't have much experience with SQL Server via PHP, but it looks like your options array may not be right. 'buffered' is not one of the values listed in [the documentation](http://php.net/manual/en/function.sqlsrv-query.php). I don't know if that would cause this problem, though. – Don't Panic Aug 25 '16 at 15:32
  • @Don'tPanic it isn't that option that's causing the errors. :s – patricia Aug 25 '16 at 15:42
  • is it possible that you are dealing with permissions issues across databases? Is sys_logs a local table in your DB or other? – Dave Aug 30 '16 at 14:41
  • @Dave I'm using the same user/password in PHP and SQL Server Management Studio. – patricia Aug 30 '16 at 14:52
  • @Dave I've updated my question. – patricia Aug 31 '16 at 17:17
  • @patricia So I would actually just try steps...comment out the offending line and re-execute the process to see if it is that line specifically...I've run into issues before where the line referenced in errors is not the actual issue. If the process goes through then you know it is that SQL statement. If not, it will probably start you on the path to tracking down the real issue. – Dave Aug 31 '16 at 19:46

2 Answers2

1

Messages

Changed database context to XXXXXXX

Changed language setting to YYYYYYY

are not error messages. They are informational messages and should be ignored by applications. PHP can be configured to ignore it.

Severity Levels: https://msdn.microsoft.com/en-us/library/ms164086.aspx

PHP Settings:

1) php.ini changes:

mssql.min_error_severity = 11
mssql.min_message_severity = 11

or

2) by executing

mssql_min_error_severity(11);

just before execution of mssql_query()

http://php.net/manual/en/function.mssql-min-error-severity.php

UPDATE:

3) The trigger should have

set nocount on;

at the top of code, just after:

AS
BEGIN

This will prevent sending multiple informational messages (like "xx rows affected") to the application.

Anton
  • 2,846
  • 1
  • 10
  • 15
  • I was more concerned with the `Attention` error. Something isn't right because I can run the statement in SQL Server Management Studio without problems but with PHP it fails without error messages. :S – patricia Aug 31 '16 at 12:10
  • I've changed those settings just in case but the error persists. – patricia Aug 31 '16 at 12:14
  • Could be other type of setting? – patricia Aug 31 '16 at 15:07
  • 2
    Do you use transaction when you make a call to mssql? Is there a change that transaction is not committed? Also could you add 'set nocount on;' to the top of your trigger - it's good practice to have it for SPs and triggers. – Anton Sep 01 '16 at 00:16
  • Also try to execute sqlsrv_configure("WarningsReturnAsErrors", 0); before running sql query. I know it's already off in your config file... but let's try it anyway. – Anton Sep 01 '16 at 00:19
  • 1
    I tried the `set nocount on` in the beginning of my trigger and it seems to work now. I'm still doing some tests but seems like that was the problem. Can you update your answer so I can then give you the +50 ? – patricia Sep 01 '16 at 10:26
  • @patricia, the answer has been updated. I'm glad you solved it :) Cheers. – Anton Sep 01 '16 at 23:37
0

Each of the application SQL drivers (including SSMS) have standard SET Options. Those can be overridden, but by default, the driver is going to pass certain setting values. (If you don't know what I am talking about take a look at this article to get an idea of what the options are and to find out how to determine which options are being used.

What I would suggest would be to determine what SET Options your application is using, then mimic those in SSMS and then troubleshoot from there. Your other option would be to go ahead and perform set statements from your app to be consistent with SSMS's default. We did something similar to that in a Node.js application a few years because we were getting a number of errors returned from procs that ran fine in .Net. Once we added statements in Node to parrot .Net everything worked fine.

I am not positive that your issue is due to SET Options, but I think it is likely.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
btberry
  • 377
  • 1
  • 7
  • The only difference was the `ARITHABORT` option and I tried turning that on but the error persists. – patricia Aug 26 '16 at 10:04
  • Have you tried executing in SSMS with the same login that your PHP application is using? – btberry Aug 26 '16 at 17:27
  • I'm using the same login as the PHP application :s – patricia Aug 29 '16 at 09:34
  • This is a long shot and it's kind of hard to set up a test for it on my end, but are you connecting to the same database through both SSMS and PHP? I am not sure that would affect anything, but I am wondering if that could possible change the database context on anything in the trigger. Does your app connect to that same database? This is a shot in the dark but I cannot come up with anything else if your SET options are the same and your login is the same. – btberry Aug 30 '16 at 23:40
  • It's the same database, I've checked that. – patricia Aug 31 '16 at 00:31
  • Bleh, I'm sorry for you. Hope you get it figured out. Last thing I would suggest would be taking apart the trigger and slowly introducing one part at a time to verify a) it is the trigger and b) what part of the trigger is a problem. Other than that I am out of suggestions (unless you get more info from doing that.) If it's not the trigger, there could be something else entirely going on. I have been assuming all along it's something with the trigger, but that may have been in error. – btberry Aug 31 '16 at 00:37
  • I think its something in my php settings and not in my trigger execution :s – patricia Aug 31 '16 at 14:41