-- I end up using this one a lot. You can add it into a JOIN to filter out tables that are empty.
SELECT O.object_id
, SCHEMA_NAME(O.schema_id) [Schema]
, O.name [Table]
, SUM(P.Rows) [RowCount]
, O.modify_date
, O.create_date
FROM sys.objects O
JOIN sys.partitions P
ON O.object_id = P.object_id
WHERE O.type = 'U'
AND O.is_ms_shipped = 0x0
AND index_id < 2 -- 0:Heap, 1:Clustered
GROUP BY O.object_id, O.name, O.create_date, SCHEMA_NAME(O.schema_id)
, O.modify_date
HAVING SUM(P.Rows) > 0
-- For example:
DECLARE @VAR VARCHAR(100)
SET @VAR = 'Descp' -- replace the value between the single quotes with your search param
SELECT T.name [TABLE], C.name [Column], O.[RowCount]
FROM sys.tables T
JOIN sys.columns C
ON C.object_id = T.object_id
JOIN (
SELECT O.object_id
, O.name [Table]
, SUM(P.Rows) [RowCount]
, O.modify_date
, O.create_date
FROM sys.objects O
JOIN sys.partitions P
ON O.object_id = P.object_id
WHERE O.type = 'U'
AND O.is_ms_shipped = 0x0
AND index_id < 2
GROUP BY O.object_id, O.name, O.create_date, SCHEMA_NAME(O.schema_id), O.modify_date
HAVING SUM(P.Rows) > 0) O
ON O.object_id = T.object_id
WHERE C.name LIKE '%' + @VAR + '%'
Monday, July 2, 2018
Thursday, June 21, 2018
Delete Older Backup History from msdb Database
-- Gets the oldest backup date in the backupset table
SELECT MIN(backup_finish_date)
FROM msdb.dbo.backupset
-- Gets the newest backup date in the backupset table
SELECT MAX(backup_finish_date)
FROM msdb.dbo.backupset
-- Purges the backupset table of all records BEFORE the one below in quotes
USE msdb;
GO
EXEC sp_delete_backuphistory '6/1/18';
Friday, March 2, 2018
List all Stored Procedures on a SQL Server that get Executed
-- =============================================
-- Author: K Griffith
-- Create date: 09 Feb 2015
-- Description: Returns a listing of all SPs on the server that get used
-- Modify date: 02 Mar 2018
--=============================================
SELECT st.dbid [DB_ID]
, DB_NAME(st.dbid) [Database]
, OBJECT_SCHEMA_NAME(st.objectid,dbid) [Schema]
, OBJECT_NAME(st.objectid,dbid) [StoredProcedure]
, MAX(cp.usecounts) [ExecutionCount]
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
WHERE DB_NAME(st.dbid) IS NOT NULL
AND cp.objtype = 'proc'
AND st.dbid = 7
GROUP BY st.dbid
, DB_NAME(st.dbid)
, OBJECT_SCHEMA_NAME(objectid,st.dbid)
, OBJECT_NAME(objectid,st.dbid)
--, cp.plan_handle
ORDER BY [Database]
Friday, February 23, 2018
Splitting a Full Name Apart When It Has Been Stuffed into a Single Field
-- In case the need arises to split a comma-delimited name - for example, where the last and first name have been stuffed into the same field such as 'Lastname, Firstname'
SELECT [UserName]
, RTRIM(LTRIM(LEFT([FieldName], CHARINDEX(',', [FieldName]) -1))) [LastName]
, RTRIM(LTRIM(STUFF([FieldName], 1, CHARINDEX(',', [FieldName]), ''))) [FirstName]
FROM [DatabaseName].[dbo].[TableName]
Subscribe to:
Posts (Atom)