Friday, September 23, 2011

Find Dependent Objects

One new feature that SQL Server 2008 offers is more reliable dependency information. SQL Server 2005 offered a DMV (dynamic management view) called sys.sql_dependencies. It is now replaced by a more reliable sys.sql_expression_dependencies.
The following script will show all the procedures that reference a given table name, along with the columns the procedure references also. Please note, this does not take into account any dynamic SQL.
USE MYDatabase
GO
DECLARE @TableName varchar(100)
SET @TableName = 'mytable'
SELECT
 SourceSchema                  = OBJECT_SCHEMA_NAME(sed.referencing_id)
 ,SourceObject                 = OBJECT_NAME(sed.referencing_id)
 ,ReferencedDB                 = ISNULL(sre.referenced_database_name, DB_NAME())
 ,ReferencedSchema             = ISNULL(sre.referenced_schema_name,
OBJECT_SCHEMA_NAME(sed.referencing_id))
 ,ReferencedObject             = sre.referenced_entity_name
 ,ReferencedColumnID   = sre.referenced_minor_id
 ,ReferencedColumn             = sre.referenced_minor_name
FROM sys.sql_expression_dependencies sed
CROSS APPLY sys.dm_sql_referenced_entities(OBJECT_SCHEMA_NAME(sed.referencing_id)
+ '.' + OBJECT_NAME(sed.referencing_id), 'OBJECT') sre
WHERE sed.referenced_entity_name = @TableName
AND sre.referenced_entity_name = @TableName

Query which will show all the tables which is referenced by the table

  • The following example lists the database objects that depend on the Sales.Customer table in the AdventureWorks2008R2 database. Both the schema name and table name are specified.

USE AdventureWorks2008R2;
GO
EXEC sp_depends @objname = N'Sales.Customer' ;
 
  • The following example lists the database objects on which the trigger iWorkOrder depends. 
 
EXEC sp_depends @objname = N'AdventureWorks2008R2.Production.iWorkOrder' ;
 

Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database

SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
---- optional:
ORDER BY
1,2,3,4
WHERE PK.TABLE_NAME='something'WHERE FK.TABLE_NAME='something'
WHERE PK.TABLE_NAME IN ('one_thing', 'another')
WHERE FK.TABLE_NAME IN ('one_thing', 'another')




or
Rewritten the query to be slightly faster:


SELECT
CONSTRAINT_NAME = REF_CONST.CONSTRAINT_NAME,
TABLE_CATALOG = FK.TABLE_CATALOG,
TABLE_SCHEMA = FK.TABLE_SCHEMA,
TABLE_NAME = FK.TABLE_NAME,
COLUMN_NAME = FK_COLS.COLUMN_NAME,
REFERENCED_TABLE_CATALOG = PK.TABLE_CATALOG,
REFERENCED_TABLE_SCHEMA = PK.TABLE_SCHEMA,
REFERENCED_TABLE_NAME = PK.TABLE_NAME,
REFERENCED_COLUMN_NAME = PK_COLS.COLUMN_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS REF_CONST
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON REF_CONST.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG
AND REF_CONST.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA
AND REF_CONST.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
AND FK.CONSTRAINT_TYPE = ‘FOREIGN KEY’
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON REF_CONST.UNIQUE_CONSTRAINT_CATALOG = PK.CONSTRAINT_CATALOG
AND REF_CONST.UNIQUE_CONSTRAINT_SCHEMA = PK.CONSTRAINT_SCHEMA
AND REF_CONST.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
AND PK.CONSTRAINT_TYPE = ‘PRIMARY KEY’
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE FK_COLS ON REF_CONST.CONSTRAINT_NAME = FK_COLS.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE PK_COLS ON PK.CONSTRAINT_NAME = PK_COLS.CONSTRAINT_NAME

Monday, September 12, 2011

SSIS Package Permissions 2005 and 2008


Role Read action Write action
db_dtsadmin Enumerate own packages. Import packages.
or Enumerate all packages. Delete own packages.
sysadmin View own packages. Delete all packages.
  View all packages. Change own package roles.
  Execute own packages. Change all package roles.
  Execute all packages.  
  Export own packages.  
  Export all packages.  
  Execute all packages in SQL Server Agent.  
db_dtsltduser Enumerate own packages. Import packages.
Enumerate all packages. Delete own packages.
View own packages. Change own package roles.
Execute own packages.  
Export own packages.  
db_dtsoperator Enumerate all packages. None
View all packages.
Execute all packages.
Export all packages.
Execute all packages in SQL Server Agent.
Windows administrators View execution details of all running packages. Stop all currently running packages.

In SQL Server 2008 these 3 Database Roles are now named db_ssisltduser, db_ssisoperator, and db_ssisadmin
 MSDN Integration Services Roles (2008)
Role Read action Write action
db_ssisadmin Enumerate own packages. Import packages.
or Enumerate all packages. Delete own packages.
sysadmin View own packages. Delete all packages.
  View all packages. Change own package roles.
  Execute own packages. Change all package roles.
  Execute all packages. Bitmap
  Export own packages.  
  Export all packages.  
  Execute all packages in SQL Server Agent.  
db_ssisltduser Enumerate own packages. Import packages.
Enumerate all packages. Delete own packages.
View own packages. Change own package roles.
Execute own packages.  
Export own packages.  
db_ssisoperator Enumerate all packages. None
View all packages.
Execute all packages.
Export all packages.
Execute all packages in SQL Server Agent.
Windows administrators View execution details of all running packages. Stop all currently running packages

How to Schedule and Run a SSIS package ( DTS ) with SQL Agent Job for Non-SysAdmin user



In SQL Server 2005, you need to go through the security layer in order to run the job.

The logic is like this:
      The job executor account needs the roles of  SQLAgentUserRole, SQLAgentReaderRole,  SQLAgentOperatorRole
      The job needs to be run under Proxy account
      The job executor account is better to be used to create SSIS package and so you are sure the job executor account has the right to run this SSIS package.

The following steps can be followed to get the job done.
The work environment is MS SQL Server Management Studio and you log in as sa.

I. Create job executor account
Highlight Security->New Login, say to make login as devlogin, type your password, default database can be your target database.
User mapping: your target database
Msdb database: you make sure to include SQLAgentUserRole, SQLAgentReaderRole,  SQLAgentOperatorRole 
Also grant db_dtsoperator role on MSDB database to devlogin user.
Then click OK

II. Create SQL proxy account and associate proxy account with job executor account
Here is the code and run it the query window.

Use master
CREATE CREDENTIAL [MyCredential] WITH IDENTITY = 'yourdomain\myWindowAccount', secret = 'WindowLoginPassword'
Use msdb

EXEC dbo.sp_add_proxy  @proxy_name = 'MyProxy',  @enabled = 1,  @description = 'Maintenance tasks on catalog application.',  @credential_name = 'MyCredential' ;
GO
Sp_grant_login_to_proxy @login_name=' devlogin', @proxy_name='MyProxy'
Sp_grant_proxy_to_subsystem @proxy_name='MyProxy', @subsystem_name='SSIS'

III. Create SSIS package
In MS SQL Server Business Intelligence Development Studio, you use job executor account devlogin to create the SSIS package (DTS) and make sure you can execute this package in SQL Server Business Intelligence Development Studio. Compile/build this package.

IV. Create the job, schedule the job and run the job
In SQL Server Management Studio, highlight SQL Server Agent -> Start. Highlight Job ->New Job�, name it , myJob.
Under Steps, New Step, name it, Step1,
Type: SQL Server Integration Service Package
Run as: myProxy
Package source: File System
Browse to select your package file xxx.dtsx
Click Ok
Schedule your job and enable it