Can we call truncate command in trigger command using sql server? - sql-server

Can we possible to call truncate command inside trigger using sql server?

TRUNCATE command is a transactional command and all the pages deleted will be writed into the transaction log. And because the trigger code is envolved in a transaction, you can ROLLBACK or COMMIT the implicit transaction into the trigger code (COMMIT is not a good practice into trigger code).
So, yes, you can !

Related

ssis transaction with out Msdtc

One of the packages is going to implement using
SQL Server Integration Services SSIS Transactions without MSDTC.
The Execute SQL task has placed ,before the data flow(Df_insert) for begin transaction.There are several update steps and index creation steps ,after this First data flow(Df_Insert).There is an update scripts which is in another sequence container and ,need to be part of this transaction.
Is there any way to include only the Df_insert and the update scripts
in the transaction.
The control flow looks like, the below
From SQL Transaction point of view ALL DML statements, i.e. inserts-updates-deletes, between BEGIN TRAN and COMMIT are part of this transaction and not deducible. Your task - committing only DFT and update script - means that update, update2 and delete are temp data used in your update script and discarded later on.
Approach - rework your logic to move results of update, update2 and possibly delete results into TEMP tables and use it afterwards. Regular #temp_table will be fine since you have to use RetainSameConnection=true for transaction without MSDTC.

What happens to connections when taking SQl Server Database Offline?

I have recently tried a big merge of 2 databases. We recreated the schema from Database 2 into Database 1 and created a script to transfer all data from database 2 into Database 1. This script takes about 35 min to run and have transaction handling with:
BEGIN TRANSACTION
...
IF(##error<>0)
COMMIT TRANSACTION
ELSE
ROLLBACK TRANSACTION
The full script is a bit sensitive but here is some SQL that have the same structure: http://pastebin.com/GWJ3ZnkF
We ran the script and all data was transfered without errors. We tested the systems running with the new combined database (removed access rights to the old database).
But as a last task we wanted to take the old database offline to make sure no one used that database. To do this we used:
ALTER DATABASE <dbname> SET OFFLINE WITH ROLLBACK IMMEDIATE
This was bad. After this line of SQL code all data in the combined database that we just copied was suddenly gone. I first asumed it wasn't really finished so the "Rollback immediate" sounds like it have performed a rollback on my transaction..
But why? Wasn't the transaction allready committed?
Also I tried running the same script again a few times but after every attempt no data was copied even if it said the script was successfull. I have no idea why... did it remember my offline rollback somehow?
What is really happening to my connections?
Sounds like you had a pending transaction uncommitted and you forced it to rollback, loosing some of the work. The rest is explained by how your scripts are structured. Is unlikely your script had a single transaction from start to bottom. Only the last transaction was rolled back, so the database was left now in a state in which it is 'half copied'. Probably your script does various checks and this intermediate state sends the script on the 'ELSE' branches where it does not do the proper work (ie. apparently does nothing).
W/o posting the exact script, is all speculation anyway.
Right now you need to restore the database to a consistent state, the one before your data copy. Use the backup you took before the data move (you did take a backup, right?). for extra credit, make sure your script is idempotent and works correctly on a half-updated database.
I'd double-check to make sure that there are no outstanding transactions. Either go through the file and count the number of BEGIN TRANSACTION vs COMMIT TRANSACTION lines, or add a statement to the end of it to SELECT ##TRANCOUNT to ensure that there are no open transactions remaining.
If your data has been committed, there should be no way it can be lost by disconnecting you.
WITH ROLLBACK IMMEDIATE:
All incomplete transactions will be rolled back and any other
connections to the database will be
immediately disconnected.
Sounds like someone got the 2 databases mixed up or maybe there is an outstanding transaction?.... Can you post your entire script?
Ref: ALTER DATABASE.
Rather than only checking ##ERROR, inspect ##TRANCOUNT as well.

Script for running DDL statements in a transaction

I am trying to execute a bunch of DDL statements in a transaction scope. I am trying to execute all the statements that relate to a model change request as as a single transaction so that all the DDL statements fail or succeed together. My objective is not to leave the DB in an inconsistent statement after the execution of the group of DDL statements.
I have found that SQL Server 2008 R2 supports transactions on DDL statements. I am not talking about DROP DB kind of DDL statements - I am referring to CREATE TABLE, ALTER TABLE, DROP TABLE, etc.
I have read the following related threads but did not find an answer.
Is it possible to run multiple DDL statements inside a transaction (within SQL Server)?
Unit testing DDL statements that need to be in a transaction
DDL scripts in a transaction block takes effect even when there are errors
What I need is a template script for executing a set of DDL statements as a transaction and to roll them back if one of the statements fail and I want the error to be printed or to be stored in an error table. Can anyone help?
In my research, I have found multiple alternatives, but I am not sure which one to pick as I am new in this area. I need some help from experienced hands.
Use XACT_ABORT in the transaction scope to abort on first error within the transaction
Use a TRY and CATCH block and put the DDL statements inside a transaction inside TRY block
Here are the pages that I have read through.
http://msdn.microsoft.com/en-us/library/ms179296.aspx
http://msdn.microsoft.com/en-us/library/ms188792.aspx
http://www.codeproject.com/KB/database/sqlservertransactions.aspx
Download Red Gate SQL Compare and see how the scripts are generated there.
This does transactional DDL and can be extended for logging.
TRY/CATCH doesn't span batches which makes is trickier to use without dynamic SQL

Sql Server 2005 Transactions For Deployment Scripts

I want to wrap my SQL deployment script in a transaction (containing a bunch of schema changes). I am doing this because if one part of it fails, I want the db to revert to what it was before I ran the script.
I have a few simple questions I would like to have resolved prior to pushing these changes:
Is it necessary to explicitly call COMMIT on the transaction at the bottom of the script?
Is it necessary to explicitly check for errors and call ROLLBACK at the bottom, or will simply using a transaction provide this effect?
Yes.
Yes.
You should also investigate SET XACT_ABORT ON. SET XACT_ABORT ON instructs SQL Server to rollback the entire transaction and abort the batch when a run-time error occurs.
This article Error Handling in SQL 2005 and Later is worth reading.

How to rollback SQL update command?

In SQL server 2008 R2. Is it possible to do a rollback on a single update command?
I know there are other questiones like this on SO but i havent seen one specific for 2008 R2 and hence I may get the same answer, if that is the case then we can close this thread.
I did the a simple update without any transactions commands:
UPDATE myTable SET col1=somevalue WHERE....
Of course you can use explicit transactions such as
BEGIN TRAN
UPDATE ...
ROLLBACK
but I don't think you are asking about that?
If you have the option SET IMPLICIT_TRANSACTIONS ON then the command will not be committed or rolled back until you do so explicitly but this is not the default behaviour.
By default transactions are auto committed so when the command finishes successfully the results of the update will be committed. If the update was to encounter an error - including the connection being killed mid update it would auto rollback.
If your database is in full recovery mode you might want to try reading transaction log, finding which rows were affected and then reverting the update.
However, this is not supported by default, because MS stored transaction log in its own format that is not well documented.
Solution is to use commands such as DBCC LOG or fn_log or third party tool such as ApexSQL Log which does all of this automatically but comes with a price.
If you need more details, here are couple of posts on reading transaction log:
Read the log file (*.LDF) in sql server 2008
SQL Server Transaction Log Explorer/Analyzer

Resources