Declare @jobId as uniqueidentifier
select
j.job_id,j.Name as "Job Name", j.description as "Job Description", CONVERT(DATETIME, RTRIM(h.run_date))
+ ((h.run_time / 10000 * 3600)
+ ((h.run_time % 10000) / 100 * 60)
+ (h.run_time % 10000) % 100) / (86399.9964) AS run_datetime,
case h.run_status
when 0 then 'Failed'
when 1 then 'Successful'
when 3 then 'Cancelled'
when 4 then 'In Progress'
end as JobStatus
from sysJobHistory h, sysJobs j
where j.job_id = h.job_id and h.run_date =
(select max(hi.run_date) from sysJobHistory hi where h.job_id = hi.job_id ) and h.run_status= 1
and cast(cast(h.run_date as varchar(8)) as date) = cast('06/27/2016' as date)
order by 2
This blog is created to add all the information and experiences that I have learnt while working on MS sql server as a DBA. The blog is also updated frequently with the real world problem that I encounter hands on at work and the resolutions to resolve them. I hope this blog will be of some use to you and you will revisit. Thank you for stopping by and you are welcome to leave comments.
Monday, June 27, 2016
Friday, June 24, 2016
Move a user databases
To move a data or log file as part of a planned relocation, follow these steps:
- Run the following statement.
ALTER DATABASE database_name SET OFFLINE;
- Move the file or files to the new location.
- For each file moved, run the following statement.
USE master GO ALTER DATABASE AdventureWorks2012 MODIFY FILE ( NAME = AdventureWorks2012_Data, FILENAME = 'C:\Disk2\AdventureWorks2012_Data.mdf'); -- New file path USE master GO ALTER DATABASE AdventureWorks2012 MODIFY FILE ( NAME = AdventureWorks2012_Log, FILENAME = 'C:\Disk2\AdventureWorks2012_log.ldf'); -- New file path
- Run the following statement.
ALTER DATABASE database_name SET ONLINE;
- Verify the file change by running the following query.
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID(N'<database_name>');
Thursday, June 23, 2016
Start the SQL Server instance in single user mode.
Click on SQL Configuration Manager
Click on SQL Services
Click on desired SQL Server instance and right click go to properties. On the Advance table enter param ‘-m;‘ before existing params in Startup Parameters box. ( make sure that the semicolon exists after the m.
-m;dC:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\master.mdf
Once the desired work is done, make sure to remove the newly added parameter.
Friday, June 17, 2016
SQL Agent Job: Update the notification operator
I found many SQL Agent job were enabled but no any notifications setup in case of failure.
I ran the following
use msdb
GO
select * from dbo.sysjobs
where notify_level_email = 0
and enabled = 1
--- gives the list of jobs enabled but no notification set up.
SELECT [ID], [Name], [Enabled]
FROM MSDB.dbo.sysoperators
WHERE [Enabled] = 1
ORDER BY [Name];
Go
-- Gives the list of operators. Pick the Id of the one you want to notify to use in the below script
UPDATE S
SET S.[notify_level_email] = 2,
S.[notify_email_operator_id] = 2 ---my operator
FROM MSDB.dbo.sysjobs S
WHERE S.[Notify_Level_Email] = 0
AND S.[Enabled] = 1;
GO
I ran the following
use msdb
GO
select * from dbo.sysjobs
where notify_level_email = 0
and enabled = 1
--- gives the list of jobs enabled but no notification set up.
SELECT [ID], [Name], [Enabled]
FROM MSDB.dbo.sysoperators
WHERE [Enabled] = 1
ORDER BY [Name];
Go
-- Gives the list of operators. Pick the Id of the one you want to notify to use in the below script
UPDATE S
SET S.[notify_level_email] = 2,
S.[notify_email_operator_id] = 2 ---my operator
FROM MSDB.dbo.sysjobs S
WHERE S.[Notify_Level_Email] = 0
AND S.[Enabled] = 1;
GO
Friday, May 20, 2016
DMV:Indexes
To check of any primary key is a NOT a clustered index
select OBJECT_SCHEMA_NAME(so.[object_id]) as schemaname ,
so.name as TableName ,
si.name as IndexName,
si.type as IndexType,
si.is_primary_key
from sys.indexes si
inner join sys.tables so on
si.[object_id] = so.[object_id]
where si.type in (0,2)
and si.is_primary_key = 1
order by so.name
select OBJECT_SCHEMA_NAME(so.[object_id]) as schemaname ,
so.name as TableName ,
si.name as IndexName,
si.type as IndexType,
si.is_primary_key
from sys.indexes si
inner join sys.tables so on
si.[object_id] = so.[object_id]
where si.type in (0,2)
and si.is_primary_key = 1
order by so.name
Tuesday, April 26, 2016
CDC error: 'The specified '@server' is invalid (valid values are returned by sp_helpserver)
I encountered the following error when I was enabling the CDC for the db1.dbo.pat_table
Could not update the metadata that indicates table [dbo].[pat_Table] is enabled for Change Data Capture. The failure occurred when executing the command '[sys].[sp_cdc_add_job] @job_type = N'capture''. The error returned was 22836: 'Could not update the metadata for database db1 to indicate that a Change Data Capture job has been added. The failure occurred when executing the command 'sp_add_jobstep_internal'. The error returned was 14234: 'The specified '@server' is invalid (valid values are returned by sp_helpserver).'. Use the action and error to determine the cause of the failure and resubmit the request.'. Use the action and error to determine the cause of the failure and resubmit the request.
After researching around I understood that the above error is caused due to mismatch in value between SERVERPROPERTY(‘ServerName’)) and master.dbo.sysservers
I solved the issue as follows -
select @@SERVERNAME --oldname
select SERVERPROPERTY('ServerName') --defaultname
--1 drop the old servername
exec sp_dropserver 'oldservername'
--2 add new servername
exec sp_addserver 'newservername', 'local'
--3 see changes
exec sp_helpserver
Could not update the metadata that indicates table [dbo].[pat_Table] is enabled for Change Data Capture. The failure occurred when executing the command '[sys].[sp_cdc_add_job] @job_type = N'capture''. The error returned was 22836: 'Could not update the metadata for database db1 to indicate that a Change Data Capture job has been added. The failure occurred when executing the command 'sp_add_jobstep_internal'. The error returned was 14234: 'The specified '@server' is invalid (valid values are returned by sp_helpserver).'. Use the action and error to determine the cause of the failure and resubmit the request.'. Use the action and error to determine the cause of the failure and resubmit the request.
After researching around I understood that the above error is caused due to mismatch in value between SERVERPROPERTY(‘ServerName’)) and master.dbo.sysservers
I solved the issue as follows -
select @@SERVERNAME --oldname
select SERVERPROPERTY('ServerName') --defaultname
--1 drop the old servername
exec sp_dropserver 'oldservername'
--2 add new servername
exec sp_addserver 'newservername', 'local'
--3 see changes
exec sp_helpserver
Subscribe to:
Posts (Atom)