Pages

Showing posts with label SQL Script. Show all posts
Showing posts with label SQL Script. Show all posts

Tuesday, November 1, 2011

How to find column name in all objects in a specific Database

SELECT * FROM INFORMATION_SCHEMA.[COLUMNS] c
WHERE c.COLUMN_NAME ='@COLUMN-NAME'

See who is connected to SQL Database

SELECT SPID,STATUS,PROGRAM_NAME,LOGINAME=RTRIM(LOGINAME),
HOSTNAME,CMD
FROM  MASTER.DBO.SYSPROCESSES
WHERE DB_NAME(DBID) = '<DATABASE NAME>' 

AND DBID != 0

Wednesday, June 29, 2011

Get Free Space for particular drive and send email

Below script gives you the free space for D drive. You can run this on any machine using SSMS.
The threshold of 300 can be changed as per your requirement

Use tempdb
Declare
@Free_space  as varchar(max),
@Body as varchar(255)

If OBJECT_ID('#Fixeddrives') is not null
BEGIN
 Drop Table #Fixeddrives

 Create Table #Fixeddrives
  (
   drives varchar(2),
   FreeSpace bigint
  )

 INSERT INTO #Fixeddrives
 Exec sys.xp_fixeddrives

 Select @Free_space =  FreeSpace/1024
 From #Fixeddrives
 Where drives='D' and (FreeSpace)/1024< = 300
 Drop Table #Fixeddrives
END

if @Free_space < 300
 BEGIN
  Select @Body = 'SERVER NAME - Total Free space on D drive is: ' + @Free_space + ' GB'
  EXEC msdb.dbo.sp_send_dbmail
  @profile_name = 'PROFILE NAME',
  @recipients ='TO EMAIL ADDRESS',
  @body = @Body,
  @subject = 'SERVER NAME - Low disk space',
  @Importance = 'HIGH'
 END

Tuesday, June 28, 2011

Get the information about the restored database(s)

Below script gives you the details (DB Name, Restored Name, Restore Data, Back up Start Date, Back up Finish date and Username) of all the restored databases. To get the details for specific database, just add the "where" clause with bckup.database_name=''

SELECT bckup.database_name AS 'DB Name' ,Restored_To_DBName AS 'Restored Name' ,Last_Date_Restored AS 'Restored Date' ,bckup.backup_start_date AS 'Backup Start Date' ,bckup.backup_finish_date AS 'Backup Finish Date' ,bckup.user_name AS 'User Name' FROM msdb..backupset bckup INNER JOIN (SELECT backup_set_id ,Restored_To_DBName ,Last_Date_Restored FROM msdb..restorehistory INNER JOIN ( SELECT rh.destination_database_name Restored_To_DBName , MAX(rh.restore_date) Last_Date_Restored FROM msdb..restorehistory rh GROUP BY rh.destination_database_name ) AS InnerRest ON destination_database_name = Restored_To_DBName AND restore_date = Last_Date_Restored ) AS RestData ON bckup.backup_set_id = RestData.backup_set_id

How to Get SSIS Package name from SSIS Store where a specific table is used

Get the SSIS Package name from SSIS Package store where a specific object is used

SELECT a.name,
       CONVERT(XML, CONVERT(VARBINARY(MAX), a.packagedata)) AS ssispackagexml
FROM   msdb.dbo.sysssispackages a (nolock)
WHERE  CAST(CONVERT(XML, CONVERT(VARBINARY(MAX), a.packagedata)) AS
            NVARCHAR(MAX)) LIKE
       '%objectname%'