home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Database / DBMaint / data1.cab / Program_Files / remaint.SQL < prev    next >
Encoding:
Text File  |  1999-04-06  |  3.3 KB  |  104 lines

  1. /* Microsoft SQL Server - Scripting            */
  2. /* Server: Nallepuh / FileChk ReMAINT            */
  3. /* Database: StrsArchive/Mik1998                */
  4. /* Creation Date 10/7/98 1:16:16 PM             */
  5.  
  6. /****** Object:  Stored Procedure dbo.sp_filechk    Script Date: 1998-10-07 13:16:16 ******/
  7. if exists (select * from sysobjects where id = object_id('dbo.sp_filechk') and sysstat & 0xf = 4)
  8.     drop procedure dbo.sp_filechk
  9. GO
  10. /****** Object:  Stored Procedure dbo.remaint    Script Date: 1998-10-07 13:16:16 ******/
  11. if exists (select * from sysobjects where id = object_id('dbo.remaint') and sysstat & 0xf = 4)
  12.     drop procedure dbo.remaint
  13. GO
  14.  
  15. /****** Object:  Stored Procedure dbo.sp_filechk    Script Date: 1998-10-07 13:16:16 ******/
  16. CREATE PROCEDURE sp_filechk 
  17. WITH ENCRYPTION
  18. AS
  19. UPDATE object 
  20. set filechk = 1
  21. where filechk = 0
  22. -- Batch (make stored procedure of it) to check if files exists
  23. SET NOCOUNT ON
  24. -- Table to hold all distinct path names
  25. CREATE TABLE #filepath (path VARCHAR(255), is_there tinyint NULL)
  26.  
  27. -- Populate the table with the distinct path names
  28. INSERT #filepath (path)
  29. SELECT DISTINCT SUBSTRING(object_file, 1, DATALENGTH(object_file) - PATINDEX('%\%', REVERSE(object_file))) FROM object WHERE arch_id IS NULL
  30.  
  31. -- Table to hold all filenames from the paths
  32. CREATE TABLE #files (filename VARCHAR(255))
  33. --CREATE CLUSTERED INDEX idx_files ON files (filename) WITH ALLOW_DUP_ROW
  34. --truncate table files
  35.  
  36. -- The folllowing cursor performs DIR for all paths and inserts the result into #files
  37. DECLARE path_cur CURSOR FOR
  38.  SELECT path FROM #filepath
  39. OPEN path_cur
  40.  
  41. DECLARE @path VARCHAR(255), @err INT, @exc VARCHAR(255)
  42. FETCH NEXT FROM path_cur INTO @path
  43. WHILE @@FETCH_STATUS = 0 --While everything is OK
  44. BEGIN
  45.  SELECT @exc = "master..xp_cmdshell 'DIR " + @path + " /b'"
  46.  INSERT #files
  47.  EXEC(@exc)
  48.  FETCH NEXT FROM path_cur INTO @path
  49. END
  50.  
  51. CLOSE path_cur
  52. DEALLOCATE path_cur
  53.  
  54. /* CHECK IF ACCESS IS  DENIED ON FILESYSTEM/MIK */
  55. IF (select COUNT(*) from #files WHERE FILENAME like 'Access is denied%') >0
  56.        BEGIN 
  57.     raiserror('Error in Filecheck, Access is Denied!  Killing the SPID now.' ,22,127) with log
  58.     END
  59. ELSE 
  60.   BEGIN
  61. CREATE TABLE #object (object_id INT)
  62.  
  63. -- Populate the table with the distinct path names
  64. INSERT #object (object_id)
  65. SELECT object_id
  66. FROM object 
  67. WHERE Not exists (select * from #files where REVERSE(SUBSTRING(SUBSTRING(REVERSE(object_file), 2, 255), 1, PATINDEX('%\%', REVERSE(object_file)) -2)) = filename ) 
  68. AND arch_id IS NULL
  69.  
  70. UPDATE object
  71.  SET filechk = 0
  72. --where filechk = 0
  73. from object o, #object t
  74. Where o.object_id = t.object_id
  75.  
  76. END
  77. DECLARE @i_missing_files INT         -- Number of missing files
  78. DECLARE @i_err_lvl INT               -- Severity level that we shall pass to RAISERROR
  79. SELECT @i_missing_files = (SELECT COUNT(*) FROM object WHERE filechk = 0)
  80.  
  81. --Set severity level (<15 is info, 15 is warning, >15 is error)
  82. IF @i_missing_files = 0
  83.  SELECT @i_err_lvl = 11 ELSE SELECT @i_err_lvl = 16
  84.  
  85. --Generate log entry (this can be IF'ed and modified to desire)
  86. --RAISERROR ("Det saknas %d fil(er) i filsystemet.", @i_err_lvl, 1, @i_missing_files)
  87. RAISERROR ("Number of files missing %d.", @i_err_lvl, 1, @i_missing_files)
  88.  
  89. DROP TABLE #files
  90. DROP TABLE #filepath
  91. DROP TABLE #object
  92. GO
  93.  
  94.  
  95. CREATE PROC remaint 
  96. WITH ENCRYPTION
  97. AS
  98.  EXEC sp_checkident
  99.  EXEC sp_filechk
  100. GO
  101.  
  102.  
  103.  
  104.