select |
Adding fuel to my passion on SQL Server
select |
When trying to restore the database with snapshots got the following error SQL message
SQL Server T-SQL Command
| restore database PRD_Accounts from disk ='H:\PRD_Accounts_backup_daily.bak' with replace, stats |
Error:
Msg 5094, Level 16, State 2, Line 1
The operation cannot be performed on a database with database snapshots or active DBCC replicas.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
Resolution
Since we cannot restore a database if we have any snapshots.
Please drop all snapshots and try database restore again.
1. To check the database snapshot and it’s source database
| select database_id, name,source_database_id,Source_DBName=DB_NAME(source_database_id) |
2. Drop the Snapshot database
| DROP DATABASE Accounts_Rpt |
3. Create script of snapshot database, before dropping it. If at all we need to re-establish the snapshot after restore we can use it
| USE master /****** Object: Database [Benefits_Rpt_sol48u] Script Date: 12/03/2012 00:01:02 ******/ |
sp_cycle_agent_errorlog
Every time SQL Server Agent is started, the current SQL Server Agent error log is renamed to SQLAgent.1; SQLAgent.1 becomes SQLAgent.2, SQLAgent.2 becomes SQLAgent.3, and so on. sp_cycle_agent_errorlog enables you to cycle the error log files without stopping and starting the server.
This stored procedure must be run from the msdb database.
Error on executing of sp_cycle_agent_errorlog
USE msdb
go
EXEC sp_cycle_agent_errorlog
GO
Msg 22022, Level 16, State 1, Line 0
SQLServerAgent Error: Access is denied.
Each time the stored procedure was run it just overwritten the current agent log without renaming it and because of this only one SQLAGENTOUT file was left.
SQL Mail Sent fail with errors below;
Error from the SQL Mail log:
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2012-11-07T22:09:50). Exception Message: Could not connect to mail server. (No connection could be made because the target machine actively refused it 205.191.22.44:25). )
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2012-01-17T11:13:58). Exception Message: Could not connect to mail server. (No such host is known). )
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2012-01-17T11:12:44). Exception Message: Cannot send mails to mail server. (Failure sending mail.). )
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2010-10-24T19:55:11). Exception Message: Could not connect to mail server. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 16.236.32.51:25). )
Resolution:
From the error message it is clear that it is a communication issue between SQL Server host and the Mail Exchange Server.
Problem was related to Exchange Server not allowing relaying from SQL Server.
Please engage Network Administrator to add SQL Server host IP Address to allow to send emails taking below steps, like
On Mail server > Exchange Server Manager > SMTP Connector Properties
> Access Properties > Access Tab > Relay button > Add IP Address of SQL Server
Also cross check if the Anti Virus (AV) program running on the SQL Server host. AV blocks services from sending on TCP-25. Adjust the AV software accordingly. Add DATABASEMAIL90.EXE to the list of innocent programs in your AV.
It would be useful to cross check, when the Linked Server remote login password has changed.
In this case, linked server will wail due to wrong password. To fix this we have reset the correct password.
| SELECT s.server_id,S.name,S.product,S.provider,l.remote_name,'Remote_Login_Modified_Dt'=l.modify_date,s.modify_date |
Before you run the following query, replace <database_name> and <schema_name.object_name> with valid names.
| USE <database_name>; |
Reference: http://msdn.microsoft.com/en-us/library/ms190324.aspx
Before you run the following query, replace <database_name> and <n_days> with valid values.
| USE <database_name>; |
Source: http://msdn.microsoft.com/en-us/library/ms190324.aspx
It leverages on the system stored procedure sys.sp_testlinkedserver
| SET NOCOUNT ON; DECLARE srvname INSENSITIVE CURSOR FOR |
On one of my SQL Server 2008 Instance, the msdb database sizes around 4 GB. I decided to find out the which table / index occupies major junk on this. I have used query below;
| --- Query 1, if there is partition SELECT TOP 10 SERVERNAME=@@SERVERNAME,DB_NAME=DB_NAME(),TABLE_NAME=OBJECT_NAME(I.ID),INDEX_NAME=I.NAME,INDID,USED, ROWS, SIZE_N_MB = ROUND((USED*8.0/1024.0),2), |
| --- Query 2 If there are partitions SELECT object_name(i.object_id) as objectName, |
The output resulted;
The following 5 big tables takes up 88% of the total database size of 3.8 GB. These 5 tables occupy 3.3 GB on the msdb database.
Query 1: Output
| TABLE_NAME | INDEX_NAME | INDID | USED | ROWS | SIZE_N_MB | ROWMODCTR | STATISTICDT |
| backupfile | PK__backupfi__57D1800A17C286CF | 1 | 233057 | 4438738 | 1820.76 | 2646909 | 2/14/2011 15:57 |
| backupset | PK__backupse__21F79AAB0E391C95 | 1 | 115791 | 1489763 | 904.62 | 228112 | 6/19/2011 6:00 |
| backupmediafamily | PK__backupme__0C13C86F0880433F | 1 | 50738 | 1489616 | 396.39 | 23386 | 7/26/2011 7:00 |
| backupfilegroup | PK__backupfi__760CD67A12FDD1B2 | 1 | 43284 | 2967333 | 338.16 | 1775145 | 2/14/2011 15:57 |
| backupmediaset | PK__backupme__DAC69E4D04AFB25B | 1 | 29250 | 1489616 | 228.52 | 44462 | 7/22/2011 15:43 |
| sysjobhistory | clust | 1 | 3553 | 33177 | 27.76 | 2428 | 7/25/2011 8:14 |
Query 2: Output
| objectName | indexName | totalPages | usedPages | dataPages | totalSpaceMB | usedSpaceMB | dataSpaceMB | Rows |
| backupfile | PK__backupfi__57D1800A17C286CF | 233073 | 233043 | 231653 | 1820 | 1820 | 1809 | 4438738 |
| backupset | PK__backupse__21F79AAB0E391C95 | 100217 | 100200 | 99826 | 782 | 782 | 779 | 1489763 |
| backupfilegroup | PK__backupfi__760CD67A12FDD1B2 | 43289 | 43283 | 43077 | 338 | 338 | 336 | 1489616 |
| backupmediafamily | PK__backupme__0C13C86F0880433F | 39473 | 39459 | 39287 | 308 | 308 | 306 | 2967333 |
| backupmediaset | PK__backupme__DAC69E4D04AFB25B | 18737 | 18728 | 18654 | 146 | 146 | 145 | 1489616 |
| SELECT COUNT(*) AS 'TotalRecords', |
Resolution:
The solution is we have to purge these MSDB Backup and Restore history tables
1. The sp_delete_backuphistory system stored procedure takes a single parameter - a cutoff date. Any data older than the supplied date is purged from the msdb tables.
2. The sp_delete_database_backuphistory system stored procedure allows you to delete historical backup data for a specific database. Unfortunately, this procedure does not offer the finer option of choosing a cutoff date. It's all or nothing.
3. sp_purge_jobhistory Removes the history records for a job.
4. Try considering creating indexes on the above said 5 tables, if no of rows are huge.
Source: http://weblogs.sqlteam.com/geoffh/archive/2008/01/21/MSDB-Performance-Tuning.aspx
| use msdb Create index IX_backupset_backup_set_id on backupset(backup_set_id) |
5. Alternatively, check when the statistics of these tables are last updated. Just run sp_updatestats. Just try sp_delete_backuphistory after the statistics are updated. The result is awesome! Very quick response.
Permanent Resolution
6. Further restrict the Job history growth by setting in SQL Agent –> History tab.
7. Also on the SQL Server Maintenance Plan “History Cleanup Task” can be of helpful
SQL Server Management Studio – SSMS is a 32 bit application. Even when we install 64 bit SQL Server Database Engine, we will get 32 bit SSMS installed on the server.
Default SSMS install location
| 32 Bit Server | C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE |
| 64 Bit Server | C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE |
Error message: Alter failed for Server ‘SivaSQL2008’
When I ran SQL Profiler and checked why its failing, observed;
Errors when it was trying to execute
SP_CONFIGURE 'USER OPTION',0 ;
RECONFIGURE
Error: 5808, Severity: 16, State: 1
Ad hoc update to system catalogs is not supported.
The reason for why AD HOC UPDATE TO SYSTEM CATALOGS is not supported.
Why it is trying to change SYSTEM CATALOGS at first place?
In SQL Server 2000 and older, this option allowed users to make updates directly to the system tables.
In SQL Server 2005 and higher, the system tables are, replaced with the Resource Database and system views.
This option is no longer supported in SQL Server 2005 and higher version, and though we can set Allow Updates to 1 with no error. As soon as we run RECONFIGURE, we will receive the error that ad hoc updates are not supported.
Because Allow Updates had been set to 1 and my Maintenance Plan runs the RECONGIFURE statement, this error was thrown and the Maintenance Plan failed.
As per Books On Line:
Changing the allow updates option will cause the RECONFIGURE statement to fail. Changes to the allow updates option should be removed from all scripts.
Resolution:
So to resolve is configured ALLOW UPDATE back to 0 and again ran Maintenance Plan and it executes fine.
| sp_configure 'allow update', 0 |
In this post I would like to address the requirement of;
SQL Server Database Publishing Wizard enables the deployment of SQL Server databases into a hosted environment on either a SQL Server 2000 or 2005 server.
It generates a single SQL script file which can be used to recreate a database (both schema and data) in a shared hosting environment where the only connectivity to a server is through a web-based control panel with a script execution window.
If supported by the hosting service provider, the Database Publishing Wizard can also directly upload databases to servers located at the shared hosting provider.
We can use the Generate and Publish Scripts Wizard to create scripts for transferring a database from one instance of the Database Engine to another. You can generate scripts for a database on an instance of the Database Engine in your local network, or from SQL Azure.
The generated scripts can be run on another instance of the Database Engine or SQL Azure.
We can also use the wizard to publish the contents of a database directly to a Web service created by using the Database Publishing Services.
We can create scripts for an entire database, or limit it to specific objects.
What version of SQL Servers are supported on this?
| Source Database | Target Database | |
| SQL Server Instance Version Supported |
|
|
| Permission needed | db_ddladmin | db_ddladmin |
| Pre-existence | The target database must be created at the hosting provider before the source database is published. Publishing overwrites objects in that existing database |
Supported Objects for Publishing
The following table lists the objects that can be published and the versions of SQL Server on which they are supported by the Generate and Publish Scripts Wizard.
| Database object | SQL Server 2008 R2 | SQL Server 2008 | SQL Server 2005 | SQL Server 2000 |
| Application role | Yes | Yes | Yes | Yes |
| Assembly | Yes | Yes | Yes | No |
| CHECK constraint | Yes | Yes | Yes | Yes |
| CLR (common language runtime) stored procedure1 | Yes | Yes | Yes | No |
| CLR user-defined function | Yes | Yes | Yes | No |
| Database role | Yes | Yes | Yes | Yes |
| DEFAULT constraint | Yes | Yes | Yes | Yes |
| Full-text catalog | Yes | Yes | Yes | Yes |
| Index | Yes | Yes | Yes | Yes |
| Rule | Yes | Yes | Yes | Yes |
| Schema | Yes | Yes | Yes | No |
| Stored procedure1 | Yes | Yes | Yes | Yes |
| Synonym | Yes | Yes | Yes | Yes |
| Table | Yes | Yes | Yes | Yes |
| User2 | Yes | Yes | Yes | Yes |
| User-defined aggregate | Yes | Yes | Yes | No |
| User-defined data type | Yes | Yes | Yes | Yes |
| User-defined function | Yes | Yes | Yes | Yes |
| User-defined table | Yes | Yes | No | No |
| User-defined type | Yes | Yes | Yes | No |
| View1 | Yes | Yes | Yes | Yes |
| XML schema collection | Yes | Yes | Yes |
1 Published without encryption.
2 Any non-system users that exist in the database are published as Roles.
How to generate SQL Scripts using Generate and Publish Scripts Wizard
We can create Transact-SQL scripts for multiple objects by using the Generate and Publish Scripts Wizard.. You can also generate a script for individual objects or multiple objects by using the Script as menu in Object Explorer.
Use the Generate and Publish Scripts Wizard to create a Transact-SQL script for many objects.
The wizard generates a script of all the objects in a database, or a subset of the objects that you select. The wizard has many options for your scripts, such as whether to include permissions, collation, constraints, and so on.
Step 1:
In Object Explorer, expand Databases, right-click a database, point to Tasks, and then click Generate Scripts. Follow the steps in the wizard to script the database objects.
Script entire database and all database objects
Click to generate scripts for all objects in the database and to include a script for the database itself.
Step 1.3: On the Set Scripting Options page, select Save scripts to a specific location.
Select Save scripts to a specific location
Summary:
Click Finish.
To generate the Script for the Data on the table:
Set “Script Data: True “
Select specific database objects
Click to limit the wizard to generate scripts for only the specific objects in the database that you choose.
For example I have selected Tables Object
Within Tables Object, I have chosen HumanResources.Employee Table
Select Save scripts to a specific location
Script Wizard Summary
View of the Output file generated:
References:
Using the Generate and Publish Scripts Wizard
How to: Generate a Script (SQL Server Management Studio)
I got error message below when i try to drop a login named ‘test_login’
Error Message:
Msg 15186, Level 16, State 1, Line 1
The server principal is set as the execution context of a trigger or event notification and cannot be dropped.
It means that there are execute as username in a stored procedure or function or triggers, and that is why it will not allow me to drop the user.
EXECUTE permission - a check is made to see if this permission is granted. If yes the user is allowed to run the stored procedure
EXECUTE AS - when the stored procedure is run, it will run under the context of the user specified in the clause rather than the context of the user who has run it.
Use the query below find the execute as object
| select object_name(object_id) from sys.sql_modules where execute_as_principal_id = user_id('test_login') |
Service Pack 4 (SP4) for Microsoft SQL Server 2005 is now available for download.
SQL Server 2005 SP4 includes SQL Server 2005 SP3 cumulative update 1 to 11, customer requested fixes, along with instances of the SQL Server 2005 SP4 database Engine support for DAC operations.
It can be downloaded from
Error:
SQL Server has encountered 2 occurrence(s) of cachestore flush for the 'Bound Trees' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
Cause:
When we take the database OFFLINE this error raised.
Reference: http://support.microsoft.com/kb/917828
I reproduced the error below;
Issue 1:
| Msg 916, Level 14, State 1, Line 1 The server principal "Test_Login" is not able to access the database "testdb" under the current security context. |
1. Created database ‘testdb’
2. Created the login ‘Test_Login’. The login not granted access on the database ‘testdb’
3. On logging on ‘Test_Login’, tried accesing the database ‘testdb’ and got the error.
From the error message, when the server principal ( login) does not have access on the database the error occurs.
Resolution 1:
Grant the database access for the login. For example;
| Use testdb Create User Test_Login for Login Test_Login |
Issue 2:
The database is in “Restricted Mode”
Try the query below, to access the database in different user context
| Use testdb |
Msg 916, Level 14, State 1, Line 1
The server principal "Test_Login" is not able to access the database "testdb" under the current security context.
Reference :
"The server principal LoginName is not able to access the database DatabaseName under the current security context"
Are you curious to find the Database Backup / Restore status?
Do you have to find out the status the DBCC CHECKDB ?
Do you have to report when the ROLLBACK would finish ?
Do you want judge how long the Database Shrink will take?
Here is the answer!
The query listed below can be used to find the Percentage of work completed for the following commands:
We have to replace the where condition filter based on the requirement.
| SELECT command, |
The default behavior of SSMS when we login, it will show all the databases available on the SQL Server Instance. Irrespective of access on the databases user has.
The reason behind is, to load the SSMS faster. Otherwise, while loading the SSMS SQL Server has to verify for each databases if the user has access or not. Also if any of the databases or offline or closed, it has to figure out if that database has to be listed or not. All this takes time.
Hence by default the SSMS will show all the databases on SSMS.
If we really required to show only the database user has access, please use the command below to restrict the show all databases on SSMS,
DENY VIEW ANY DATABASE TO login_name
When VIEW ANY DATABASE is revoked, a user can only see master, tempdb, any database he owns, and the user’s current database context.
There is a whitepaper in detail SQL Server 2005 /2008 Maintenance for MOSS ( Sharepoint ).