Wednesday, November 16, 2011

SQL Server CPU, Memory count query

SQL 2005
USE [master];
GO
select
    cpu_count
,    hyperthread_ratio
,    physical_memory_in_bytes / 1048576 as 'mem_MB'
,    virtual_memory_in_bytes / 1048576 as 'virtual_mem_MB'
,    max_workers_count
,    os_error_mode
,    os_priority_class
from
    sys.dm_os_sys_info

xp_cmdshell Copy file path space error/issue on SQL Server

Using  xp_cmdshell , copy file on SQL Server have an folder name issue.

Example:
Wrong one:  xp_cmdshell 'copy  C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\mssqlsystemresource.mdf  d:\temp'

Error: The above command will fail to execute it due to the folder naming convention and the spaces between folder names.(ex.Program Files  and Microsoft SQL Server). So the solution for this is just put full path on  double quote(") start and end.Then it works fine.


Correct one:  xp_cmdshell 'copy "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\mssqlsystemresource.mdf" d:\temp'



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

Monday, August 29, 2011

PowerShell 2008

http://www.sqlservergeeks.com/blogs/piyush.bajaj.2007/sql-server-bi/113/sql-server-powershell-in-sql-server-2008-part-i

http://www.sqlservergeeks.com/blogs/piyush.bajaj.2007/sql-server-bi/120/sql-server-powershell-in-sql-server-2008-part-ii

http://www.sqlservergeeks.com/blogs/piyush.bajaj.2007/sql-server-bi/121/sql-server-powershell-in-sql-server-2008-part-iii


http://www.sqlservergeeks.com/blogs/sarab/sql-server-bi/214/how-to-enable-powershell-feature-in-windows-server-2008-r2

Monday, August 15, 2011

Installation steps for SQL Server 2008 R2

http://www.sqlservergeeks.com/articles/sql-server-bi/32/installation-guide-for-sql-server-2008-r2

Thursday, August 11, 2011

One-click Deploy SSIS Package from Visual Studio to SQLServer

http://blog.boxedbits.com/archives/25

SSIS Packages deployment

http://decipherinfosys.wordpress.com/2008/09/16/deploying-ssis-packages-in-sql-server-2005/

There are two options available for us to deploy the SSIS Packages.

a) File System Deployment:

As the name suggests, this option allows us to deploy the SSIS Project to the File System i.e. a physical location on the hard disk on the target server. If this option is chosen, we have to back up the SSIS Packages manually every night for any disaster recovery.

b) Sql Server Deployment:
This option allows us to deploy the SSIS project to the SQL Server i.e. on the MSDB database. Since the msdb is usually backed up every night, this method of deployment saves us some extra work of backing up the SSIS Packages manually if deployed using the file system.

SSIS Packages execution  options:


Syntax:   dtexec /option [value] [/option [value]]...
1. Command prompt
dtexec /f "c:\pkgOne.dtsx"  --File system
dtexec /sq pkgOne /ser productionServer -- SQL Server
 

2. Using the xp_cmdshell extended stored procedure by using the dtexec command line utility
EXEC xp_cmdshell 'dtexec /f "C:\UpsertData.dtsx"'

3.Using BIDS

Could not save the package "D:\Test\Deployment1\Package2.dtsx" to SQL Server "(local)".

Error while deplying the SSIS Packag: Could not save the package "D:\Test\Deployment1\Package2.dtsx" to SQL Server "(local)".
The SaveToSQLServer method has encountered OLE DB error code 0x80004005 (Login timeout expired).  The SQL statement that was issued has failed.

Solutuon:  http://msdn.microsoft.com/en-us/library/ms137789.aspx

Modify the MsDtsSrvr.ini.xml, file ( ServerName\InstanceName)
,which is located in the folder, %ProgramFiles%\Microsoft SQL Server\100\DTS\Binn.

Tuesday, June 21, 2011

SQL Server Performance Tuning

http://www.brentozar.com/sql-server-training-videos/

http://vimeo.com/16316098

Saturday, May 14, 2011

xp_cmdshell exec permission for non-sysadmin user

(1) Enable the xp_cmdshell procedure
Enable xp_cmdshell by using sp_configure or "Surface Area Configuration"
USE MASTER
GO
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE
GO
(2) Create a login 'Domain\TestUser' for the non-sysadmin user that has public access to the master database
(3) Grant EXEC permission on the xp_cmdshell stored procedure
     GRANT EXECUTE ON xp_cmdshell TO [Domain\TestUser]
(4) Create a proxy account that xp_cmdshell will be run under using sp_xp_cmdshell_proxy_account
      EXEC sp_xp_cmdshell_proxy_account '
[Domain\TestUser]','pwd'
Note: pwd means windows password for
[Domain\TestUser] account id on the box.
(5).Grant control server permission to user
USE master;
GRANT CONTROL SERVER TO
[Domain\TestUser]
GO

INSERT Data from XL Sheet to SQL Server Database Table

--1.XL sheet to Table
Insert into Table2 Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;Database=D:\test.xls;HDR=YES',
    'SELECT * FROM [Sheet1$]')
--2.Select data from xl and display in SQL Server Management
Select * from openrowset('microsoft.jet.oledb.4.0',
'Excel 8.0;database=D:\Test.xls',
'select * from [Sheet1$]')
--3.Insert data from SQL Table to XL sheet.
Insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;Database=D:\Test2.xls;',
    'SELECT * FROM [Sheet1$]') select * from Table2

Saturday, April 9, 2011

Backup & Restore permissions

1.Backup

--User/Batch id should be db_backupoperator role or 'SysAdmin' role

2.Restore
--User/Batch id should be dbcreator role or 'SysAdmin' role
--Or If User/Batch id have administror privileges on windows server level,,then database can restore without dbcreator role or 'SysAdmin' role.

Database Backup & Restore using Batch scripts

--Backup batch file script
SQLCMD -S SQLInstance1 -E -i "c:\test\Backupquery.sql"

--Backupquery.sql script
--Get Date format to YYYYMMDDHHMM

Declare @BkpFiledate varchar(255)
set @BkpFiledate=CONVERT(VARCHAR(20), GETDATE(), 112) +CONVERT(VARCHAR(20), DATEPART(hour, GETDATE()))+CONVERT(VARCHAR(20), DATEPART(minute, GETDATE()), 112)

--print 'BkpFiledate'+@BkpFiledate
DECLARE @BkpFilePath NVARCHAR(255), @BkpFileName NVARCHAR(255), @BkpFullPath NVARCHAR(255)
SET @BkpFilePath = 'C:\Test\'

print '['+convert(varchar(25),Getdate())+'] '+'Start BACKUP DATABASE TestDB ';
SET @BkpFileName = 'Backup\TestDB_backup_'+@BkpFiledate+'.BAK'
SET @BkpFullPath = @bkpFilePath + @bkpFileName
BACKUP DATABASE TESTDB TO DISK = @BkpFullPath WITH INIT, STATS=10
print '['+convert(varchar(25),Getdate())+'] '+'End BACKUP DATABASE TestDB ';
--------------------------------------------------------------------------------------

--Restore batch file script
set bkpLocation=C:\Test\Backup
set Data1Location=C:\Test\Data1
set Log1Location=C:\Test\LOG1

set DBName=TestDB2
REM Finds the FOR loop for recent backup file on %BackupLocation%\%DBName%\ location
FOR /F %%I IN ('DIR "%BkpLocation%\%DBName%\*.BAK" /B /O:D') DO set RecentBackupFile=%%I

SQLCMD -S SQLInstance2 -E -d master -Q "RESTORE DATABASE %DBName% FROM DISK=N'%BkpLocation%\%DBName%\%RecentBackupFile%' WITH FILE = 1,
MOVE N'%DBName%_Data' TO N'%Data1Location%\%DBName%.mdf',
MOVE N'%DBName%_Log' TO N'%Log1Location%\%DBName%_1.ldf',
REPLACE,STATS=10"

Sunday, February 6, 2011

Commandline exec SQL Agent job

1.SQLCMD -SBKCOM\INSTANCE1 -E -Q"EXEC msdb.dbo.sp_start_job @job_name ='TEST'"

Permissions--On MSDB Database "SqlAgentOperator" role required to run the sql agent jobs the through command line.

2.isql /U username -n -E /d "database name" -Q "stored procedure name" /S servername -oC:\returncode.txt

3.osql -E -d MSDB -Q "sp_start_job 'MySQLJob'"
4.CREATE PROCEDURE sp_RunJob
AS
BEGIN
EXEC msdb..sp_start_job @job_name = 'The job name'
END

Saturday, January 29, 2011

SSIS Pacakge Execute

1. http://msdn.microsoft.com/en-us/library/ms162810.aspx
2. http://www.codeproject.com/KB/database/Call_SSIS_from_SP.aspx

--Using Stored procedure
http://geekswithblogs.net/stun/archive/2009/03/05/mapping-stored-procedure-parameters-in-ssis-ole-db-source-editor.aspx
http://geekswithblogs.net/stun/archive/2010/02/24/execute-ssis-package-from-stored-procedure-with-parameters-using-dtexec-utility.aspx


Syntax

dtexec /option [value] [/option [value]]...

Using dtexec from the xp_cmdshell
You can run dtexec from the xp_cmdshell prompt. The following example shows how to run a package called

UpsertData.dtsx and ignore the return code:
EXEC xp_cmdshell 'dtexec /f "C:\UpsertData.dtsx"'

The following example shows how to run the same package and capture the return code:
DECLARE @returncode int
EXEC @returncode = xp_cmdshell 'dtexec /f "C:\UpsertData.dtsx"'

SSIS Pacakge - Exit codes returned from dtexec utility

Value        Description

0           The package executed successfully.

1           The package failed.

3          The package was canceled by the user.

4          The utility was unable to locate the requested package. The package could not be found.

5          The utility was unable to load the requested package. The package could not be loaded.

6          The utility encountered an internal error of syntactic or semantic errors in the command line.

ASCMD Command-line Utility -- SSAS