Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Wednesday, December 25, 2013

The database could not be exclusively locked to perform the operation. (Microsoft SQL Server, Error: 5030)


First we will see how to set the database to single user mode,

ALTER DATABASE dbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE

Now we will try to rename the database

ALTER DATABASE dbName MODIFY NAME = dbNewName


Finally we will set the database to Multiuser mode


ALTER DATABASE dbNewName
SET MULTI_USER WITH ROLLBACK IMMEDIATE


source :
http://weblogs.asp.net/varadam/archive/2012/08/10/the-database-could-not-be-exclusively-locked-to-perform-the-operation-microsoft-sql-server-error-5030.aspx

Tuesday, January 10, 2012

Get days between 2 different times


/*get the all "dayeNumber" days between the begin and end date -- Sunday dayeNumber = 1 */


create FUNCTION [dbo].GetDays

( 

@dayNumber int ,@BeginDate datetime,@EndDate datetime

)
RETURNS @days TABLE(date datetime)

AS

Begin

declare
 @diff int

if @dayNumber > datepart(dw,getdate())

set @diff= @dayNumber- datepart(dw,getdate())

else

set
 @diff= @dayNumber- datepart(dw,getdate()) +7

set @BeginDate =(Select DATEADD(day,@diff,getdate()))

set @EndDate = (Select DateAdd( month,5,getdate()))

-- get days
WHILE @BeginDate <= @EndDate BEGIN

INSERT INTO @Days SELECT @BeginDate

SELECT @BeginDate = DATEADD(dd, 7, @BeginDate)

ENDreturn

end