Primary Key Script

The below script generates primary key across all the tables in the database.

;with CTE as (select distinct COLUMN_NAME,ORDINAL_POSITION,TABLE_NAME,b.name,c.index_id,
b.[object_id] as ObjectID,b.parent_object_id as [TableObjectID]
from INFORMATION_SCHEMA.KEY_COLUMN_USAGE A Inner Join sys.key_constraints B on
A.TABLE_NAME=Object_name(B.parent_object_id) and SCHEMA_NAME(SCHEMA_ID)=TABLE_SCHEMA
and A.Constraint_Name=B.Name
left outer join sys.indexes C on C.object_id=B.parent_object_id and is_primary_key=1)
,CTE2 as (select distinct T2.[objectid],T2.TableObjectID,T2.index_id,
STUFF((Select ','+Column_Name
from CTE T1
where T1.[objectid]=T2.[Objectid] and T1.[Tableobjectid]=T2.[TableObjectid]
and T1.[index_id]=T2.[index_id] order by ORDINAL_POSITION asc
FOR XML PATH('')),1,1,'') as [keycolumns] from CTE T2 )

Select distinct Table_Schema+'.'+Table_Name as [TableName],CONSTRAINT_NAME,'ALTER TABLE '+Table_Name +' ADD CONSTRAINT '+Name+ ' PRIMARY KEY '+
case when index_id=1 then ' CLUSTERED '
when index_id>1 then ' NONCLUSTERED '
when index_id=0 then '' End
+'('+keycolumns +');' as [Create Script]
from INFORMATION_SCHEMA.KEY_COLUMN_USAGE A Inner Join sys.key_constraints B on
A.TABLE_NAME=Object_name(B.parent_object_id) and A.CONSTRAINT_NAME=B.Name
Inner join CTE2 C on C.ObjectID=B.object_id and C.TableObjectID=B.parent_object_id

Advertisement

Backing up and Restoring single SSIS package

I was responding to a question on MSDN forums and part of the question was about how to backup the SSIS packages after they are deployed onto SQL Server. This is not related to SQL 2012 where SSIS packages can be deployed into it’s own SSISDB database as project model. This question was related to SQL Server 2008R2.

When SSIS packages are deployed, they are stored in MSDB database. So, MSDB database has to be restored if you need to recover the ssis packages. Of course, you should also have to put the ssis packages in a source control and have backups of it.

But an interesting point was raised – “is it possible to restore a single SSIS package”. The concerns mentioned were having to do full restore of msdb database.

I thought this is a real concern because having to restore msdb to recover a ssis package could lead loss of other important information such as backup\restore history etc. So, the question was – Is it possible to recover a single SSIS package without having to restore complete MSDB database?

So, to test this, I created a simple ssis package and deployed to sql server.
The below query returns the ssis packages deployed on the server.
Select * from msdb.dbo.sysssispackages

Now, I copied the data from msdb.dbo.sysssispackages into another table
Select * into newssispackages from msdb.dbo.sysssispackages

Now, I made another change to the SSIS package and redeployed. So, to recover the prior version of ssis package, first,I deleted the SSIS package from table

Delete from msdb.dbo.sysssispackages where name ='TestJob'
and then inserted the corresponding row from another table into dbo.sysssispackages.

Insert into dbo.sysssispackages
Select * from newssispackages where name ='TestJob'

This restored the prior version of SSIS into msdb. So, we can take table backup(Select * into) and revert back to previous versions as explained above.

I am not sure whether this works in every scenario and would be curious to know on scenarios where this won’t work.

Transaction Log Backups

Question:Does the transaction log backup contain the data from the last log backup or from the oldest active transaction?
Answer:Transaction Log backup contains log data from the last log backup.That’s why when we need to restore all the log backups in sequential order.Let’s verify using the below script.
--Session 1
Create database Dblog
go

--make sure database is in fully recovery
use DBLog
go
create table TableLog(sno int primary key identity(1,1),sname varchar(20),scity varchar(8000))
Go
begin tran
insert into TableLog values('sqlwhsiper',replicate('a',8000))

--do not commit yet.
--Commit
--sesssion 2

Backup database dblog to disk ='E:\Backup\dblog_FB.bak' with init

Backup log dblog to disk ='E:\Backup\dblog_log1.bak' with init

use DbLog
go
insert into TableLog values('sqlwhisper',replicate('a',8000))
go 10000

Backup log dblog to disk ='E:\Backup\dblog_log2.bak' with init

--Session 1. Commit the open transaction in session 1.
Commit

Backup log dblog to disk ='E:\Backup\dblog_log3.bak' with init

--Read the transaction log backup .The second log backup does not contain any reference to the active transaction in the first backup file
SELECT *
FROM
fn_dump_dblog (
NULL, NULL, N'DISK', 1, N'E:\Backup\dblog_log2.bak',
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)
where [operation] like '%INSERT%'
GO

--Read the transaction log backup 3.The third log backup contains any reference to the active transaction in the first backup file saying it was commited.
SELECT *
FROM
fn_dump_dblog (
NULL, NULL, N'DISK', 1, N'E:\Backup\dblog_log3.bak',
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)
where [operation] like '%INSERT%'
GO

--clean up
use master
go
drop database dblog