Enabling broker after Restoring Sql Server DataBase - sql-server

I have DataBase with enabled Service Broker. Then I want to restore my database in program from backup of other database, but after restoring(I restore on existing database name), my method, whitch enables Service Broker, puts this error:
Msg 9772, Level 16, State 1, Line 1
The Service Broker in database "ServeDB2" cannot be enabled because there is already an enabled Service Broker with the same ID.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
This is my method:
public void TurnOnBroker()
{
if (!this.database.BrokerEnabled)
{
this.server.KillAllProcesses(this.database.Name);
this.database.BrokerEnabled = true;
this.database.Alter();
RefreshConnection();
}
}
What should i fix here?Any suggestions?

keep a note of these options
ALTER DATABASE mydb SET ENABLE_BROKER
ALTER DATABASE mydb SET DISABLE_BROKER
ALTER DATABASE mydb SET NEW_BROKER
if youre getting something like this is already an enabled Service Broker with the same ID, go for the NEW_BROKER

ALTER DATABASE [Database_name] SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
This will create the new service broker

Every database has a unique ID used by Service Broker. This ID must be unique across all databases in a Sql Server instance (well, it should be unique globally, but Sql Server doesn't have a way to enforce that). When you restore a database, you have an option of disabling Service Broker in the restored database, enabling it with the GUID of the backed up database (so that it can take over message processing from the backed up database) or assign it a new GUID. You're trying to do the second option while you still have the old database around and you run into GUID conflict.
See here for more info.

Run this query to find out which other databases are using the same service broker as the database you are using (e.g. for a database called DATABASE_NAME)...
SELECT name, is_broker_enabled, service_broker_guid FROM sys.databases
WHERE service_broker_guid IN (SELECT service_broker_guid FROM sys.databases where name = 'DATABASE_NAME');
... returns ...
name, is_broker_enabled, service_broker_guid
DATABASE_NAME_OTHER, 1, KBHDBVJH-SDVHIOHD-SODIVDIOH-UHDSV
DATABASE_NAME_ANOTHER, 0, KBHDBVJH-SDVHIOHD-SODIVDIOH-UHDSV
DATABASE_NAME, 0, KBHDBVJH-SDVHIOHD-SODIVDIOH-UHDSV
Then run the following queries to obtain a new broker for your database...
ALTER DATABASE DATABASE_NAME SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE DATABASE_NAME SET NEW_BROKER;
ALTER DATABASE DATABASE_NAME SET MULTI_USER;
Run the first query again and your database should be the only one in the list...
SELECT name, is_broker_enabled, service_broker_guid FROM sys.databases
WHERE service_broker_guid IN (SELECT service_broker_guid FROM sys.databases where name = 'DATABASE_NAME');
... now returns ...
name, is_broker_enabled, service_broker_guid
DATABASE_NAME, 1, ASJCBUHBC-7UIOSUI-IUGGUI87-IUGHUIG

I found a very simple solution for that- just simlpy assign new service broker, like this:
public void TurnOnBroker()
{
if (!this.database.BrokerEnabled)
{
this.server.KillAllProcesses(this.database.Name);
string brokerCommand = String.Format("ALTER DATABASE {0} SET NEW_BROKER", this.database.Name);
this.database.ExecuteNonQuery(brokerCommand);
RefreshConnection();
}
}

Related

Querying SQL Server on remote host arises a Character Set Problem

When calling to SQL SERVER on remote hosting with query below a problem occurs:
SELECT COLUMN_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '..ş'
Table Names, having Local Culture Alphabet do not return in query, although they are in schema. For example, tables with Turkish Chars like 'ı' 'ş' not seen in query result...
you have to set collation when creating or altering:
use master
ALTER DATABASE YourDataBase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE YourDataBase COLLATE Turkish_CI_AS
ALTER DATABASE YourDataBase SET MULTI_USER
this time no need to use N' in each query.

Error getting SQL Server database out of recovery mode?

I have a SQL Server database in recovery status this is the script i used to try to get it out of recovery mode:
--Step 1 (Check Database Status)
SELECT
DATABASEPROPERTYEX ('CM_DSS', 'status') AS '"CM_DSS Current Status"',
DATABASEPROPERTYEX ('ReportServer', 'status') AS '"ReportServer Current Status"',
DATABASEPROPERTYEX ('ReportServerTempDB', 'status') AS '"ReportServerTempDB Current Status"',
DATABASEPROPERTYEX ('SUSDB', 'status') AS '"SUSDB Current Status"'
--Step 2 (Set Database into Single User Mode)
ALTER DATABASE ReportServer SET Single_User
--Step 3 (Set Database in Emergency Mode)
ALTER DATABASE ReportServer SET EMERGENCY
--Step 4 (Repair Missing Log File with Dataloss
DBCC CHECKDB('ReportServer', REPAIR_ALLOW_DATA_LOSS)
--Step 5 Set Database Access for Everyone
ALTER DATABASE ReportServer SET MULTI_USER
This is the error I get back after trying to run step 2 :
Msg 5011, Level 14, State 9, Line 2
User does not have permission to alter database 'ReportServer', the database does not exist, or the database is not in a state that allows access checks.
Msg 5069, Level 16, State 1, Line 2
ALTER DATABASE statement failed.
I am sure I have admin rights to this db , if not how do I check? Because the ReportServer database does exist it is just in recovery mode..
ALTER DATABASE [database_name]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
should kick everyone else off so it can change to single user mode
To check what your rights are,
use [database_name]
select *
from sys.fn_my_permissions (NULL,'DATABASE')
and you need to have ALTER rights on the database to use SET on the database
Example:

Monitor when Database is created and receive an email

I created a trigger at the level of the server to control when a db is created.
I have this script that was working fine on SQL 2014, now we moved to SQL 2017, the script is working but I receive lot of emails
CREATE TRIGGER [ddl_trig_database]
ON ALL SERVER
FOR ALTER_DATABASE
AS
DECLARE #results NVARCHAR(max)
DECLARE #subjectText NVARCHAR(max)
DECLARE #databaseName NVARCHAR(255)
SET #subjectText = 'NEW DATABASE Created on ' + ##SERVERNAME + ' by ' + SUSER_SNAME()
SET #results = (SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)'))
SET #databaseName = (SELECT EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]', 'VARCHAR(255)'))
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'EmailProfile',
#recipients = 'test#domain.com',
#body = #results,
#subject = #subjectText,
#exclude_query_output = 1 --Suppress 'Mail Queued' message
GO
I receive for example in different emails each of these lines:
ALTER DATABASE [testNewDB] SET DELAYED_DURABILITY = DISABLED
ALTER DATABASE [testNewDB] SET RECOVERY FULL
ALTER DATABASE [testNewDB] SET READ_WRITE
ALTER DATABASE [testNewDB] SET READ_COMMITTED_SNAPSHOT OFF
There are more so I believe the trigger is sending the info for each configuration parameter of the new db created, any idea how to receive only the info of the new DB created without all the rest?
You can replace ALTER_DATABASE with CREATE_DATABASE, but this will not catch a restore event because a restore does not generate a DLL event.
CREATE TRIGGER [ddl_trig_database]
ON ALL SERVER
FOR CREATE_DATABASE
AS
The following article covers a solution that will work around the missing DDL event:
DDL triggers enable us to audit DDL changes but there are a few
missing events, design decisions and installation complications. This
post explains and provides a full solution that includes auditing for
database restores (there is no DDL event for this) and an incremental
self install, which keeps the whole server audit configured for DDL
auditing.
https://www.sqlservercentral.com/forums/topic/sql-2008-ddl-auditing-a-full-self-installingupdating-solution-for-whole-server
The solution in the article for RESTORE events involves a job that runs to check for new databases:
SQL 2008 Audit RESTORE DATABASE
SQL Agent job which runs (in less than 1 second) every 1 minute to
copy new restore database auditing information from
msdb.dbo.restorehistory to dbadata.dbo.ServerAudit. If it finds that a
database restore has happened but has not been audited it
automatically runs the “Setup DDL Audit” job because there is a
possibility that the restored database is not configured for DDL
auditing as expected.

What are the user permissions needed to enable snapshot isolation in a DB?

I have a user who has db_datareader, db_datawriter permissions on a DB.
I want to set the isolation levels to the DB to which the user has access to.
What permissions will my user need to be able to set these.
DB used: SQL SERVER 2008
This is not setting an isolation level:
ALTER DATABASE dbname SET ALLOW_SNAPSHOT_ISOLATION ON;
That is altering the database. For that you need to provide them with ALTER rights on the database:
GRANT ALTER ON DATABASE::dbname TO username;
Otherwise you get this error:
Msg 5011, Level 14, State 9, Line 1
User does not have permission to alter database 'dbname', the database does not exist, or the database is not in a state that allows access checks.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
Now, ALTER is all or nothing - you can't use it to allow them to change the allow snapshot setting but not other settings like forced parameterization, compatibility level, etc. You can do this much more granularly, though; perhaps you could create a stored procedure that does something like this:
CREATE PROCEDURE dbo.SetIsolationLevel
WITH EXECUTE AS OWNER
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql NVARCHAR(MAX) = N'ALTER DATABASE '
+ QUOTENAME(DB_NAME())
+ ' SET ALLOW_SNAPSHOT_ISOLATION ON;';
EXEC sp_executesql #sql;
END
GO
Now you just have to give the user EXEC permissions on that procedure, which your user can now call (instead of the ALTER DATABASE command explicitly and instead of giving them full ALTER DATABASE privileges):
GRANT EXEC ON dbo.SetIsolationLevel TO username;
GO
You can simulate them calling this stored procedure by logging in as them, or using the EXECUTE AS feature directly:
EXECUTE AS USER = 'username';
GO
EXEC dbo.SetIsolationLevel;
GO
REVERT;
GO
Another idea is to simply set the model database to have this setting, than any new databases that get created for your users will automatically inherit it, then you don't have to worry about making them turn it on.
ALTER DATABASE model SET ALLOW_SNAPSHOT_ISOLATION ON;
GO
CREATE DATABASE splunge;
GO
SELECT snapshot_isolation_state_desc FROM sys.databases WHERE name = N'splunge';
Result:
ON

How long should SET READ_COMMITTED_SNAPSHOT ON take?

How long should it take to run
ALTER DATABASE [MySite] SET READ_COMMITTED_SNAPSHOT ON
I just ran it and it's taken 10 minutes.
How can I check if it is applied?
You can check the status of the READ_COMMITTED_SNAPSHOT setting using the sys.databases view. Check the value of the is_read_committed_snapshot_on column. Already asked and answered.
As for the duration, Books Online states that there can't be any other connections to the database when this takes place, but it doesn't require single-user mode. So you may be blocked by other active connections. Run sp_who (or sp_who2) to see what else is connected to that database.
Try this:
ALTER DATABASE generic SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE
OK (I am the original questioner) so it turns out this whole time I didn't even have the darn thing enabled.
Here's the ultimate code to run to enable snapshot mode and make sure it is enabled.
SELECT is_read_committed_snapshot_on, snapshot_isolation_state_desc,snapshot_isolation_state FROM sys.databases WHERE name='shipperdb'
ALTER DATABASE shipperdb SET allow_snapshot_isolation ON
ALTER DATABASE shipperdb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE shipperdb SET read_committed_snapshot ON
ALTER DATABASE shipperdb SET MULTI_USER
SELECT is_read_committed_snapshot_on, snapshot_isolation_state_desc,snapshot_isolation_state FROM sys.databases WHERE name='shipperdb'
This works even with connections active (presumably you're fine with them getting kicked out).
You can see the before and after state and this should run almost immediately.
IMPORTANT:
The option READ_COMMITTED_SNAPSHOT above corresponds to IsolationLevel.ReadCommitted in .NET
The option ALLOW_SNAPSHOT_ISOLATION above corresponds to IsolationLevel.Snapshot in .NET
Great article about different versioning
.NET Tips:
Looks like Isolationlevel.ReadCommitted is allowed in code even if not enabled by the database. No warning is thrown. So do yourself a favor and be sure it is turned on before you assume it is for 3 years like I did!!!
If you're using C# you probably want the ReadCommitted IsolationLevel and not Snapshot - unless you are doing writes in this transaction.
READ COMMITTED SNAPSHOT does optimistic reads and pessimistic writes. In contrast, SNAPSHOT does optimistic reads and optimistic writes. (from here)
bool snapshotEnabled = true;
using (var t = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadCommitted
}))
{
using (var shipDB = new ShipperDBDataContext())
{
}
}
In additional you may get an error about being 'unable to promote' a transaction. Search for 'promotion' in Introducing System.Transactions in the .NET Framework 2.0.
Unless you're doing something special like connecting to an external database (or second database) then something as simple as creating a new DataContext can cause this. I had a cache that 'spun up' its own datacontext at initialization and this was trying to escalate the transaction to a full distributed one.
The solution was simple :
using (var tran = new TransactionScope(TransactionScopeOption.Suppress))
{
using (var shipDB = new ShipperDBDataContext())
{
// initialize cache
}
}
See also Deadlocked article by #CodingHorror
Try this code:
if(charindex('Microsoft SQL Server 2005',##version) > 0)
begin
declare #sql varchar(8000)
select #sql = '
ALTER DATABASE ' + DB_NAME() + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE ;
ALTER DATABASE ' + DB_NAME() + ' SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE ' + DB_NAME() + ' SET MULTI_USER;'
Exec(#sql)
end
I tried the command:
ALTER DATABASE MyDB SET READ_COMMITTED_SNAPSHOT ON
GO
against a dev box but the it took 10+ minutes and so I killed it.
I then found this:
https://willwarren.com/2015/10/12/sql-server-read-committed-snapshot/
and used his code block (which took about 1:26 to run):
USE master
GO
/**
* Cut off live connections
* This will roll back any open transactions after 30 seconds and
* restricts access to the DB to logins with sysadmin, dbcreator or
* db_owner roles
*/
ALTER DATABASE MyDB SET RESTRICTED_USER WITH ROLLBACK AFTER 30 SECONDS
GO
-- Enable RCSI for MyDB
ALTER DATABASE MyDB SET READ_COMMITTED_SNAPSHOT ON
GO
-- Allow connections to be established once again
ALTER DATABASE MyDB SET MULTI_USER
GO
-- Check the status afterwards to make sure it worked
SELECT is_read_committed_snapshot_on
FROM sys.databases
WHERE [name] = 'MyDB '
Try use master database before altering current database.
USE Master
GO
ALTER DATABASE [YourDatabase] SET READ_COMMITTED_SNAPSHOT ON
GO
I didn't take a second for me when i changed my DB to single user
All you need to do is this:
ALTER DATABASE xyz SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
No need to put the database into single user mode.
You will rollback uncommitted transactions though.
Try Shut off the other SQL services so that only the SQL server service is running.
Mine ran for 5 minutes then I cancelled it because it was obvious nothing was happening. Its a brand new server so there are no other users connected. I shut off the SQL Reporting Services and then ran it again.. took less than a second to complete.
With "ROLLBACK IMMEDIATE" it took about 20-30 seconds on my db which is 300GB.
ALTER DATABASE DBNAME SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE

Resources