CREATE FUNCTION [dbo].[fn_StripCharacters]
( @String NVARCHAR(MAX),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = STUFF(@String, PatIndex(@MatchExpression,@String), 1, '')
RETURN @String
END
SELECT dbo.fn_StripCharacters('a1$%s#22d&39#f4$', '^a-z') as OnlyAlphaChars
The sp_helpindex stored procedure gives useful information like name, description and key about the indexes on a table or view. However for retrieving detailed information, you should make use of the sys.indexes object catalog view.
Let us say you want to view the Index MetaData for the table Orders in the Northwind database.
Here’s the query for it:
SELECT
name,
type_desc,
is_unique,
allow_row_locks,
allow_page_locks,
is_disabled,
fill_factor
FROM sys.indexes
WHERE object_id = OBJECT_ID('Orders')
OUTPUT
sp_MSforeachdb iterates through each database in a SQL Server instance. Instead of using a cursor, this Undocumented Stored Procedure is of immense help when I need to run a command against all the databases in my local server. Here are some scenarios where the sp_MSforeachdb can be practically used for your day to day tasks:
- Print all the database names in a SQL Server Instance
- Print all the tables in all the databases of a SQL Server Instance
- The example shown above prints the tables for master, model, msdb and tempdb. If you want to eliminate these databases in the query use, do the following:
- Display the size of all databases in a SQL Server instance
- Determine all the physical names and attributes(size,growth,usage) of all databases in a SQL Server instance
- Change Owner of all databases to 'sa'
- Check the Logical and Physical integrity of all objects in the database
EXEC sp_MSforeachdb 'USE ?; PRINT DB_NAME()'
EXEC sp_MSforeachdb 'USE ? SELECT DB_NAME() + ''.'' + OBJECT_NAME(object_Id) FROM sys.tables'
EXEC sp_MSforeachdb '
USE ?
SELECT OBJECT_NAME(object_Id)
FROM sys.tables
where DB_NAME() NOT IN(''master'', ''model'', ''msdb'', ''tempdb'')
'
EXEC sp_MSforeachdb 'USE ?; EXEC sp_spaceused'
EXEC sp_MSforeachdb 'USE ? EXEC sp_helpfile;'
EXEC sp_MSforeachdb 'USE ?; EXEC sp_changedbowner ''sa'''
sp_MSforeachdb 'DBCC CHECKDB(?)'
Similarly you can backup all databases at one go or do a CHECKSUM using this useful procedure. There are many more ways to use it. Got a useful tip on sp_MSforeachdb? Share it using the comments below.
sp_MSforeachtable can be used to loop through all the tables in your databases. Here are some common usages of this useful stored procedure:
- Display the size of all tables in a database
- Display Number of Rows in all Tables in a database
- Rebuild all indexes of all tables in a database
- Disable all constraints of all tables in a database
- Disable all Triggers of all tables in a database
- Delete all data from all tables in your database
- To RESEED all table to 0, use this script
- Reclaim space from dropped variable-length columns in tables or indexed views
- Update Statistics of all Tables in a database
USE NORTHWIND
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
USE YOURDBNAME
EXEC sp_MSforeachtable 'SELECT ''?'', Count(*) as NumberOfRows FROM ?'
USE YOURDBNAME
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
Note: DBCC DBREINDEX has been deprecated in SQL 2005. Microsoft says "This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER INDEX instead."
USE YOURDBNAME
EXEC sp_MSforeachtable @command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL"
USE YOURDBNAME
EXEC sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'
-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
DELETE FROM ?
ELSE
TRUNCATE TABLE ?
'
GO
-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1
DBCC CHECKIDENT (''?'', RESEED, 0)
'
GO
The two tips shown above have been taken from http://blogs.officezealot.com/mauro/archive/2006/03/12/9402.aspx and http://www.sqljunkies.com/WebLog/roman/archive/2006/03/08/18620.aspx
USE YOURDBNAME
EXEC sp_MSforeachtable 'DBCC CLEANTABLE(0,''?'') WITH NO_INFOMSGS; ';
USE YOURDBNAME
EXEC sp_MSforeachtable 'UPDATE statistics ? WITH ALL'
Welcome to SQL Server Examples
The examples on this portal will provide you with the building blocks necessary to quickly resolve different TSQL coding issues. You can browse the examples by using the Examples link on the main menu. You can also download all the examples on this site by clicking HERE.
Also available on this site is a SQL Server monitoring tool known as the SQL Server DBA Dashboard tool. This tool provides both high level and detailed reports to help monitor your SQL Server instances. With this tool you can quickly identify the resources used by specific TSQL statements and stored procedures.
Categories
- Android (2)
- Backpacking (5)
- BBQ (3)
- Bikes (3)
- Boy Scouts (6)
- Breakfast (2)
- Cars (1)
- Chinese (2)
- CMD (8)
- eBooks (1)
- ELL (1)
- Family (13)
- Favorite Poetry (6)
- Flame (1)
- Fun (29)
- Instructables (4)
- Joannie (38)
- LDS Church (12)
- Men's Health Lists (2)
- Oracle (2)
- Pixar (1)
- PowerShell (4)
- Productivity (1)
- Projects (8)
- Recipe (46)
- Reign (2)
- RV (1)
- Scouting (7)
- Security (1)
- Shopping (7)
- Smoking (3)
- Softball (7)
- Sports (6)
- SQL Server (61)
- Stealth (1)
- Tools (58)
- Video (23)
- Visual Studio (5)
- Webelos (2)
Recent Posts
Archives
-
▼
2014
(2)
- ► 03/30 - 04/06 (1)
-
►
2013
(9)
- ► 11/24 - 12/01 (3)
- ► 10/06 - 10/13 (1)
- ► 09/08 - 09/15 (1)
- ► 08/18 - 08/25 (2)
- ► 06/09 - 06/16 (1)
- ► 04/28 - 05/05 (1)
-
►
2012
(12)
- ► 06/24 - 07/01 (1)
- ► 06/03 - 06/10 (3)
- ► 05/27 - 06/03 (1)
- ► 05/20 - 05/27 (2)
- ► 04/22 - 04/29 (1)
- ► 03/18 - 03/25 (3)
- ► 03/04 - 03/11 (1)
-
►
2011
(84)
- ► 12/11 - 12/18 (1)
- ► 10/30 - 11/06 (2)
- ► 10/16 - 10/23 (1)
- ► 10/09 - 10/16 (1)
- ► 09/04 - 09/11 (1)
- ► 08/28 - 09/04 (1)
- ► 07/10 - 07/17 (4)
- ► 07/03 - 07/10 (2)
- ► 06/26 - 07/03 (6)
- ► 06/12 - 06/19 (2)
- ► 05/29 - 06/05 (1)
- ► 04/17 - 04/24 (1)
- ► 04/10 - 04/17 (2)
- ► 04/03 - 04/10 (2)
- ► 03/13 - 03/20 (1)
- ► 03/06 - 03/13 (1)
- ► 02/27 - 03/06 (1)
- ► 02/20 - 02/27 (1)
- ► 02/13 - 02/20 (5)
- ► 02/06 - 02/13 (1)
- ► 01/30 - 02/06 (41)
- ► 01/23 - 01/30 (4)
- ► 01/09 - 01/16 (1)
- ► 01/02 - 01/09 (1)
-
►
2010
(56)
- ► 12/26 - 01/02 (1)
- ► 12/12 - 12/19 (4)
- ► 12/05 - 12/12 (2)
- ► 11/28 - 12/05 (1)
- ► 11/14 - 11/21 (6)
- ► 10/31 - 11/07 (4)
- ► 10/24 - 10/31 (4)
- ► 10/17 - 10/24 (1)
- ► 10/10 - 10/17 (2)
- ► 10/03 - 10/10 (4)
- ► 09/26 - 10/03 (3)
- ► 09/19 - 09/26 (1)
- ► 09/12 - 09/19 (9)
- ► 08/08 - 08/15 (1)
- ► 06/20 - 06/27 (1)
- ► 04/04 - 04/11 (1)
- ► 03/14 - 03/21 (1)
- ► 02/21 - 02/28 (3)
- ► 02/07 - 02/14 (3)
- ► 01/31 - 02/07 (2)
- ► 01/03 - 01/10 (2)
-
►
2009
(58)
- ► 12/20 - 12/27 (1)
- ► 09/20 - 09/27 (7)
- ► 09/06 - 09/13 (1)
- ► 07/26 - 08/02 (1)
- ► 07/12 - 07/19 (1)
- ► 06/14 - 06/21 (1)
- ► 06/07 - 06/14 (4)
- ► 05/31 - 06/07 (2)
- ► 05/24 - 05/31 (4)
- ► 05/17 - 05/24 (5)
- ► 05/10 - 05/17 (9)
- ► 05/03 - 05/10 (12)
- ► 04/26 - 05/03 (10)
Favorite Links
Favorite Tunes
Pronounced





of or pertaining to the period in which a fisherman must leave to go fishing. |
of or pertaining to applications in which the computer must respond as rapidly as required by the user or necessitated by the process being controlled. |