/************************************ Chapter 13 Scripts For MCITP SQL Server 2005 Database Administration All-in-One Exam Guide By Darril Gibson The scripts available here are the same ones used in each of the chapters. Ideally, you would type in the script and learn from any errors you might make. Occasionally, however, you may want a copy of the script to paste into the query window or into a batch file. If so, feel free to use these script files. I strongly encourage you not to rely on these pre-typed scripts, but instead to get used to typing the T-SQL yourself. The more you do it (even if you’re just typing what you see in the book), the more comfortable you’ll get with T-SQL. Some scripts in the book are intended to be run from the command line. They've been included where appropriate so that you can copy and paste them into Notepad and save them as a batch file (.bat). You can then run the batch file from the command prompt. *************************************/ Rem this can be pasted into a batch file Net Send administrator “MCITP Success” /************************************/ EXEC sp_addmessage 50100, 16, 'Users cannot be deleted from the database', 'us_english','True','Replace' /************************************/ RAISERROR (50100, 16,1) /************************************/ RAISERROR (50100, 16,1) WITH LOG /************************************/ EXEC sp_addmessage 50101, 16, 'Part number %d needs to be reordered.', 'us_english','true','replace' /************************************/ USE AdventureWorks; GO RAISERROR (50101, 16,1, 55); /************************************/ BACKUP LOG AdventureWorks TO DISK = 'C:\AW_Log.bak' /************************************/ USE AdventureWorks; GO DECLARE @i int SET @i = 1 WHILE @i < 1000 BEGIN SET @i = @i + 1 INSERT INTO Person.Contact (FirstName, LastName,PasswordHash, PasswordSalt) VALUES ('Mickey', 'Mouse', 'P@ssw0rd','P@$$S@lt') END /************************************/ DELETE FROM Person.Contact WHERE FirstName = 'Mickey' AND LastName = 'Mouse' /************************************/ USE AdventureWorks; GO SELECT * FROM Person.Contact /************************************/ USE AdventureWorks; GO SELECT * FROM Person.Contact /************************************/ USE MASTER; GO SELECT * FROM sys.dm_db_index_usage_stats /************************************/ DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR); /************************************/ SELECT * FROM sys.dm_os_wait_stats ORDER BY Wait_time_ms DESC /************************************/ DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR); /************************************/ SELECT * FROM sys.dm_os_wait_stats ORDER BY Wait_time_ms DESC /************************************/ SELECT * FROM sys.dm_os_sys_info /************************************/ SELECT * FROM sys.dm_db_index_physical_stats (DB_ID ('AdventureWorks'), NULL, NULL, NULL, NULL) WHERE avg_fragmentation_in_percent > 10 ORDER BY avg_fragmentation_in_percent DESC /************************************/ SELECT * FROM sys.objects WHERE object_id = 1435152158 /************************************/ SELECT name, index_id, index_type_desc, avg_fragmentation_in_percent, page_count FROM sys.dm_db_index_physical_stats (DB_ID ('AdventureWorks'), NULL, NULL, NULL, NULL) AS S JOIN sys.objects AS O ON S.object_ID = O.object_id WHERE avg_fragmentation_in_percent > 10 ORDER BY avg_fragmentation_in_percent DESC /************************************/ USE AdventureWorks; GO SELECT * from sys.dm_db_index_usage_stats WHERE database_id = DB_ID ('AdventureWorks'); /************************************/ USE AdventureWorks; GO SELECT * from sys.dm_db_index_usage_stats WHERE database_id = DB_ID ('AdventureWorks'); /************************************/ USE AdventureWorks; GO SELECT * FROM Person.Contact; /************************************/ SELECT * from sys.dm_db_index_usage_stats WHERE database_id = DB_ID ('AdventureWorks');