給資料庫的每一個表添加相同的列,比如“建立時間”
如果每一個表都去修改,在上百個表的資料庫操作,將是一件繁瑣的工作。
更快捷的方法,利用Sql server的sp_msforeachtable枚舉每一個表的資訊,然後參數添加列的指令碼,問題就很簡單了。
運行T-sql
USE AdventureWorks;
EXEC sp_msforeachtable
'PRINT "ALTER TABLE ? ADD Date_Created DATETIME DEFAULT GETUTCDATE();"' ;
執行後會print一段指令碼:
代碼
ALTER TABLE [Production].[ProductProductPhoto] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Sales].[StoreContact] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Person].[Address] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Production].[ProductReview] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Production].[TransactionHistory] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Person].[AddressType] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Production].[ProductSubcategory] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [dbo].[AWBuildVersion] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Production].[TransactionHistoryArchive] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Purchasing].[ProductVendor] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Production].[BillOfMaterials] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Production].[UnitMeasure] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Purchasing].[Vendor] ADD Date_Created DATETIME DEFAULT GETUTCDATE();
ALTER TABLE [Purchasing].[PurchaseOrderDetail] ADD Date_Created DATETIME DEFAULT GETUTCDATE();