Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert into Table with Active Trigger #21

Closed
nyabutid opened this issue Jan 16, 2015 · 10 comments
Closed

Insert into Table with Active Trigger #21

nyabutid opened this issue Jan 16, 2015 · 10 comments
Labels

Comments

@nyabutid
Copy link

{
  "error": {
    "name": "RequestError",
    "status": 500,
    "message": "The target table 'dbo.tableName' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.",
    "code": "EREQUEST",
    "precedingErrors": [],
    "stack": "RequestError: The target table 'dbo.tableName' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.\n    at Connection.<anonymous> (app/node_modules/loopback-connector-mssql/node_modules/mssql/lib/tedious.js:608:17)\n    at Connection.emit (events.js:95:17)\n    at Parser.<anonymous> (app/node_modules/loopback-connector-mssql/node_modules/mssql/node_modules/tedious/lib/connection.js:450:15)\n    at Parser.emit (events.js:95:17)\n    at Parser.nextToken (app/node_modules/loopback-connector-mssql/node_modules/mssql/node_modules/tedious/lib/token/token-stream-parser.js:103:18)\n    at Parser.addBuffer (app/node_modules/loopback-connector-mssql/node_modules/mssql/node_modules/tedious/lib/token/token-stream-parser.js:80:17)\n    at Connection.sendDataToTokenStreamParser (app/node_modules/loopback-connector-mssql/node_modules/mssql/node_modules/tedious/lib/connection.js:884:35)\n    at Connection.STATE.SENT_CLIENT_REQUEST.events.data (app/node_modules/loopback-connector-mssql/node_modules/mssql/node_modules/tedious/lib/connection.js:289:23)\n    at Connection.dispatchEvent (app/node_modules/loopback-connector-mssql/node_modules/mssql/node_modules/tedious/lib/connection.js:748:59)\n    at MessageIO.<anonymous> (app/node_modules/loopback-connector-mssql/node_modules/mssql/node_modules/tedious/lib/connection.js:676:22)"
  }
}
--I imagine the query used is similar to
INSERT INTO dbo.tableName
        ( ID ,
          Col1 ,
          Col2 
        )
        OUTPUT INSERTED.ID
VALUES  ( 0 ,
          'Col1' ,
          'Col2'
        )
--Microsoft documentation on OUTPUT with TRIGGER enabled: http://msdn.microsoft.com/en-us/library/ms177564.aspx
@nyabutid
Copy link
Author

Current workaround:

Replace

// node_modules/loopback-connector-mssql/lib/mssql.js:157
var sql = "INSERT INTO " + tblName + " (" + fieldsAndData.fields + ")" + MsSQL.newline;
  sql += "OUTPUT INSERTED." + modelPKID + " AS insertId" + MsSQL.newline;
  sql += "VALUES (" + fieldsAndData.paramPlaceholders + ");";

with (use select scope_identity() instead of OUTPUT INSERTED)

// node_modules/loopback-connector-mssql/lib/mssql.js:157
var sql = "INSERT INTO " + tblName + " (" + fieldsAndData.fields + ")" + MsSQL.newline;
  sql += MsSQL.newline;
  sql += "VALUES (" + fieldsAndData.paramPlaceholders + ");SELECT SCOPE_IDENTITY() AS insertId;";

@raymondfeng
Copy link
Contributor

What's going to happen if the PK is not an identity, for example, some sort of uuid?

@nyabutid
Copy link
Author

In my schema the IDENTITY is always the ID. Can this be provided as a configuration option instead to handle these caveats: when triggers are enabled on a table or when your PK is not the IDENTITY on the table? Or can you extend the SCOPE_IDENTITY to return the PK by selecting the row with that ID after an insert (I'd be concerned about performance on this option)?

@idoshamun
Copy link
Contributor

+1

idoshamun added a commit to idoshamun/loopback-connector-mssql that referenced this issue Mar 3, 2015
@tuomastanner
Copy link

+1 Switched to @idosh s fork for the time being and all seems to work perfectly. Big thumbs up!

@SandeshSarfare
Copy link

Hello All,

I have the same issue however mssql.js which I have seems to be completely different and I am not sure how resolve error "The target table 'dbo.Test' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause".......

For an instance I've below code in my mssql.js where Output is inserted

MsSQL.prototype.buildInsertInto = function(model, fields, options,callback) {
var stmt = this.invokeSuper('buildInsertInto', model, fields, options);
var idName = this.idName(model);
if (idName) {
stmt.merge(MsSQL.newline + 'OUTPUT INSERTED.' +
this.columnEscaped(model, idName) + ' AS insertId');
}
return stmt;
};

@SandeshSarfare
Copy link

mssql.js which i have is different than one in this repo.....please suggest a workaround......otherwise we can not go live on production......Thanks in advance

FoysalOsmany added a commit to FoysalOsmany/loopback-connector-mssql that referenced this issue Feb 5, 2016
@FoysalOsmany
Copy link
Contributor

@raymondfeng Can you do the code review and merge?

#70

FoysalOsmany added a commit to FoysalOsmany/loopback-connector-mssql that referenced this issue Feb 5, 2016
raymondfeng added a commit that referenced this issue Feb 10, 2016
#21 Fix for Insert into Table with Active Trigger
raymondfeng pushed a commit that referenced this issue Feb 10, 2016
 * Refactor Fix for Insert into Table with Active Trigger by getting the column data type instead of varchar. #21 (FoysalOsmany)

 * Fix for Insert into Table with Active Trigger #21 (FoysalOsmany)

 * Upgrade should to 8.0.2 (Simon Ho)

 * Add help for Azure SQL users (Oleksandr Sochka)
@stale stale bot added the stale label Aug 22, 2017
@stale
Copy link

stale bot commented Aug 22, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Sep 5, 2017
@stale
Copy link

stale bot commented Sep 5, 2017

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants