How to rollback database when user abort process? - sql

I have createde a stored procedure such as:
CREATE PROCEDURE backupDB
BEGIN
...
exec('BACKUP DATABASE '+#targetDbName+' TO DISK = ''C:\ABC\'+#backupFileName+'.bak''')
...
END
I would like to rollback database when user abort backup Database process. For example, I create a button named "Cancel" and when user click it, all process in procedure 'backupDB' will be rollolbacked.
So, How can do it?
I use MS SQL Server 2008 R2 and Visual studio 2013 with ASP.NET MVC 5.
Thanks for your help.

You can apply Transaction in C# code or inside your stored procedure.Transaction

Related

Execute custom sql procedure in MS Master Data Services 2012 Database

My goal is to launch stored procedure from another DB (not MDS DB) when user commits Verion to start ETL process. For that in MDS DB i've added launching custom stored procedure in [mdm].[udpVersionSave].
But when i'm trying to commit Version in MDS Web interface, nothing happens - Version doesn't become commited. BTW, when i launch procedure from MDS DB - it's working.
My guess - the problem is in user\login access. But i try a lot combinations in giving access - nothing helped.
UPD.:
Code which launches stored procedure:
execute [AdventureWorksDW2012].[dbo].[sp__test_insert_data] #code = N'3';
Code of procedure sp__test_insert_data:
insert into AdventureWorksDW2012.dbo._test_insert_data(col_ver)
values (#code);
Procedure sp__test_insert_data works fine, when i'm launching it manually under sa.
Any ideas?

Backup and restore SQL Server Management Studio job schedule

I've got a job in SQL Server Management Studio and I want to back up the schedule that it runs on so that the schedule can be applied to other jobs that I add. I know that I can get what I assume is the data I need to copy from using the following:
-- lists all aspects of the information for the job NightlyBackups.
USE msdb ;
GO
EXEC dbo.sp_help_job
#job_name = N'NightlyBackups',
#job_aspect = N'SCHEDULES' ;
GO
I'm just wondering how I can store the results of this stored procedure in a way that will allow me to add it to other jobs on the system. Preferably in T-SQL .
The GUI method:
Right-click the job in SSMS and script it as CREATE; alter parameters to suit.
The T-SQL method:
I don't have that on-hand, but try opening Profiler, look for SQL:Completed and RPC:Completed, and then do the GUI method - you should capture the T-SQL that SSMS is executing! Alter to suit.

Can I close SSMS but leave a stored procedure running?

Is it possible to shut down ms sql server management studio while a stored procedure is running, without stopping the stored procedure?
If you mean an SP you are running within SSMS then no. Obviously closing your own SSMS won't affect SP's that are running from other users on the server.
You really can't, however you can create a SQL Agent job which will execute the stored proc do you need a result set returned to you or are you updating data?
If its an update I think you're fine just running it from the agent, if not, your next simplest way to return a long running stored proc's result set would to be create an SSIS package which outputs that result set to a csv, excel doc what ever is appropriate. This package can then also be executed by the SQL Agent.
Yes you can, but you will not be able to see the result of the SP if something is returned. Once the execution is given to server the server will execute the SP not the SSMS.

Easy Install Update stored procedure in sql server express 2005

We have a stored procedure that needs to be updated at a customer site. Basically we will be emailing the changed sp to customer. What is the easiest way a non tech user can install update this sp in sql server express 2005 ? I would ideally like to create some bat file or exe that the user can just double click and the sp gets installed. I know c#, t-sql and basic dos script commands. OS is win xp. Please do not propose any fancy solutions using powershell since that may not be installed on customer machine. Note this is sql EXPRESS 2005. The sp itself is like any standard sp that has below structure. Worst case I plan to create a word doc with step by step screenshots on how to open sql management studio , open file, execute....
use [dbname]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_name]')
AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_name]
AS blah blah
thank you
If the user has SQL Server Management Studio, then they also have SQLCMD which is a command line utility for connecting to the database. You could put your sql script in to a .sql file and then create a .bat file that calls SQLCMD with the appropriate command line switches.
You should be able to build a (C#) application with a new SqlCommand {type = text}, containing your update sp, and as long as the connection string info is correct, you should be able to build it, send it to them to run the exe, and then done.
The only difficulty I see is making sure the connection strings are right if you don't already know their environment and aren't working with a technical user.
Good luck!

How do I add breakpoints to a stored SQL Procedure for debugging?

I am trying to debug a SQL procedure, and am using the following query to do so.
USE [database]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[Populate]
#ssId = 201
SELECT 'Return Value' = #return_value
GO
The problem is, the procedure I am testing, (Populate) doesn't seem to stop at any of the breakpoints I assign to it. Is there a different way to add breakpoints such that I can follow the debugger? I am getting the following error: "Invalid line number specified with SQL breakpoint".
Actually, if you have SQL Server 2008, you can once again debug in Management Studio.
Each of these articles will take you through it step by step, with screenshots. It could hardly be easier.
SQL Server 2008: The Debugger is back
T-SQL Debugger is back in SQL Server 2008 ... debugging now made easy
New Features in SQL Server 2008 Management Studio
Follow these tutorials:
Debugging Stored Procedures in SQL Server 2005
Debugging SQL Server CLR functions, triggers and stored procedures
1 Make a connection to a database.
2 Right-click the stored procedure, user-defined function, or trigger you want to work with and choose Open from the shortcut menu.
To insert a break point
1.Right-click the line of SQL syntax in the editor window where you want to insert a breakpoints.
2.Point to Break point and then click Insert Break point.