how to rollback a transaction using rollback command? [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have deleted a record from my table and after executing rollback command it shows "command completed successfully" but when i check it using select statement the table is still looking empty? why ?

Some database connection methods such as ODBC default to what's known as "autocommit" mode - that is, the database connection automatically issues a COMMIT after each statement is executed. JDBC does the same thing. I cannot say if this is what's happening in your case, but if so there's no way to do a ROLLBACK. Best of luck.

Rollback command takes you back to the latest committed state of the table.I guess your delete query might have contained some statement that committed the change(deletion of record).

Jason Clark,
I did a test using MySql and use the "begin", "delete" and "rollback", used the following SQL (an example):
begin; delete from aluno where id = 1; rollback;
In PostgreSQL, SQL syntax is the same and also worked.
Are you sure you used the correct SQL? I might have been some mistake? Is it really necessary to use "begin transaction" instead of just "begin"?
I hope this helps!

Related

SQL Server -Succeed insert statement but records did not load [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
On what scenario can merge/insert/update statement complete successfully without error and not load any records to the target table because of some error, locking or other issue?
I am trying to see what scenario my stored procedure will not fail or throw errors but records would not be loaded in database.
On what scenario can merge/insert/update statement complete successfully without error and not load any records to the target table
Some possibilities:
the DML statement succeeded, but affected 0 rows
it succeeded, but was in a transaction that was subsequently rolled back
you have INSTEAD OF triggers on the table.
This is assuming you're not looking at the wrong table or the wrong database, which is sometimes easy to do.

Bulk Insert with a wrong data in a row [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to set up an automated data workflow for my company, inserting data into a database (microsoft sql server) every Monday.
The "Bulk Insert" statement will insert data row by row. However, if it finds wrong data in the middle, it will stop the process and it won't take out the data that were inserted.
Is there any way that we can validate the data first so that it won't start inserting until it validates that the data are clean to be inserted?
Thank you!
... if it finds wrong data in the middle, it will stop the process and it won't take out the data that were inserted.
Use a transaction, that is exactly what they were made for (to roll back or commit multiple operations as a part of a transaction)
General Remarks
The BULK INSERT statement can be executed within a user-defined transaction to import data into a table or view. Optionally, to use multiple matches for bulk importing data, a transaction can specify the BATCHSIZE clause in the BULK INSERT statement. If a multiple-batch transaction is rolled back, every batch that the transaction has sent to SQL Server is rolled back.
Example:
BEGIN TRANSACTION TrnBlkInsert
BEGIN TRY
-- your bulk insert here
BULK INSERT .....
COMMIT TRANSACTION TrnBlkInsert
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION TrnBlkInsert;
THROW;
END CATCH
Validate every row before insert is a way.
But still something wrong may happen when you do insert.
So, you may consider Transaction. the whole insert in one Transaction.
Any row insert fail. database will rollback all.
with transaction. you don't need validate before.

We can go for transaction management in procedure but we can’t go in function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
We can go for transaction management in procedure but we can’t go in function, I have seen this statement at multiple places, while we ask for difference between function & procedure, But I did below test in oracle and **I can see its working fine for function also**. Can anybody please let me know what thing am I missing about above statement, because this statement looks completely wrong to me?
select * from test; *(test table having single column "name varchar(2)")
create or replace function FUNTest
return number as result NUMBER(6,2);
BEGIN
SAVEPOINT fn_fntest;
insert into test(NAME) values('Dinesh');
ROLLBACK TO fn_fntest;
return 1;
END;
/
Begin
DBMS_OUTPUT.PUT_LINE(FUNTest());
end;
/
Purpose of function is different from procedure.
Function: Supposed to do some calculation and return some value(most
of the cases)
Procedure: Perform some operation based on data/column.It manages transaction as well, because you will definately be storing new data somewhere.
Now talking about transaction management in function, well it depends on calling mechanism of function.
If your function is having transactional statement like commit/rollback then it should be called from some other block which is capable of handling transaction like procedure or anonymous block(your case).
If you call that same function from select statement like "select funtest() from dual;" then you will get error as select statement is not capable of opening transaction.
If you still want to call any function having transactional statement from non-transactional body(select statement) then your function should be capable of opening separate independent transaction(PRAGMA AUTONOMOUS_TRANSACTION).
Please refer to http://www.datacoons.com/content/transaction.php for more information on transaction management.

How to Schedule a trigger? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We had created a trigger for inserting a set of rows.
I want to schedule this trigger automatically and insert these data into another table. i.e, it has to execute automatically based on the period given.
Kindly suggest the possibilities steps for this.
Here i give you the clear explanation about the operation,
My project to transfer the data from MS SQL database table to MY SQL Database table. This event has to be scheduled. I have to select few fields from MS Sql table and that selected data to be transfer to My Sql table Automatically based on Scheduled.
You could use mysql event scheduler to configure and run your job.
Please have look into
http://dev.mysql.com/doc/refman/5.1/en/events-configuration.html
First, Trigger is one database object and trigger created over the any physical table. this is depends on table action so at every action of table trigger will execute.
we cant schedule trigger to execute on perticluar time.
There is alternative solution is: Enable and Disable trigger using scedule SQL Job.
Script is :
--To Enalble Trigger
ENABLE TRIGGER [Trigger_Name] ON [TableName]
GO
--To Disable Trigger
DISABLE TRIGGER [Trigger_Name] ON [TableName]
GO
If you're using MS SQL, then you can use SQL Server Agent to schedule a job/task to execute whatever you need done.
You can read more about it here http://msdn.microsoft.com/en-us/library/ms191439.aspx
Your best bet maybe to create a stored procedure and then in the scheduled job, simply execute that stored procedure to run.
Alternatively, if you don't have SQL Server Agent (requires full version of MS SQL not SQL Server Express) then you could possibly look into using Windows Scheduled Task to schedule a batch file to run periodically, making a call to the sqlcmd utility http://technet.microsoft.com/en-us/library/ms165702(v=sql.105).aspx which you should be able to setup to execute your stored procedure.
Hope this helps.

No connection to tables, sprocs etc [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been working on a database for a couple of weeks now, and suddenly nothing works and I just can't figure out how to solve it.
I can login to SQL server and list all the tables and sprocs. The problem is that when I try to run a sproc I get a message telling me that there are no such table, even though it exists.
When I try to alter or create a sproc I get the following message:
Msg 262, Level 14, State 1, Procedure usp_GetUser, Line 14
CREATE PROCEDURE permission denied in database 'master'.
What can be wrong?
... in database 'master'.
Wrong database context: I assume it should be "MyDatabase" not "master"
You're currently running against the master database rather than whichever user database you have been working on.
Either run
use MyDatabaseName
Or at the top of SSMS there is a drop down that currently says ''master". Change it to your database name