which is the correct way to delete multiple jobs in sql server - jobs

Im using SQL Server 2012 and I have a case where I have to delete sql jobs. I found two approaches which are as follows:
Approach 1
DECLARE #jobId binary(16)
WHILE (1=1)
BEGIN
SET #jobId = NULL
SELECT TOP 1 #jobId = job_id FROM msdb.dbo.sysjobs WHERE (name like N'Abc%')
IF ##ROWCOUNT = 0
BREAK
IF (#jobId IS NOT NULL)
BEGIN
EXEC msdb.dbo.sp_delete_job #jobId
END
END
Approach 2
DECLARE #listStr VARCHAR(MAX)=null
SELECT #listStr = COALESCE(#listStr+'exec msdb.dbo.sp_delete_job ' ,'') + '''' + convert(varchar(max),job_id) + '''; '
FROM msdb.dbo.sysjobs WHERE (name like N'$(TestPublisherServer)-$(TestPublisherDB)%')
IF #listStr is not null
BEGIN
PRINT 'exec msdb.dbo.sp_delete_job ' + #listStr
EXEC ('exec msdb.dbo.sp_delete_job ' + #listStr)
END
Both the approaches will delete the jobs, but I want to know which is the best way or you can suggest me more efficient or correct ways to delete the jobs.
And one more question is, do we have to stop/disable the job before deleting it.
TIA
Harsha

I would use approach 1 because it is just simple and effective. There is no need to disable the job before. If you delete a job while it is running, the job will terminate with an error, so check if thats requirement for you.
Way to handle this problem(posted by OP):
IF exists(
select 1 from msdb.dbo.sysjobactivity activity
where activity.run_Requested_date is not null
and activity.stop_execution_date is null
and activity.job_id = #jobId)
exec msdb.dbo.sp_stop_job #job_id = #jobId
I'm not sure if you are worried about the schedule to stay. Depending on how #delete_unused_schedule is set, sp_delete_job will delete it by default.
See the stored procedure comment
#delete_unused_schedule BIT = 1 -- For backward compatibility schedules are deleted by default if they are not
-- being used by another job. With the introduction of reusable schedules in V9
-- callers should set this to 0 so the schedule will be preserved for reuse.
So this option will remain you flexible, but i think you will have no need for the schedule because you probably want to clean up your system.

Related

ALLUSERSPROFILE OF the HOST SQL Server?

I need to write a SQL Server 2008R2 compatible script to create a share. The script will be executed from inside VB6 code but I am pretty sure that's a moot point here.
The following is PSEUDOCODE at the end
CREATE PROCEDURE [dbo].[create_Server_share]
#TheShare VARCHAR(50),
#TheDIR VARCHAR(250) = NULL
AS
BEGIN
IF (#TheDIR IS NULL) -- ALLUSERSPROFILE usually C:\Programdata
SET #TheDIR = ENVREFERENCE('%ALLUSERSPROFILE%')+ '\XYZ'
....
I already see that ENVREFERENCE is NOT available in SQL Server 2008 R2 (which is the oldest version I have to accomodate for our clients)
But I am not married to using ENVREFERENCE either - I just want the HOST MACHINE to give me its environment return for ALLUSERSPROFILE (obviously I should not grab this value from the executing code in the application because I will be getting the CLIENT's value instead of the desired HOST server's value; hence my desire to execute it from the T-SQL script)
So do any SQL guru's have some insight into this?
Thanks in advance.
Harry
Can't say this is completely bulletproof, but I past the first few dozen tests.
Thanks to Jeroen Mostert I realized I had my access to %ALLUSERSPROFILES% already on the Host server. the script then became something I could do...
-- create_Server_share.sql written by Harry Abramowski on 6/26/2018
-- we ARE NOT doing these steps in a command line prompt nor in VB6
-- because this share has to be made **ON** THE SERVER!
-- stored procs are a bitch!
go
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF EXISTS(SELECT 1
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_NAME = 'create_Server_share'
AND SPECIFIC_SCHEMA = 'dbo')
BEGIN
DROP PROCEDURE create_Server_share
END
go
CREATE PROCEDURE [dbo].[create_Server_share]
#TheShare varchar(50),
#TheDrive char=null,
#TheDIR varchar(250)=null
AS
BEGIN
if (#TheDIR is null)
set #TheDIR = '%ALLUSERSPROFILE%\XYZ'
if (#TheDrive is null)
set #TheDrive = 'X'
DECLARE #answer as varchar(MAX)
declare #myString as varchar(1000)
DECLARE #i INT
-- JUST in case its not already set up, let's enable use of the reconfig in SQL
EXEC sp_configure 'show advanced options', 1; --might not be needed
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell',1; -- wont hurt to assume it IS needed
RECONFIGURE;
-- net share XYZShare=C:\Programdata\XYZ /grant:everyone,FULL
a_redo:
set #myString = ('net share ' + #TheShare +'=' + #TheDIR + ' /grant:everyone,FULL')
CREATE TABLE #xyzout ([outputtext] varchar(MAX))
Insert into #xyzout (outputtext) EXECUTE xp_cmdshell #myString
-- what about The system cannot find the file specified.???
if exists(select #xyzout.outputtext from #xyzout where #xyzout.outputtext = 'The system cannot find the file specified.')
begin
set #myString = ('mkdir ' + #TheDIR)
EXECUTE xp_cmdshell #mystring
print ('The directory ' + #TheDIR + ' was created')
drop table #xyzout
goto a_redo -- yeah I know!
end
-- was there an error - was it just an "already exists" message? let's see
set #answer = (select top 1 outputtext from #xyzout)
print #answer
-- now update systemProps table so the client machines know there's a share and what drive they should map it to
if charindex('system error',lower(#answer))= 0
IF NOT EXISTS (SELECT a.* FROM syscolumns a, sysobjects b
WHERE a.name = 'XYZShare' AND
a.id = b.id AND
b.name = 'systemProps')
ALTER TABLE system ADD XYZShare NVARCHAR(1000) NULL ;
if charindex('system error',lower(#answer))= 0
begin
update systemProps set XYZShare = (#TheDrive + '=\\' +
CAST(serverproperty('MachineName') as varchar) + '\' + #TheShare );
select systemProps.XYZShare from systemProps;
return 0;
end
else
begin
select * from #xyzout where not(outputtext is null)
return 1;
end
EXEC sp_configure 'xp_cmdshell',0; --let's leave that off?
RECONFIGURE;
DROP TABLE #xyzout
---- if you need to delete the SHARE ITSELF you COULD use this: EXEC XP_CMDSHELL 'net share Xshared /delete'
--HOWEVER you can easily do either from the windows explorer in NETWORK view or My Computer view
END
GRANT EXECUTE ON dbo.create_Server_share TO PUBLIC
GO
Hope this is useful to someone. You guys always come through for me!

Executing SQL query on multiple databases

I know my post has a very similar title to other ones in this forum, but I really couldn't find the answer I need.
Here is my problem, I have a SQL Server running on my Windows Server. Inside my SQL Server, I have around 30 databases. All of them have the same tables, and the same stored procedures.
Now, here is the problem, I have this huge script that I need to run in all of these databases. I wish I could do it just once against all my databases.
I tried a couple things like go to "view" >> registered servers >> local server groups >> new server registration. But this solution is for many servers, not many databases.
I know I could do it by typing the database name, but the query is really huge, so it would take too long to run in all databases.
Does anybody have any idea if that is possible?
You can use WHILE loop over all database names and inside loop execute query with EXECUTE. I think that statement SET #dbname = ... could be better, but this works too.
DECLARE #rn INT = 1, #dbname varchar(MAX) = '';
WHILE #dbname IS NOT NULL
BEGIN
SET #dbname = (SELECT name FROM (SELECT name, ROW_NUMBER() OVER (ORDER BY name) rn
FROM sys.databases WHERE name NOT IN('master','tempdb')) t WHERE rn = #rn);
IF #dbname <> '' AND #dbname IS NOT NULL
EXECUTE ('use '+QUOTENAME(#dbname)+';
/* Your script code here */
UPDATE some_table SET ... ;
');
SET #rn = #rn + 1;
END;
Consider running the script in SQLCMD Mode from SSMS (Query--SQLCMD Mode). This way, you can save the script to a file and run it in the context of each of the desired databases easily:
USE DB1;
:r C:\SqlScript\YourLargeScript.sql
GO
USE DB2;
:r C:\SqlScript\YourLargeScript.sql
GO
USE DB3;
:r C:\SqlScript\YourLargeScript.sql
GO
This technique can also be used to run the script against databases on other servers with the addition of a :CONNECT command. The connection reverts back to initial server/database after execution of the entire script:
:CONNECT SomeServer
USE DB4;
:r C:\SqlScript\YourLargeScript.sql
GO
:CONNECT SomeOtherServer
USE DB5;
:r C:\SqlScript\YourLargeScript.sql
GO
Important gotcha: Note GO batch separators are needed for :CONNECT to work as expected. I recommend including GO in the the invoking script like the above example but GO as the last line in the :r script file will also provide the desired results. Without GO in this example (or at the end of the script file), the script would run twice on SomeServer and not run against SomeOtherServer at all.
ApexSQL Propagate is the tool which can help in this situation. It is used for executing single or multiple scripts on multiple databases, even multiple servers. What you should do is simply select that script, then select all databases against which you want to execute that script:
When you load scripts and databases you should just click the “Execute” button and wait for the results:
You can write script like this
DECLARE CURSOR_ALLDB_NAMES CURSOR FOR
SELECT name
FROM Sys.Databases
WHERE name NOT IN('master', 'tempdb')
OPEN CURSOR_ALLDB_NAMES
FETCH CURSOR_ALLDB_NAMES INTO #DB_NAME
WHILE ##Fetch_Status = 0
BEGIN
EXEC('UPDATE '+ #DB_NAME + '..SameTableNameAllDb SET Status=1')
FETCH CURSOR_ALLDB_NAMESINTO INTO #DB_NAME
END
CLOSE CURSOR_ALLDB_NAMES
this is the normal way of doing this :
suppose you want to do a select on database DBOther than it would be :
select * from DBOther..TableName
Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name
select * from DBOther.dbo.ViewName
If any of the databases is on another server on another machine, than make sure the Database is in the Linked Server.
Then you can access the table or view on that database via:
SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]
Here is another way that does not requires typing the database name :
use DB1
go
select * from table1
go
use DB2
go
select * from table1
go
Note that this will only work if the tables and fields are exact the same on each database
You can use the following script to run the same script on a set of databases. Just change the filter in the insert line.
declare #dbs table (
dbName varchar(100),
done bit default 0
)
insert #dbs select [name], 0 FROM master.dbo.sysdatabases WHERE [Name] like 'targets_%'
while (exists(select 1 from #dbs where done = 0))
begin
declare #db varchar(100);
select top 1 #db = dbName from #dbs where done = 0;
exec ('
use [' + #db + '];
update table1 set
col1 = '''',
col2 = 1
where id = ''45b6facb-510d-422f-a48c-687449f08821''
');
print #db + ' updated!';
update #dbs set done = 1 where dbName = #db;
end
If your SQL Server version does not support table variables, just use Temp Tables but don`t forget to drop them at the end of the script.
Depending on the requirement, you can do this:
declare #dbName nvarchar(100)
declare #script nvarchar(max)
declare #dbIndex bigint = 0
declare #dbCount bigint = (
select count(*) from
sys.databases
)
declare crs_databases cursor for
(
select
[name]
from
sys.databases
)
open crs_databases
fetch next from crs_databases into #dbName
while ##FETCH_STATUS = 0
begin
set #dbIndex = #dbIndex+1
set #script = concat(#script,
' select Id from ['+#dbName+']..YourTableName ',
case
when #dbIndex = #dbCount then ''
else 'union'
end)
fetch next from crs_databases into #dbName
end
select #script
close crs_databases
deallocate crs_databases
Please note that the double dotted notation assumes that the schema is dbo. Otherwise, you need to explicitly write down the schema.
select Id from ['+#dbName+'].schema.YourTableName
When you need to execute stored procedures on each server, the #script variable will have another content.

iterative executing stored procedure with a set based approach

I have an issue where I am trying to replace the following code with a different solution. Currently I am using a cursor but it is running to slowly. I am under the understanding that iterative solutions can only be completed with cursors or while loops but I am trying to find a set based approach and running out of ideas. I was hoping that I could find some inspiration here. Thanks all.
--used to find a unique list of Some_ID
#Id1, #Id2, #Id3
DECLARE SomeCursor CURSOR FOR
SELECT SOME_ID FROM SomeTable
WHERE ID1=#Id1 AND ID2=#Id2 and ID3=#Id3
OPEN SomeCursor
FETCH NEXT FROM SomeCursor INTO #SomeID
WHILE ##Fetch_Status = 0
BEGIN
Print #SomeID
--simply populates a single table with values pulled from
--other tables in the database based on the give parameters.
EXEC SP_PART1 #SomeID, #parameters...
print 'part 2 starting'
EXEC SP_PART2 #SomeID, #parameters...
FETCH NEXT FROM SomeCursor INTO #SomeID
print getdate()
END
CLOSE SomeCursor;
DEALLOCATE SomeCursor;
Your only option to make this set-based is to rewrite the sps to make them set-based (using table-valed parameters intead of individual ones) or to write set based code in this proc instead of re-using procs designed for single record use. This is a case where code re-use is usually not appropriate.
I'm not too sure what you want, but why not use your select statement to create your sql scripts and execute them all at once with something like this.
DECLARE #sql VARCHAR(MAX);
SELECT #sql = COALESCE(#sql,'') + 'EXEC SP_Part1 ' + SOME_ID + '; EXEC SP_Part2 ' + SomeID + '; GO '
FROM SomeTable
WHERE ID1=#Id1 AND ID2=#Id2 and ID3=#Id3
EXEC (#sql)

SQL Server 2008 r2: How to check all views for runtime errors?

I have a LOT of views in the database.
Each view ofc refers to one or more tables.
There was some work done with those tables (alter, delete columns) and now i need to check all views for any runtime errors.
I went straithforward: got list of all views, iterated over it and launch SELECT TOP 0 * FROM view_name dynamically so any errors should appear in the Messages pane.
This is my code
DECLARE #view_name_template varchar(max) = '%'
DECLARE #columnList varchar(75) = '*'
--------------------------
DECLARE #tmp_views AS TABLE (view_name varchar(max))
DECLARE #view_name varchar(max)
DECLARE #sqlCommand nvarchar(max)
DECLARE #num int = 1
DECLARE #total_count int
SET NOCOUNT ON
INSERT INTO #tmp_views
SELECT name FROM sys.views
WHERE name LIKE #view_name_template
SELECT #total_count = COUNT(*) FROM sys.views WHERE name LIKE #view_name_template
DECLARE db_cursor CURSOR FOR
SELECT view_name FROM #tmp_views ORDER BY LOWER(view_name)
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #view_name
WHILE ##FETCH_STATUS = 0
BEGIN
SET #sqlCommand = 'SELECT TOP 0 ' + #columnList + ' FROM ' + #view_name
PRINT CAST(#num as varchar(31)) + '/' + CAST(#total_count as varchar(31)) + ' ' + #sqlCommand
EXECUTE sp_executesql #sqlCommand
FETCH NEXT FROM db_cursor INTO #view_name
SET #num = #num + 1
END
CLOSE db_cursor
DEALLOCATE db_cursor
It works fine except it completely freezes on some views (select from those views in other window works extremely fast and fine). I think it is server a memory overflow issue or something similar.
Tell me please: what is the lightweighiest way to check view has errors or not? Maybe SQL Server has a special function or stored procedure?
The code is not "hanging". It is waiting for the view to run, despite the top 0.
SQL Server offers several ways of testing queries. In addition to the top 0, you also have:
`set parseonly1
set noexec on
And then the more recent sp_describe_first_result_set.
Each of these do different things. parseonly checks for syntax errors but doesn't look at table layouts. I believe noexec completely compiles the query, creating the execution plan. top 0 will compile the query and also run it.
In some cases, the optimizer may not recognize that a query that returns no rows might need to do no work. For instance, there might be subqueries that are run despite the top 0, and this is causing the delay.
Two approaches. The first is to use noexec on (documented here). The second, if feasible, would be to create another database with the same structure and no data. You can then test the queries on that database.

Force a sync of all SQL Server replication subscribers from a publisher

I have a SQL Server, which has merge replication configured to around 100 subscriber databases.
I would like to be able to force an ad hoc synchronisation to all the subscribers from the publisher using TSQL.
I have found some code on TechNet which calls out to an executable, but I was hoping to avoid that for security reasons etc.
Any way to do it without?
REM -- Declare the variables.
SET Publisher=%instancename%
SET Subscriber=%instancename%
SET PublicationDB=AdventureWorks2008R2
SET SubscriptionDB=AdventureWorks2008R2Replica
SET Publication=AdvWorksProductsTran
REM -- Start the Distribution Agent with four subscription streams.
REM -- The following command must be supplied without line breaks.
"C:\Program Files\Microsoft SQL Server\100\COM\DISTRIB.EXE" -Subscriber %Subscriber%
-SubscriberDB %SubscriptionDB% -SubscriberSecurityMode 1 -Publication %Publication%
-Publisher %Publisher% -PublisherDB %PublicationDB% -Distributor %Publisher%
-DistributorSecurityMode 1 -Continuous -SubscriptionType 0 -SubscriptionStreams 4
From here:
http://technet.microsoft.com/en-us/library/ms147377(v=sql.105).aspx
Found out the answer using EXECUTE msdb.dbo.sp_start_job ....
See below for a cursor that runs through all databases in a supplied list.
DECLARE #DatabaseName varchar(50)
DECLARE #Job_Name NVARCHAR(100)
DECLARE #SQL NVARCHAR(max)
DECLARE dc CURSOR FAST_FORWARD READ_ONLY FOR
SELECT UniqueName
FROM mylistofdatabases
OPEN dc
FETCH NEXT FROM dc INTO #DatabaseName
WHILE ##FETCH_STATUS = 0
BEGIN
--Use MSDB systables to find the new SQL Agent Job ID.
SELECT TOP 1 #Job_Name =J.name
FROM msdb.dbo.sysjobs AS J
INNER JOIN msdb.dbo.sysjobsteps AS S
ON S.job_id = J.job_id
WHERE S.command LIKE '%' + #DatabaseName + '%'
AND J.name LIKE '%mainjob%'
ORDER BY J.date_created DESC
--Kick off the job
SET #SQL = 'EXECUTE msdb.dbo.sp_start_job N''' + CAST(#Job_Name AS VARCHAR(100)) + ''''
EXEC(#SQL)
FETCH NEXT FROM dc INTO #DatabaseName
END
CLOSE dc
DEALLOCATE dc