--this query finds the current ( as of the date the query is run) age of the person.
declare @dob datetime
set @dob = '07/12/1966'
select CASE
WHEN dateadd(year, datediff (year, @dob, GETDATE()),@dob) > GETDATE()
THEN datediff (year, @dob, GETDATE()) - 1
ELSE datediff (year, @dob, GETDATE())
END as Age
--select dateadd(year, datediff (year, @dob, GETDATE()),@dob)
--select datediff (year, @dob, GETDATE())
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, July 18, 2016
Thursday, July 14, 2016
db_executor role for user permission to execute all procedures
SQL Server has several fixed database roles such as db_datareader and db_datawriter, which grants the user read and write access respectively to all the tables in the database. Curiously there is no role to grant a user permission to execute stored procedures, but fortunately this is easily resolved by creating a new role.
The following SQL creates the new role in a database, and then grants it execute rights :
The following SQL creates the new role in a database, and then grants it execute rights :
-- Create a db_executor role
CREATE ROLE db_executor
-- Grant execute rights to the new role
GRANT EXECUTE TO db_executor
CREATE ROLE db_executor
-- Grant execute rights to the new role
GRANT EXECUTE TO db_executor
--then add a member to this role
Labels:
database role,
db_excutor,
permissions,
stored procedures
Monday, June 27, 2016
To check the last successful run date and time of the sql job
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
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
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
Subscribe to:
Posts (Atom)