Pages

Showing posts with label Sql Server. Show all posts
Showing posts with label Sql Server. Show all posts

Monday, June 8, 2015

SQL Agent Job and Job Step History in SQL Server


To generate a comprehensive and descriptive set from the job history the following script can be run in SSMS/QA. It uses the system database msdb and some of the system tables related to jobs and job history.

USE msdb
Go
SELECT j.name JobName,h.step_name StepName, h.step_id,
CONVERT(CHAR(10), CAST(STR(h.run_date,8, 0) AS dateTIME), 111) RunDate,
STUFF(STUFF(RIGHT('000000' + CAST ( h.run_time AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') RunTime,
h.run_duration StepDuration,
case h.run_status when 0 then 'failed'
when 1 then 'Succeded'
when 2 then 'Retry'
when 3 then 'Cancelled'
when 4 then 'In Progress'
end as ExecutionStatus,
h.message MessageGenerated
FROM sysjobhistory h inner join sysjobs j
ON j.job_id = h.job_id
Where j.name = 'XXXXXXXXXXX'
ORDER BY h.run_date, h.run_time
Desc
GO

Column Description
[JobName] Name of job as specified
[StepName] Name of step as specified
[RunDate] Date when job run
[RunTime] Time when job run
[StepDuration] Duration in seconds that a step took to complete
[ExecutionStatus] Execution status of step
[MessageGenerated] Message generated at end of step

Sunday, February 5, 2012

How to add Users for databases on Sql Server 2008

How to add Users for databases on Sql Server 2008

Following are the steps for adding users on Sql server.

Step:1
Got to Security-> Logins on server.

Step: 2
Right click on Logins. Add Login name and select SQL Server authentication. Set password and Confirm password.
Unchecked Enforce password expiration option.

Step: 3
Go to Server Roles and select public option

Step: 4
Go to User Mapping and select Db_Owner, Public  Option and mapped it with database from databases.

After following the above steps you will find recently added user will be automatically added on the database.

Thursday, August 4, 2011

How to remove special characters from a string in MS SQL Server (T-SQL)

-- Removes special characters from a string value.
-- All characters except 0-9, a-z and A-Z are removed and
-- the remaining characters are returned.
-- Author: Christian d'Heureuse, www.source-code.biz

create function dbo.RemoveSpecialChars (@s varchar(256))
returns varchar(256) with schemabinding
    begin 
     if @s is null 
        return null
     declare @s2 varchar(256)
     set @s2 = ''
     declare @l int
     set @l = len(@s)
     declare @p int 
     set @p = 1
     while @p <= @l
         begin
           declare @c int
           set @c = ascii(substring(@s, @p, 1))  
           if @c between 48 and 57 or
              @c between 65 and 90 or
              @c between 97 and 122    
           set @s2 = @s2 + char(@c)
           set @p = @p + 1
     end
     if len(@s2) = 0
     return null
return @s2
end

Example of how to use the function:
select dbo.RemoveSpecialChars('abc-123+ABC')
Result:
abc123ABC