<p>I've been asked to implement some code that will update a row in a MS SQL Server database and then use a stored proc to insert the update in a history table.  We can't add a stored proc to do this since we don't control the database.  I know in stored procs you can do the update and then call execute on another stored proc.  Can I set it up to do this in code using one SQL command?</p>

[{'answer_id': 75422, 'author': 'dpollock', 'author_id': 7884, 'author_profile': 'https://Stackoverflow.com/users/7884', 'pm_score': 0, 'selected': False, 'text': '<p>You can also create sql triggers.</p>\n'}, {'answer_id': 75429, 'author': 'foxxtrot', 'author_id': 10369, 'author_profile': 'https://Stackoverflow.com/users/10369', 'pm_score': 0, 'selected': False, 'text': '<p>Depending on your library, you can usually just put both queries in one Command String, separated by a semi-colon.</p>\n'}, {'answer_id': 75431, 'author': 'JBB', 'author_id': 12332, 'author_profile': 'https://Stackoverflow.com/users/12332', 'pm_score': 0, 'selected': False, 'text': "<p>Insufficient information -- what SQL server? Why have a history table?</p>\n\n<p>Triggers will do this sort of thing. MySQL's binlog might be more useful for you. </p>\n\n<p>You say you don't control the database. Do you control the code that accesses it? Add logging there and keep it out of the SQL server entirely.</p>\n"}, {'answer_id': 75465, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': True, 'text': '<p>Either run them both in the same statement (separate the separate commands by a semi-colon) or a use a transaction so you can rollback the first statement if the 2nd fails.</p>\n'}, {'answer_id': 75470, 'author': 'Nathan Feger', 'author_id': 8563, 'author_profile': 'https://Stackoverflow.com/users/8563', 'pm_score': 1, 'selected': False, 'text': "<p>You don't really need a stored proc for this.  The question really boils down to whether or not you have control over all the inserts.  If in fact you have access to all the inserts, you can simply wrap an insert into datatable, and a insert into historytable in a single transasction.  This will ensure that both are completed for 'success' to occur.  However, when accessing to tables in sequence within a transaction you need to make sure you don't lock historytable then datatable, or else you could have a deadlock situation.</p>\n\n<p>However, if you do not have control over the inserts, you can add a trigger to certain db systems that will give you access to the data that are modified, inserted or deleted.  It may or may not give you all the data you need, like who did the insert, update or delete, but it will tell you what changed.</p>\n"}, {'answer_id': 75852, 'author': 'osp70', 'author_id': 2357, 'author_profile': 'https://Stackoverflow.com/users/2357', 'pm_score': 0, 'selected': False, 'text': '<p>Thanks all for your reply, below is a synopsis of what I ended up doing.  Now to test to see if the trans actually roll back in event of a fail.   </p>\n\n<pre><code>sSQL = "BEGIN TRANSACTION;" &amp; _\n           " Update table set col1 = @col1, col2 = @col2" &amp; _\n           " where col3 = @col3 and " &amp; _\n           " EXECUTE addcontacthistoryentry @parm1, @parm2, @parm3, @parm4, @parm5, @parm6; " &amp; _\n           "COMMIT TRANSACTION;"\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/75347', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2357/']