Emptying data for all tables in SQL Server View sourceprint?1 sp_msforeachtable @command1 = ' Delete from? ' View sourceprint?01 sp_msforeachtable How to use 02 03 1) Description The 04 system stored procedures sp_msforeachtable and sp_msforeachdb are two unlisted stored procedures provided by Microsoft , starting with MS SQL 6.5. 05 is stored in the master database of SQL Server . 06 07 2) Parameter description : @command1 nvarchar (2000),--The first SQL command to run @replacechar nchar (1) = N '? ',--the specified placeholder symbol @command2 nvarchar (a) = null,--The second SQL command to run One @command3 nvarchar (a) = null,--third run SQL instruction @whereand nvarchar (+) = null,--optional condition to select Table @precommand nvarchar (a) = null, the action before the instruction is executed (similar to the action before the trigger of the control ) @postcommand nvarchar = null-the action after the instruction is executed (similar to the trigger action of the control ) 15 16 3) Example 17--Details of each table in the statistical database sp_msforeachtable exec @command1 = "sp_spaceused '?" 19--Get the number of records and the capacity of each table : EXEC sp_msforeachtable @command1 = "print '? '", @command2 = "sp_spaceused '? '", @command3 = "SELECT count (*) from?" 23--Get all the storage space of the database : EXEC sp_msforeachdb @command1 = "print '? '", @command2 = "sp_spaceused" 26-Check all the databases EXEC sp_msforeachdb @command1 = "print '? '", @command2 = "DBCC CHECKDB (?) " 29--Update statistics for all tables in the pubs database that already start with T : EXEC sp_msforeachtable @whereand = "and name like ' t% '", @replacechar = ' * ', @precommand = "print ' Updating Statistics ..... ' Print '", @command1 = "print ' * ' UPDATE STATISTICS *", @postcommand = "Print" print ' Complete Update statistics! ' " 35--delete data from all tables in the current database Sp_msforeachtable @command1 = ' Delete from? ' PNS sp_msforeachtable @command1 = "TRUNCATE TABLE?" 38 39 4) use of parameter @whereand The @whereand parameter plays the role of instruction condition restriction in the stored procedure , the specific wording is as follows : In a @whereend, you can write @whereand = ' and O.name in (' Table1 ', ' Table2 ',.......) ' 42 Example : I want to update the value of note column null in Table1/table2/table3 Sp_msforeachtable @command1 = ' Update? Set note= ' where NOTE is NULL ', @whereand = ' and O.name in (' Table1 ', ' Table2 ', ' Table3 ') ' 44 45 5) "?" The special use of stored procedures has created these two powerful stored procedures 46 here "?" is equivalent to the role of a wildcard in DOS commands and when we search for files under Windows. print ' Prints all tables that need to be emptied : ' exec sp_msforeachtable @command1 = "print '? '", @whereand = ' and (O.[name] like "yds_%" or o.[name] like ' fks_% ') ' print '---------------------------------------------------------------------' print ' Print table with no foreign KEY constraint : ' exec sp_msforeachtable @command1 = " print '? ', @whereand = ' and ObjectProperty (o.id, ' tablehasforeignref ') =0 and xtype= ' U ' and (O.[name] like ' yds_% ' or o.[name] like ' sgcb_% ' or o.[name] like ' ydzy_% ' or o.[name] ' --empty table with no foreign KEY constraint document_% exec sp_msforeachtable @command1 =" truncate table? ", @whereand = ' and ObjectProperty (O.id, ' Tablehasforeignref ') =0 and xtype= ' U ' and (O.[name] like "yds_%" or o.[name] like "sgcb_%" or o.[name] like "ydzy_% ' or o.[name] like ' document_% ') ' print '-------------------------------------------------------------------- -' TRUNCATE TABLE chzl_xx TRUNCATE TABLE BookMarks print ' prints a table with foreign KEY constraints : ' exec sp_msforeachtable @command1 = "print '? '", @whereand = ' and ObjectProperty (o.id, ' tablehasforeignref ') =1 and xtype= ' U ' and (O.[name] like ' yds_% ' or O.[name] Li Ke ' sgcb_% ' or o.[name] like "ydzy_%" or o.[name] like ' document_% ') ' --Empty tables with foreign KEY constraints exec sp_msforeachtable @command1 = "Delete from?", @whereand = ' and ObjectProperty (o.id, ' tablehasforeignref ') =1 and xtype= ' U ' and (O.[name] like ' yds_% ' or O.[name] Li Ke ' sgcb_% ' or o.[name] like "ydzy_%" or o.[name] like ' document_% ') ' print '---------------------------------------------------------------------' print ' Reset maxids table : ' --Reset maxids table Update Maxids Set maxid=1
|