Friday, April 4, 2014

Finding Last Backup Time for All Database – Last Full, Differential and Log Backup

SET NOCOUNT ON
GO
SET quoted_identifier OFF
DECLARE @dbname AS VARCHAR(80)
DECLARE @msgdb AS VARCHAR(100)
DECLARE @dbbkpname AS VARCHAR(80)
DECLARE @dypart1 AS VARCHAR(2)
DECLARE @dypart2 AS VARCHAR(3)
DECLARE @dypart3 AS VARCHAR(4)
DECLARE @currentdate AS VARCHAR(10)
DECLARE @server_name AS VARCHAR(30)
SELECT @server_name = @@servername
SELECT @dypart1 = DATEPART(dd,GETDATE())
SELECT @dypart2 = DATENAME(mm,GETDATE())
SELECT @dypart3 = DATEPART(yy,GETDATE())
SELECT @currentdate= @dypart1 + @dypart2 + @dypart3
PRINT "#####################################################################"
PRINT "# SERVERNAME : "+ @server_name + " DATE : "+ @currentdate +"#"
PRINT "#####################################################################"
PRINT "DatabaseName Full Diff TranLog"
PRINT "##########################################################################################################################################"
SELECT SUBSTRING(s.name,1,50) AS 'DATABASE Name',
b.backup_start_date AS 'Full DB Backup Status',
c.backup_start_date AS 'Differential DB Backup Status',
d.backup_start_date AS 'Transaction Log Backup Status'
FROM MASTER..sysdatabases s
LEFT OUTER JOIN msdb..backupset b
ON s.name = b.database_name
AND b.backup_start_date =
(SELECT MAX(backup_start_date)AS 'Full DB Backup Status'
FROM msdb..backupset
WHERE database_name = b.database_name
AND TYPE = 'D') -- full database backups only, not log backups
LEFT OUTER JOIN msdb..backupset c
ON s.name = c.database_name
AND c.backup_start_date =
(SELECT MAX(backup_start_date)'Differential DB Backup Status'
FROM msdb..backupset
WHERE database_name = c.database_name
AND TYPE = 'I')
LEFT OUTER JOIN msdb..backupset d
ON s.name = d.database_name
AND d.backup_start_date =
(SELECT MAX(backup_start_date)'Transaction Log Backup Status'
FROM msdb..backupset
WHERE database_name = d.database_name
AND TYPE = 'L')
WHERE s.name <>'tempdb'
ORDER BY s.name

Wednesday, January 22, 2014

Contained Databases in SQL Server 2012

Contained Databases in SQL Server 2012


Contained
databases are a new feature in SQL Server 2012 and are defined on MSDN Library asa database that is isolated from other databases and from the instance of SQL Server that hosts the database.

The
containment setting of a database can be NONE, PARTIAL or FULL. But only NONE and PARTIAL are supported on SQL Server 2012.

There
are three types of containment hinted at in SQL Server 2012:
  • NONE: This default containment mode is what exists prior to SQL Server 2012. There are no boundaries defined or visible.
  • PARTIAL: With partially contained databases, you have the ability to define clearer boundaries between databases and the server, making it easier for metadata to be hosted within the databases. This, in turn, makes SQL Server databases more portable and less dependent on underlying hosts.
  • FULL: Fully contained databases are only alluded to in SQL Server 2012 BOL in a few locations and aren't currently available as an option. It's assumed, however, that full containment will enable greater database portability and potentially allow for strict enforcement of containment boundaries as a means of fully isolating databases from each other and from the underlying host.

Benefits and characteristics.


The
following are some of the benefits and characteristics that contained databases have:

1.
They make easier to migrate databases from one server to another. Errors related to orphan users are no longer an issue with contained databases, since a contained database user can now be created without an associated login.

2.
Authentication can now occur at the database level.

3.
Contained database users can be Windows and SQL Server authentication users.

4.
A contained database user can access only contained database objects. They cannot access system databases and cannot access server objects.

5.
Metadata is stored on the contained database and not stored on system databases. This makes contained databases more portable than the databases we know.

 
Disadvantages and limitations.


Some
disadvantages and limitations are the following:

1.
There are some security concerns. A database owner can create contained database users without the permission of a DBA. The possibility of denial of service attacks exist with contained databases using AUTO_CLOSE option. For security best practices about contained databases, please see the references shown at the end of the article.2. Partially contained databases cannot use replication, change data capture, change tracking, numbered procedures, schema-bound objects that depend on built-in functions with collation changes. You may find more limitations on the references shown at the end of this article.Requirements of contained databases.
1 It is required to enable contained databases on the instance.2. The contained database needs to be added to the connection string or specified when connecting via SQL Server Management Studio.Step-by-step instructions on how to create a contained database.

To
be able to create contained databases on a SQL Server 2012 instance, we need to enable the contained database authentication option on the instance.

Open
SQL Server 2012 Management Studio, connect to the instance, make a right click on the name of the instance on Object Explorer, select the Advanced page on the "Select a page" panel, and set to true the "Enable Contained Databases" option.

Alternatively,
you can use sp_configure system stored procedure to enable contained databases on the instance, as shown below.
EXEC sp_configure 'show advanced', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'contained database authentication', 1GO
RECONFIGURE
GO





When
a database is created the "Containment type" should be set to "partial" to make the database a contained database, as shown below.

This
can be done using T-SQL too, as shown below.


A
contained database allows the creation of a database user that is not associated to an instance login. A contained database user can be created expanding the Security folder on the contained database, making a right click on the Users folder and selecting the "New User" option.



Next,
set the user type to "SQL user with password", assign a user name, set the password for the database user and specify the default schema for the user.


Specify
the roles for this user on the database and click OK.



If
you would like to create the contained database user using T-SQL, please see the example below.CREATE USER [MorilloCD2User]
    WITH PASSWORD=N'p@ssw0rd123',
   
DEFAULT_SCHEMA=[dbo]GO





To
connect a contained database user to a contained database, the database name should be specified on the connection string. If you are using SQL Server Management Studio, on the Connect to Server dialog specify the authentication, specify the user name, provide the password, click on the Options button and specify the database name on the "Connect to database" combo box.

Thursday, June 20, 2013

Display the size of all tables in a database



Below is the query to get the list of tables with sizes in a database:
 

CREATE PROCEDURE getAllTablesSize
AS
BEGIN
      DBCC UPDATEUSAGE (0) WITH NO_INFOMSGS;
      CREATE TABLE
            #temp (
                         [name] varchar(250),
                         [rows] varchar(50),
                         [reserved] varchar(50),
                         [data] varchar(50),
                         [index_size] varchar(50),
                         [unused] varchar(50)
                       );
      INSERT #temp EXEC ('sp_msforeachtable ''sp_spaceused ''''?''''''');
      UPDATE  #temp
      SET
            [rows] = LTRIM(RTRIM(REPLACE(t.rows,'KB',''))),
            [reserved] = LTRIM(RTRIM(REPLACE(t.reserved,'KB',''))),
            [data] = LTRIM(RTRIM(REPLACE(t.data,'KB',''))),
            [index_size] = LTRIM(RTRIM(REPLACE(t.index_size,'KB',''))),
            [unused] = LTRIM(RTRIM(REPLACE(t.unused,'KB','')))
      FROM #temp AS t
      SELECT
            SUM(CAST([reserved] as decimal))/1024 AS 'Total reserved MB',
            SUM(CAST([data] as decimal))/1024 AS 'Total data MB',
            SUM(CAST([index_size] as decimal))/1024 AS 'Total index_size MB',
            SUM(CAST([unused] as decimal))/1024 AS 'Total unused MB'
      FROM
            #temp
      SELECT
            [name] ,
            CAST([rows] as INT)'rows' ,CAST([reserved] as INT)/1024 'reserved MB',
            CAST([data] as INT)/1024 'data MB' ,
            CAST([index_size]/1024 as INT)'index_size MB',
            CAST([unused] as INT)/1024 'unused MB'
      FROM
            #temp
      ORDER BY  name
      DROP  TABLE #temp;
END
GO
--EXECUTE getAllTablesSize

Thursday, May 30, 2013

SQL DBA reference websites

Hi Viewers,

Below are the sites for SQL DBA reference which i am using:

http://www.careerride.com/SQL-DBA-different-types-of-BACKUPs.aspx
http://www.sqldbadiaries.com/
http://www.sql-server-helper.com/
http://strictlysql.blogspot.in/2010/06/finding-cpu-utilization-in-sql-server.html
http://www.sqlmag.com/articles/sqlserverdenali/sql-server-denali-alwayon-140199
http://beyondrelational.com/blogs/community/archive/2011/12/21/sql-server-2012-denali-alwayson.aspx
http://insqlserver.com/
http://www.a2zmenu.com/Blogs/SQL/store%20procedure%20to%20take%20database%20backup.aspx
http://www.jamesserra.com/archive/2012/02/sql-server-2012-denali-t-sql-enhancements/
http://www.mytechmantra.com
http://sqlwithmanoj.wordpress.com/tag/t-sql-interview-questions/
http://www.sql-server-citation.com/2007/08/resource-for-new-sql-server-dba-and.html
http://www.sqlcourse.com/intro.html
http://sqlserver2008tutorial.com/member.html
http://dba.fyicenter.com/faq/sql_server/
http://www.careerride.com/SQLServer-Interview-Questions.aspx
http://www.mssqltips.com/sql-server-tip-category/53/dba-best-practices/
http://learnsqlwithbru.com/sql-server-interview-questions-and-answers/sql-server-dba-q-and-a/
http://www.sqlmonkeys.com
http://www.bradmcgehee.com/2010/03/an-introduction-to-sql-server-2008-audit/
http://sqlblog.com/blogs/jonathan_kehayias/archive/2009/06/06/getting-log-space-usage-without-using-dbcc-sqlperf.aspx
http://sqldbachronicles.blogspot.in/2011/05/check-log-space-dbcc-sqlperflogspace.html
http://dbaspot.com/microsoft-sql-server/
http://sql-school.blogspot.in/
http://arshadali.blogspot.in/
http://www.blackwasp.co.uk/SQLProgrammingFundamentals.aspx
http://vblvbl.brinkster.net/SQL7DI/ch12c.htm
http://www.sqlserver-training.com/sql-server-2008-interview-questions/-
http://www.sqlvillage.com/Interviews/Inerview%20Questions%20-%20Part%202.asp
http://www.slideshare.net
http://www.mssqltips.com/sqlservertip/1253/sql-server-dba-concurrency-and-locking-interview-questions/
http://sqlserverplanet.com/
http://sqlserverpedia.com/wiki/Database_Administration
http://www.mssqlfix.com/2011/02/mcitp-70-450-exam-sample-question-and.html
http://sql-server-2005.com/
http://www.sql-dba.com/sql_server_scripts.html
http://www.databasetimes.com/2009/05/keep-up-indexes-in-sql-server-2005.html
http://www.sqllion.com/2009/07/sql-server-indexes-pros-and-cons-part-2/
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=37037#top
http://www.sqlserverblogforum.com/
http://www.databasejournal.com/features/mssql/archives/
http://www.reference.com/motif/computers/difference-between-a-database-instance-and-schema


Backup and Restore
http://www.sqlbackuprestore.com/speedinguprestores.htm


upgrade sql2000 to sql2005:
http://www.sqlservercentral.com/articles/Administration/2988/   
http://www.sqlservercentral.com/articles/News/3036/         
http://gouravverma.blogspot.in/2008/04/top-10-new-features-in-sql-server-2005.html

SQL Server 2008:
http://sqlcat.com/sqlcat/b/top10lists/archive/2009/01/30/top-10-sql-server-2008-features-for-the-database-administrator-dba.aspx
http://blog.sqlauthority.com/sql-server-interview-questions-and-answers/

SQL Programming:
http://beginner-sql-tutorial.com/sql.htm
www.sqlserver2005tutorial.com/
http://dba.fyicenter.com/faq/sql_server/


Cluster:
http://clearinglogfiles.blogspot.in/2012/03/cluster-problems_3942.html

SSRS:
http://www.katieandemil.com/ssrs-create-reports-a-simple-example-of-ssrs-report

Certification:
http://www.ucertify.com/certifications/Microsoft/mcitp-database-admin-sql-server-2008.html
http://braindumps.org/
Exam: 70-432
Exam: 70-433
Exam: 70-450
Exam: 70-451
http://www.sql-training.net/

SQL SERVER 2012:
http://www.mssqltips.com/sql-server-tip-category/117/sql-server-denali/
http://www.mssqltips.com/sql_server_dba_tips.asp
http://social.technet.microsoft.com/wiki/contents/articles/3783.what-s-new-in-sql-server-2012-en-us.aspx
http://www.infosysblogs.com/microsoft/2012/03/sql_server_2012_-_overview.html#more
http://www.sqlskills.com/T_MCMVideos.asp
http://www.sqlserver-training.com/what-is-contained-database-in-sql-server/-
http://www.devproconnections.com/article/sqlserverdenali/sql-server-denalis-security-enhancements-140479
http://www.yourdba.com/?p=123
http://sql-server-tuning.com/2011/11/06/sql-server-2012-availability-enhancements-part-ii/
http://www.simple-talk.com/sql/database-administration/sql-server-2012-alwayson/

Tutorial:
http://www.infotechguyz.com/SQLServer2012Tutorial.html

Ebook:
http://www.andreavb.com/forum/viewtopic_9981.html

Interview:
http://www.mssqltips.com/sqlservertip/1472/sql-server-dba-phone-interview-questions/
http://www.sqlserver-expert.com/search/label/TempDB
http://interviewquestions4u.com/database/sql-server/sql-server-dba
http://www.mssqlfix.com/2010/11/sql-dba-interview-questions-with.html
https://www.logicsmeet.com/forum/35808-sql-server-interview-questions-and-answers-for-experienced.aspx
http://www.mssqltips.com/sql-server-tip-category/46/interview-questions-dba/
http://www.careerride.com/SQLServer-Interview-Questions.aspx#sql16
http://www.careerride.com/Online-practice-test.aspx

Websites for SQL DBA:
http://www.mssqltips.com/sqlservertip/948/sql-server-websites/
www.dba-24x7.com/sql-server-services.php


Indexes:
http://msdn.microsoft.com/en-us/library/ms175049.aspx
http://www.simple-talk.com/sql/learn-sql-server/sql-server-index-basics/
http://www.codeproject.com/Articles/39006/Overview-of-SQL-Server-2005-2008-Table-Indexing-Pa
http://www.sqllion.com/2009/07/sql-server-indexes-pros-and-cons-part-2/