Wednesday 20 June 2012

To read sql error log through T-SQL command


system extended stored procedure xp_readerrrorlog use to read errorlog
  • Parameter 1 Type :- INT
    Name :- Error Log File
    Default Value  :- 0
    Description  :- 0 for current, 1 for Archive #1, 2 for Archive #2, and so on
  • Parameter 2 Type :- INT
    Name :- Log File Type
    Default Value :- 1
    Description :- 1 for error log and 2 for SQL Server Agent log
  • Parameter 3 Type :- Varchar (255)
    Name :- Search String 1
    Description :- String one wants to search in error log
  • Parameter 2 Type :- varchar (255)
    Name :- Search String 2
    Description :- Its second string which can be searched to refine the search result 
Following example show whole error log of sql server
exec xp_readerrorlog 0,1,null,null

Monday 18 June 2012

Function to replace all special characters

CREATE FUNCTION dbo.RemoveSpecialCharacter(@Str NVARCHAR(MAX)) RETURNS NVARCHAR(MAX)
AS
BEGIN
    SET @Str = Replace(@str, Char(13), ' ') --replace all enters with space
    SET @Str = Replace(@str, Char(9), ' ')--replace all tabs with space
    SET @Str = Replace(@str, Char(10), ' ')--replace all line feeds with space
    RETURN @Str
END

Wednesday 11 April 2012

Shrink database when log is full

DECLARE @DatabaseName VARCHAR(50);
SET @DatabaseName = 'DatabaseName'

SELECT name, recovery_model_desc, log_reuse_wait_desc
FROM sys.databases
WHERE name = @DatabaseName




USE DatabaseName;

GO

-- Truncate the log by changing the database recovery model to SIMPLE.

ALTER DATABASE DatabaseName

SET RECOVERY SIMPLE;

GO

-- Shrink the truncated log file to 1 MB.

DBCC SHRINKFILE (DatabaseName, 2);

GO

Monday 23 January 2012

Create User with "Create Procedure Permissions"

USE MASTER
CREATE LOGIN da WITH PASSWORD = '12345'

USE yourdatabasename
GO
CREATE USER da FOR LOGIN da

CREATE ROLE yourrolename

EXEC sp_addrolemember 'db_datareader',yourrolename
EXEC sp_addrolemember 'db_datawriter', yourrolename
EXEC sp_addrolemember @RoleName = 'yourrolename', @MemberName = 'da'

GRANT EXECUTE TO da
GRANT CREATE PROCEDURE TO da

GRANT ALTER ON SCHEMA::dbo TO yourrolename

Check the Isolation Level with DBCC useroptions

DBCC useroptions