Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. List usage of any object in any DB on Server

List usage of any object in any DB on Server

Scheduled Pinned Locked Moved Database
databasecomsysadmintutorialquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OMalleyW
    wrote on last edited by
    #1

    Hello, I posted this over at sqlserverentral.com but have not had any reply's. If you are a member here is the link: Code[^] What this will do: Allows you to show the usage of any table, procedure, column, or view in any database on the server where the code is running. Example: exec spFindObjectUsage 'MyTableOfParts' This will return all the views and procedures that call out MyTableOfParts. Here is the code: CREATE PROCEDURE spFindObjectUsage @ObjectToFind NVARCHAR(100) = '' ,@ResultMessage VARCHAR(200) = '' OUTPUT AS DECLARE @ReturnCode INT ,@StringToExecute NVARCHAR(1500) ,@DBToProcess INT ,@ServerName VARCHAR(200) ,@TableWithServer VARCHAR(200) ,@NameOfDatabase VARCHAR(50) -- DECLARE @ServerDatabaseTables TABLE (TempTblID INT NOT NULL IDENTITY(1,1) ,DBName VARCHAR(100) NOT NULL DEFAULT '' ,Processed BIT NOT NULL DEFAULT 0) -- CREATE TABLE #ServerDatabaseObjectUsage (UsageID INT NOT NULL IDENTITY(1,1) ,DBName VARCHAR(100) NOT NULL DEFAULT '' ,ObjectUsedIn VARCHAR(200) NOT NULL DEFAULT '' ,TypeOfObject VARCHAR(50) NOT NULL DEFAULT '' ,IsColumnOfTable BIT NOT NULL DEFAULT 0) -- -- first get all the databases on the current server -- INSERT INTO @ServerDatabaseTables (DBName) SELECT name FROM master.dbo.sysdatabases -- SET @TableWithServer = '' SET @NameOfDatabase = '' -- SET NOCOUNT ON -- each database has it's own listing of System Objects so inorder to get -- a correct listing we will need to go through every database. -- the only way I know to do this is using sqlexec. -- I know it is not the best way but we will need the ability to dynamically -- tell the query what system tables to use. Ex: master.dbo.systemobjects or production.dbo.systemobjects...ect WHILE EXISTS(SELECT * FROM @ServerDatabaseTables WHERE Processed = 0) BEGIN SET @DBToProcess = (SELECT MIN(TempTblID) FROM @ServerDatabaseTables WHERE Processed = 0) -- SELECT @ServerName = DBName + '.dbo.' ,@NameOfDatabase = DBName FROM @ServerDatabaseTables WHERE TempTblID = @DBToProcess -- SET @StringToExecute = 'INSERT INTO #ServerDatabaseObjectUsage ' + '(DBName' + ',ObjectUsedIn' + ',TypeOfObject' + ',IsColumnOfTable) ' + 'SELECT DISTINCT ' + char(39)+@NameOfDatabase+char(39)+

    O 1 Reply Last reply
    0
    • O OMalleyW

      Hello, I posted this over at sqlserverentral.com but have not had any reply's. If you are a member here is the link: Code[^] What this will do: Allows you to show the usage of any table, procedure, column, or view in any database on the server where the code is running. Example: exec spFindObjectUsage 'MyTableOfParts' This will return all the views and procedures that call out MyTableOfParts. Here is the code: CREATE PROCEDURE spFindObjectUsage @ObjectToFind NVARCHAR(100) = '' ,@ResultMessage VARCHAR(200) = '' OUTPUT AS DECLARE @ReturnCode INT ,@StringToExecute NVARCHAR(1500) ,@DBToProcess INT ,@ServerName VARCHAR(200) ,@TableWithServer VARCHAR(200) ,@NameOfDatabase VARCHAR(50) -- DECLARE @ServerDatabaseTables TABLE (TempTblID INT NOT NULL IDENTITY(1,1) ,DBName VARCHAR(100) NOT NULL DEFAULT '' ,Processed BIT NOT NULL DEFAULT 0) -- CREATE TABLE #ServerDatabaseObjectUsage (UsageID INT NOT NULL IDENTITY(1,1) ,DBName VARCHAR(100) NOT NULL DEFAULT '' ,ObjectUsedIn VARCHAR(200) NOT NULL DEFAULT '' ,TypeOfObject VARCHAR(50) NOT NULL DEFAULT '' ,IsColumnOfTable BIT NOT NULL DEFAULT 0) -- -- first get all the databases on the current server -- INSERT INTO @ServerDatabaseTables (DBName) SELECT name FROM master.dbo.sysdatabases -- SET @TableWithServer = '' SET @NameOfDatabase = '' -- SET NOCOUNT ON -- each database has it's own listing of System Objects so inorder to get -- a correct listing we will need to go through every database. -- the only way I know to do this is using sqlexec. -- I know it is not the best way but we will need the ability to dynamically -- tell the query what system tables to use. Ex: master.dbo.systemobjects or production.dbo.systemobjects...ect WHILE EXISTS(SELECT * FROM @ServerDatabaseTables WHERE Processed = 0) BEGIN SET @DBToProcess = (SELECT MIN(TempTblID) FROM @ServerDatabaseTables WHERE Processed = 0) -- SELECT @ServerName = DBName + '.dbo.' ,@NameOfDatabase = DBName FROM @ServerDatabaseTables WHERE TempTblID = @DBToProcess -- SET @StringToExecute = 'INSERT INTO #ServerDatabaseObjectUsage ' + '(DBName' + ',ObjectUsedIn' + ',TypeOfObject' + ',IsColumnOfTable) ' + 'SELECT DISTINCT ' + char(39)+@NameOfDatabase+char(39)+

      O Offline
      O Offline
      OMalleyW
      wrote on last edited by
      #2

      Would you be so kind as to put your 2 cents in on this code? Thanks, Will I hate users. Not all of them, just the ones who talk.CP member: Al Einstien

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups