Add T-SQL in maintenance plan in SQL Server - sql

How can I set a T-SQL statement as a part of a maintenance plan in SQL Server?
Since I can see options of backup etc but not the possibility of adding T-SQL

I am not sure why you are looking to set up a maintenance plan.But, the alternate approach would be to set up a SQL server agent job to execute your T-SQL statements (which can be put together as procedures) and schedule it accordingly.
At the same time, you can execute SQL jobs through maintenance plans as well. This page will also help you : https://learn.microsoft.com/en-us/sql/relational-databases/maintenance-plans/use-the-maintenance-plan-wizard?view=sql-server-2017

First I connected to my SQL server using SQL Server Management Studio.
I went to the node Management, right-clicked the subnode Maintenance Plans and created a new maintenance plan called Test. My maintenance plan automatically got a subplan called Subplan_1. I just kept it and saved the maintenance plan.
Next, I went to the node SQL Server Agent, opened the subnode Jobs and double-clicked node Test.Subplan_1. It had a job step called Subplan_1. Double-clicking that job step opened the job step's properties. There I could choose the type Transact-SQL script (T-SQL) and enter my SQL code.
I did not encounter any problems. I used SQL Server 2017, but I am pretty sure it works about the same way in earlier versions of SQL Server...
Edit:
Like sabhari karthik commented and answered, it is very well possible to just create a new job with SQL Server Agent and schedule that job. So perhaps you do not need a maintenance plan at all. But if you do use maintenance plans (or are required to use and/or edit existing maintenance plans), it might be just the case that a maintenance plan's subplan automatically gets a related SQL Server Agent job. But I am not sure. I have never configured and used any maintenance plans before. I'm just a software developer, not a DBA.
Edit 2:
I see in the Maintenance Plan Wizard that there is an option to execute a SQL Server Agent Job as a maintenance task as well. But it seems you need to create that SQL Server Agent Job first.

Related

Schedule a job to run only if another job is not currently running in Microsoft SQL Server Management Studio

Currently have 2 jobs that can't run in parallel. Is there a way I can defer the execution based on the status? So MAG_LOGICAR_D3_H should not run if MAG_LOGICAR_D3_M is currently running and vice versa using Microsoft SQL Server Management Studio ?
On way to accomplish this is using a maintenance plan.
Add two: Execute SQL Server Agent Job Task.
Configure the jobs.
Link both jobs.
Force the execution of second job after Success or Completion of the first one.

View SQL Blocks/Locks?

I was wondering if there is there a tool or a way to view which statements cause a block/lock and which objects would be affected? I know about sp_who and sp_who2, but those only work while the system is running.
For example, I know running this:
UPDATE myTable SET col1 = 'something'
Will put a lock on "myTable", but there are more complex scenarios (like nested procs and triggers) that would be more difficult to identify.
I was hoping for tool like the "Actual Execution Plan" built-into SSMS, but other tools, queries will suffice
Thanks all
You can refer this blog and follow the different ways to identify the blocks:
sp_who2 System Stored Procedure
sys.dm_exec_requests DMV
Sys.dm_os_waiting_tasks
SQL Server Management Studio Activity Monitor
SQL Server Management Studio Reports
SQL Server Profiler
Also check: Different techniques to identify blocking in SQL Server
You can use SQL Activity Monitor (right click on the SQL instance in SSMS) to get a list the number of current locks etc and you can use the 'Task State' column of the 'Processes' panel in there to see currently running statements which will tell you if things are blocking.
These all focus on activities which are currently executing though, so I'm not sure this will be what you want.

Is there a way to track all the queries that has been executed by the server?

I want to be able to see all the queries that has been executed on the server last 2 days etc.
see the script, date of execution, sender etc.
is there any way?
I am using SQL X 2005.
I don't believe it's possible without SQL Server Profiler running.
Yes you can use SQL-Trace to log each command submitted to the server. It's the same mechanism used by the profiler, but you do not have to have the profiler or any other tool to use it.
There are two modes in which SQL Trace can run - in-memory buffer and disk file. The former is only used by profiler, is not documented and should not be used. Use the disk-file mode. The file can later on be opened on the same or different machine and even loaded into a table for analysis.
To learn more go to this page: http://msdn.microsoft.com/en-us/library/ms191511.aspx and search for section titled "To perform monitoring tasks with SQL Trace by using Transact-SQL stored procedures"
Here is a free, open-source Profiler tool that might help.
Profiler for Microsoft SQL Server 2005/2008 Express Edition

Is it possible to automate MS SQL Server (2005) queries via SQL Server Management Studio?

I have two queries saved on my SQL Server 2005 and I would like to have them running at certain intervals, e.g. every 24 hours. Is there a way to automate these queries and schedule them via SQL Server Management Studio?
You need to use the SQL Server Agent, installed as part of SQL Server. This is the background service that's responsible for running scheduled maintenance and backup tasks.
Expand the SQL Server Agent node in SQL Server Management Studio, you should see a tree node called "Jobs"
Right-clicking this will give you the option to add a new job. Jobs consist of a series of steps to be executed in order, and when you add a new step to your job, you can choose various types of step, including "Transact-SQL Script"
Create a new job, add a single T-SQL step, put the queries that you want to run into the step properties, and then use the "Schedule" option in the Job properties to create a recurring schedule that'll run the job every 24 hours (or whenever).
You can use the SQL Server Agent, which will allow the server to run the script/stored procedure.
You can also use the Windows Task Scheduler, either on the server or on any other server or workstation to schedule isqlw/sqlcmd to execute your script/stored procedure.
Create a job with a step in which you execute your queries; the job can be scheduled at your needs.
At a previous employer the operations department had a task sheduling application. They prefered to use the command line tools that come with sql server to execute jobs (stored procedures) on a scheduled basis. This way the "task scheduling application" could recieve an exit status (pass/fail, ok/error) and hold up dependent jobs.
I'm sorry I don't remember the name of the command line tool, but it was part of the sql server distro. I also do not remember the name of the task scheduling application. It was not the windows task scheduler. It was something enterprise level used to manage the nightly cycle.
Not sure of the scale of your project, but this is another way to go.
SKapsal's comment on a command line tool for executing SQL commands is a reference to osql (for SQL2000) or sqlcmd (for SQL2005+). You could save your script a file and run it from that command line using Windows Task Scheduler or something similar.
SQL Agent is still the preferred solution, however, as it provides GUI controls for job creation, scheduling, logging and viewing job execution history/results.
how to schedule a job for sql query to run daily?
This is similar question with helpful answer.
Covering simple step by step manual.

In MS SQL Server 2005, is there a way to export, the complete maintenance plan of a database as a SQL Script?

Currently, if I want to output a SQL script for a table in my database, in Management Studio, I can right click and output a create script.
Is there an equivalent to output an SQL script for a database's maintenance plan?#
Edit
The company I work for has 4 servers, 3 servers and no sign of integration, each one running about 500,000 transactions a day. The original maintenance plans were undocumented and trying to create a default template maintenance plan.
You can't export them as scripts, but if your intention is to migrate them between server instances then you can import and export them as follows:
Connect to Integration Services and expand Stored Packages>MSDB>Maintenance Plans. You can then right click on the plan and select import or export
I don't think you can do that with Maintenance Plans, because those are DTS packages, well now they are called SSIS (SQL Server Integration Services).
There was a stored procedure from which you could add maintenance plans, but I think that it may be deprecated. (sp_add_maintenance_plan). I don't have a SQL 2005 to test here.
The question is, why would you want to export the complete mp? :) If it's for importing in other server, then a ssis package could be useful. I suggest you take a look in that direction, because those you can export/import among servers.