John McCormack Engineering Manager

Engineering management, SRE and database administration

  • Posts
    • Hire me
      • Take a look at my Sessionize speaker’s profile
      • Let me solve your SQL Server problems – No longer available
    • Guides
    • cost-optimization
    • SQL Server
    • Training
    • Personal
    • Azure
    • T-SQL
    • AWS RDS
    • AWS SQL Server
  • Resume
  • Free Training
    • SQL Server on Amazon RDS (Free Course)
    • Free practice questions to help you pass DP-900
  • Cost Optimization
    • Azure IaaS SQL Backups – Stop burning money
    • Your Azure SQL Database and Managed Instance is too big
    • Turn the cloud off at bedtime to save 70%
    • Your Azure SQL Virtual Machine might be too big
    • Save money with Azure SQL DB serverless
    • Save up to 73% with reserved instances
    • Delete unused instances to save money in Azure
  • Personal
    • About

List all views

1st August 2014 By John McCormack Leave a Comment

This script is handy when you want to list all views on an instance or even find a specific view if you know the name but don’t know the database.

[sql]

— Written by John McCormack

— This script uses the unspported stored procedure sp_msforeachdb so there are more reliable ways of going about this task.
— It uses a table variable and inserts each row returned into the table variable
— The ? is the database – All databases will be

DECLARE @views table (DBName sysname, ViewName nvarchar(100))
INSERT INTO @views
EXEC sp_msforeachdb
‘USE ?
SELECT ”?”,name FROM sys.views
— where [name] = ”vw_CurrentDaySales”’
— Uncomment the where clause if you wanted to find a specific view

SELECT * FROM @views

[/sql]

Filed Under: T-SQL Tagged With: find views, sp_msforeachdb, sql server views

About John McCormack

John McCormack is an engineering manager at Docusign. He specialises in SRE and Database administration, and has extensive experience in all major public clouds.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

John McCormack · Copyright © 2026