sp-msforeachtable

sp_MSforeachtable - parsing of dynamic sql

青春壹個敷衍的年華 提交于 2020-01-14 06:05:10
问题 I recently found an issue whereby I wanted to use the sp_MSforeachtable stored proc to select all tables with the word Transcode in the table name, and to run some SQL on those tables. I managed to write some code which worked, but not perfectly - for those tables which I'd hoped it would gracefully skip over (i.e. those which did not have transcode in the name) it instead threw errors due to certain expected columns (which only exist in the transcode tables) not existing on those tables. The

sp_MSforeachtable - parsing of dynamic sql

南笙酒味 提交于 2020-01-14 06:04:06
问题 I recently found an issue whereby I wanted to use the sp_MSforeachtable stored proc to select all tables with the word Transcode in the table name, and to run some SQL on those tables. I managed to write some code which worked, but not perfectly - for those tables which I'd hoped it would gracefully skip over (i.e. those which did not have transcode in the name) it instead threw errors due to certain expected columns (which only exist in the transcode tables) not existing on those tables. The

SQL Server: How to make server check all its check constraints?

时间秒杀一切 提交于 2019-12-28 05:16:08
问题 It seems that some scripts generated by Enterprise Manager* (or not, it doesn't matter) created check constraints WITH NOCHECK . Now when anyone modifies the table, SQL Server is stumbling across failed check constraints, and throwing errors. Can i make SQL go through all its check constraints, and check them? Running: sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all' only enables previously disabled check constraints, it doesn't actually check them. Footnotes * SQL Server 2000 回答1: DBCC

SQL Server: How to make server check all its check constraints?

给你一囗甜甜゛ 提交于 2019-12-28 05:16:07
问题 It seems that some scripts generated by Enterprise Manager* (or not, it doesn't matter) created check constraints WITH NOCHECK . Now when anyone modifies the table, SQL Server is stumbling across failed check constraints, and throwing errors. Can i make SQL go through all its check constraints, and check them? Running: sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all' only enables previously disabled check constraints, it doesn't actually check them. Footnotes * SQL Server 2000 回答1: DBCC

How to exclude tables from sp_msforeachtable

丶灬走出姿态 提交于 2019-12-18 18:40:00
问题 I know that sp_msforeachtable allows to perform queries on all tables. I have 100 tables and I want to perform the same query on 97 tables. I'm using this query: EXEC sp_MSForEachTable "DELETE FROM ?" Is it possible to exclude certain tables? 回答1: EXEC sp_MSforeachtable 'IF OBJECT_ID(''?'') NOT IN ( ISNULL(OBJECT_ID(''[dbo].[T1]''),0), ISNULL(OBJECT_ID(''[dbo].[T2]''),0) ) DELETE FROM ?' 回答2: Simplest syntax I came across to include or exclude schemas and tables: exec sp_MSforeachtable 'print

ALTER INDEX failed because of QUOTED_IDENTIFIER when running from sp_msForEachTable

天大地大妈咪最大 提交于 2019-12-09 09:28:47
问题 When I try to rebuild an index on a table: ALTER INDEX ALL ON [dbo].[Allocations] REBUILD that works fine. But when I call EXECUTE sp_msForEachTable 'ALTER INDEX ALL ON ? REBUILD' I reach the same same table, and it fails with: Msg 1934, Level 16, State 1, Line 2 ALTER INDEX failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query

sp_MSforeachtable order by

谁都会走 提交于 2019-12-08 06:45:28
问题 I am using sp_MSforeachtable to get a rowcount of specific tables in my database. I want these ordered by name. How do I add an ORDER BY clause to sp_MSforeachtable ? 回答1: from this link: http://web.archive.org/web/20080701045806/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html This will return correct counts, where methods using the meta data tables will only return estimates. create this procedure (slightly different than from

ALTER INDEX failed because of QUOTED_IDENTIFIER when running from sp_msForEachTable

放肆的年华 提交于 2019-12-03 12:18:16
When I try to rebuild an index on a table: ALTER INDEX ALL ON [dbo].[Allocations] REBUILD that works fine. But when I call EXECUTE sp_msForEachTable 'ALTER INDEX ALL ON ? REBUILD' I reach the same same table, and it fails with: Msg 1934, Level 16, State 1, Line 2 ALTER INDEX failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations. And to confirm that it's

Delete all views from Sql Server

假装没事ソ 提交于 2019-12-03 04:48:25
问题 By using this statement in SQL Server: EXEC sp_msforeachtable 'DROP TABLE ?' I know it's possible to delete all tables at once. Is there a similar statement for views? I tried this hoping to be lucky: EXEC sp_msforeachview 'DROP VIEW ?' but it doesn't work! 回答1: Here you have, no cursor needed: DECLARE @sql VARCHAR(MAX) = '' , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ; SELECT @sql = @sql + 'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(v.name) +';' + @crlf FROM sys.views v

Delete all views from Sql Server

跟風遠走 提交于 2019-12-02 18:03:28
By using this statement in SQL Server: EXEC sp_msforeachtable 'DROP TABLE ?' I know it's possible to delete all tables at once. Is there a similar statement for views? I tried this hoping to be lucky: EXEC sp_msforeachview 'DROP VIEW ?' but it doesn't work! Here you have, no cursor needed: DECLARE @sql VARCHAR(MAX) = '' , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ; SELECT @sql = @sql + 'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(v.name) +';' + @crlf FROM sys.views v PRINT @sql; EXEC(@sql); declare @SQL nvarchar(max) set @SQL = ( select 'drop view '+name+'; ' from sys.views