Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1772 lines
37 KiB

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddRestrictedCabAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  2. drop procedure [dbo].[AddRestrictedCabAccess]
  3. GO
  4. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddUserAndLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  5. drop procedure [dbo].[AddUserAndLevel]
  6. GO
  7. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddUserForApproval]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  8. drop procedure [dbo].[AddUserForApproval]
  9. GO
  10. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  11. drop procedure [dbo].[AddUserLevel]
  12. GO
  13. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ApproveUser]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  14. drop procedure [dbo].[ApproveUser]
  15. GO
  16. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckApprovalAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  17. drop procedure [dbo].[CheckApprovalAccess]
  18. GO
  19. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckRestrictedCabAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  20. drop procedure [dbo].[CheckRestrictedCabAccess]
  21. GO
  22. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckUserAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  23. drop procedure [dbo].[CheckUserAccess]
  24. GO
  25. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckUserAccessApprovals]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  26. drop procedure [dbo].[CheckUserAccessApprovals]
  27. GO
  28. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DefragIndexes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  29. drop procedure [dbo].[DefragIndexes]
  30. GO
  31. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUserID]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  32. drop procedure [dbo].[GetUserID]
  33. GO
  34. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAddUserAndLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  35. drop procedure [dbo].[OcaAddUserAndLevel]
  36. GO
  37. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAddUserForApproval]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  38. drop procedure [dbo].[OcaAddUserForApproval]
  39. GO
  40. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAddUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  41. drop procedure [dbo].[OcaAddUserLevel]
  42. GO
  43. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaApproveUser]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  44. drop procedure [dbo].[OcaApproveUser]
  45. GO
  46. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaCheckUserAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  47. drop procedure [dbo].[OcaCheckUserAccess]
  48. GO
  49. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaCheckUserAccessApprovals]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  50. drop procedure [dbo].[OcaCheckUserAccessApprovals]
  51. GO
  52. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaGetUserID]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  53. drop procedure [dbo].[OcaGetUserID]
  54. GO
  55. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaRemoveUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  56. drop procedure [dbo].[OcaRemoveUserLevel]
  57. GO
  58. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RemoveUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  59. drop procedure [dbo].[RemoveUserLevel]
  60. GO
  61. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestProc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  62. drop procedure [dbo].[TestProc]
  63. GO
  64. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[rolemember]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  65. drop procedure [dbo].[rolemember]
  66. GO
  67. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[updatestats]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  68. drop procedure [dbo].[updatestats]
  69. GO
  70. SET QUOTED_IDENTIFIER ON
  71. GO
  72. SET ANSI_NULLS OFF
  73. GO
  74. CREATE PROCEDURE AddRestrictedCabAccess
  75. @User varchar(50),
  76. @Domain varchar(50),
  77. @iDatabase int,
  78. @iBucket int,
  79. @szCabFilename varchar(100),
  80. @szCabPath varchar(255)
  81. As
  82. set nocount on
  83. -- local var to hold record id
  84. declare @target int
  85. declare @userfound int
  86. declare @userApproved int
  87. declare @CabAccessID int
  88. SELECT @userfound = UserID FROM AuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  89. if @userfound is NULL
  90. begin
  91. select @target = -1
  92. return @target
  93. end
  94. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @userfound
  95. if @userApproved is NULL
  96. begin
  97. select @target = -1
  98. return @target
  99. end
  100. SELECT @CabAccessID= CabAccessID from CabAccess WHERE UserID = @userfound and iBucket=@iBucket and iDatabase=@iDatabase
  101. and CabFilename=@szCabFilename and CabPath=@szCabPath
  102. if @CabAccessID is NULL
  103. begin
  104. INSERT INTO CabAccess (UserID,iDatabase, iBucket, CabFilename, CabPath, StatusID, DateRequested)
  105. VALUES (@userfound,@iDatabase, @iBucket, @szCabFilename, @szCabPath, 1, CURRENT_TIMESTAMP)
  106. end
  107. else
  108. begin
  109. UPDATE CabAccess SET StatusID=1, DateRequested=CURRENT_TIMESTAMP WHERE (CabAccessID=@CabAccessID)
  110. end
  111. SELECT @CabAccessID= CabAccessID from CabAccess WHERE UserID = @userfound and iBucket=@iBucket and iDatabase=@iDatabase
  112. and CabFilename=@szCabFilename and CabPath = @szCabPath
  113. if @CabAccessID is NULL
  114. begin
  115. select @target = -1
  116. return @target
  117. end
  118. select @target = @CabAccessID
  119. RETURN @target
  120. GO
  121. SET QUOTED_IDENTIFIER OFF
  122. GO
  123. SET ANSI_NULLS ON
  124. GO
  125. SET QUOTED_IDENTIFIER OFF
  126. GO
  127. SET ANSI_NULLS ON
  128. GO
  129. CREATE PROCEDURE AddUserAndLevel
  130. @User varchar(50),
  131. @Domain varchar(50),
  132. @CurrTime varchar(50),
  133. @Level int
  134. As
  135. set nocount on
  136. -- local var to hold record id
  137. declare @target int
  138. declare @levelDB int
  139. declare @userDB int
  140. declare @userID int
  141. -- do we already have this FileInCab?
  142. SELECT @userID = UserID FROM AuthorizedUsers WHERE
  143. UserAlias =@User AND UserDomain = @Domain
  144. if @userID is NULL
  145. begin -- not there, create new record
  146. INSERT INTO AuthorizedUsers (UserAlias, UserDomain, DateSignedDCP)
  147. VALUES (@User, @Domain,@CurrTime)
  148. SELECT @userID = UserID FROM AuthorizedUsers WHERE
  149. UserAlias =@User AND UserDomain = @Domain
  150. end
  151. else
  152. begin -- update record with new signing date/time
  153. UPDATE AuthorizedUsers SET DateSignedDCP = @CurrTime
  154. WHERE (UserID = @userID)
  155. end
  156. -- PRINT @userID
  157. -- we don't have the userid
  158. if @userID is NULL
  159. begin
  160. select @userID = 0
  161. return @userID
  162. end
  163. select @levelDB = AccessLevelID FROM AccessLevels WHERE
  164. AccessLevelID = @Level
  165. -- bad level passed in
  166. if @levelDB is NULL
  167. begin
  168. select @levelDB = 0
  169. return @levelDB
  170. end
  171. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  172. UserID = @userID and AccessLevelID = @Level
  173. if @target is NULL
  174. begin -- not there, create new record
  175. INSERT INTO UserAccessLevels (UserID, AccessLevelID)
  176. VALUES (@userID, @Level)
  177. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  178. UserID = @userID and AccessLevelID = @Level
  179. end
  180. if @target is NULL
  181. begin
  182. select @target = 0
  183. return @target
  184. end
  185. -- return the UserAccessLevelID
  186. RETURN @target
  187. GO
  188. SET QUOTED_IDENTIFIER OFF
  189. GO
  190. SET ANSI_NULLS ON
  191. GO
  192. SET QUOTED_IDENTIFIER ON
  193. GO
  194. SET ANSI_NULLS ON
  195. GO
  196. CREATE PROCEDURE AddUserForApproval
  197. @ApprovalType int,
  198. @User varchar(50),
  199. @Domain varchar(50),
  200. @Reason varchar(250)
  201. As
  202. set nocount on
  203. -- local var to hold record id
  204. declare @target int
  205. declare @userfound int
  206. declare @userApproved int
  207. SELECT @userfound = UserID FROM AuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  208. if @userfound is NULL
  209. begin
  210. select @target = -1
  211. return @target
  212. end
  213. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @userfound and ApprovalTypeID=@ApprovalType
  214. if @userApproved is NULL
  215. begin -- not there, create new record
  216. INSERT INTO Approvals (UserID,ApprovalTypeID, Reason, ApproverEmailStatus)
  217. VALUES (@userfound,@ApprovalType,@Reason,0)
  218. SELECT @userApproved = UserID from Approvals WHERE UserID = @userfound
  219. if @userApproved is NULL
  220. begin
  221. select @target = -1
  222. return @target
  223. end
  224. end
  225. select @target = @userApproved
  226. RETURN @target
  227. GO
  228. SET QUOTED_IDENTIFIER OFF
  229. GO
  230. SET ANSI_NULLS ON
  231. GO
  232. SET QUOTED_IDENTIFIER OFF
  233. GO
  234. SET ANSI_NULLS ON
  235. GO
  236. CREATE PROCEDURE AddUserLevel
  237. @UserID int,
  238. @Level int
  239. As
  240. set nocount on
  241. -- local var to hold record id
  242. declare @target int
  243. declare @levelDB int
  244. declare @userDB int
  245. select @userDB = UserID from AuthorizedUsers WHERE
  246. UserID = @UserID
  247. if @userDB is NULL
  248. begin
  249. select @userDB = 0
  250. return @userDB
  251. end
  252. select @levelDB = AccessLevelID FROM AccessLevels WHERE
  253. AccessLevelID = @Level
  254. if @levelDB is NULL
  255. begin
  256. select @levelDB = 0
  257. return @levelDB
  258. end
  259. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  260. UserID = @UserID and AccessLevelID = @Level
  261. if @target is NULL
  262. begin -- not there, create new record
  263. INSERT INTO UserAccessLevels (UserID, AccessLevelID)
  264. VALUES (@UserID, @Level)
  265. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  266. UserID = @UserID and AccessLevelID = @Level
  267. end
  268. RETURN @target
  269. GO
  270. SET QUOTED_IDENTIFIER OFF
  271. GO
  272. SET ANSI_NULLS ON
  273. GO
  274. SET QUOTED_IDENTIFIER ON
  275. GO
  276. SET ANSI_NULLS ON
  277. GO
  278. CREATE PROCEDURE ApproveUser
  279. @UserID int,
  280. @ApprovalTypeID int,
  281. @ApproverUserID int,
  282. @DateApproved varchar(50)
  283. As
  284. set nocount on
  285. -- local var to hold record id
  286. declare @target int
  287. declare @userfound int
  288. declare @userApproved int
  289. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @UserID and ApprovalTypeID = @ApprovalTypeID
  290. if @userApproved is not NULL
  291. begin -- not there, create new record
  292. UPDATE Approvals SET ApproverUserID = @ApproverUserID,
  293. DateApproved = @DateApproved,RequesterEmailStatus=0
  294. WHERE (UserID = @UserID and ApprovalTypeID = @ApprovalTypeID)
  295. end
  296. select @target = 0
  297. RETURN @target
  298. GO
  299. SET QUOTED_IDENTIFIER OFF
  300. GO
  301. SET ANSI_NULLS ON
  302. GO
  303. SET QUOTED_IDENTIFIER OFF
  304. GO
  305. SET ANSI_NULLS OFF
  306. GO
  307. -- return values:
  308. -- 0 = user not in database
  309. -- 1 = user in database or approved in approvals table
  310. -- 2 = user access is denied
  311. -- 3 = user in table, but not yet approved
  312. -- 4 = user not in approvals table
  313. CREATE PROCEDURE CheckApprovalAccess
  314. @User varchar(50),
  315. @Domain varchar(50),
  316. @Level int,
  317. @ApprovalTypeID int
  318. As
  319. set nocount on
  320. -- local var to hold record id
  321. declare @target int
  322. declare @HasApproval int
  323. declare @DBUserID int
  324. declare @ApproverUserID int
  325. declare @Reason varchar(255)
  326. declare @DateApproved varchar(50)
  327. declare @ApprovalsUserID int
  328. SELECT @DBUserID=AuthorizedUsers.UserID, @target=UserAccessLevels.AccessLevelID,
  329. @HasApproval= Approvals.UserID
  330. FROM UserAccessLevels INNER JOIN
  331. AuthorizedUsers ON
  332. UserAccessLevels.UserID = AuthorizedUsers.UserID LEFT OUTER
  333. JOIN
  334. Approvals ON
  335. AuthorizedUsers.UserID = Approvals.UserID
  336. WHERE (AuthorizedUsers.UserAlias = @User) AND
  337. (AuthorizedUsers.UserDomain = @Domain) AND
  338. ((UserAccessLevels.AccessLevelID = 0) OR
  339. ((UserAccessLevels.AccessLevelID = @Level) AND
  340. (AuthorizedUsers.DateSignedDCP IS NOT NULL) AND
  341. (CURRENT_TIMESTAMP <= DATEADD(month, 12,
  342. AuthorizedUsers.DateSignedDCP))))
  343. ORDER BY UserAccessLevels.AccessLevelID DESC
  344. -- @target will point to the last value we read
  345. if @target IS NULL
  346. begin
  347. select @target = 0
  348. return @target
  349. end
  350. else
  351. begin
  352. -- @target is 0 if access is denied, we return 2 in this case
  353. if @target = 0
  354. begin
  355. select @target = 2
  356. return @target
  357. end
  358. end
  359. SELECT @ApprovalsUserID = UserID,@ApproverUserID = ApproverUserID, @Reason = Reason, @DateApproved = DateApproved
  360. FROM Approvals
  361. WHERE UserID = @DBUserID AND ApprovalTypeID = @ApprovalTypeID
  362. -- not in this table, they need to be added
  363. if @ApprovalsUserID is NULL
  364. begin
  365. -- print 'okay to pass'
  366. select @target = 4
  367. return @target
  368. end
  369. if @ApproverUserID is NULL or @Reason is NULL or @DateApproved is NULL
  370. begin
  371. -- print 'not approved yet'
  372. select @target = 3
  373. return @target
  374. end
  375. -- print 'approved'
  376. select @target = 1
  377. RETURN @target
  378. GO
  379. SET QUOTED_IDENTIFIER OFF
  380. GO
  381. SET ANSI_NULLS ON
  382. GO
  383. SET QUOTED_IDENTIFIER OFF
  384. GO
  385. SET ANSI_NULLS OFF
  386. GO
  387. -- -1 = no access to this cab
  388. -- otherwise, statusid (which is >= 0)
  389. CREATE PROCEDURE CheckRestrictedCabAccess
  390. @User varchar(50),
  391. @Domain varchar(50),
  392. @iDatabase int,
  393. @iBucket int,
  394. @szCabFilename varchar(100),
  395. @szCabPath varchar(255)
  396. As
  397. set nocount on
  398. -- local var to hold record id
  399. declare @target int
  400. declare @userfound int
  401. declare @userApproved int
  402. declare @CabAccessID int
  403. declare @StatusID int
  404. SELECT @userfound = UserID FROM AuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  405. if @userfound is NULL
  406. begin
  407. select @target = -1
  408. return @target
  409. end
  410. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @userfound
  411. if @userApproved is NULL
  412. begin
  413. select @target = -1
  414. return @target
  415. end
  416. SELECT @CabAccessID= CabAccessID, @StatusID=StatusID from CabAccess WHERE UserID = @userfound and iBucket=@iBucket and iDatabase=@iDatabase
  417. and CabFilename=@szCabFilename and CabPath =@szCabPath
  418. if @CabAccessID is NULL
  419. begin
  420. select @target = -1
  421. return @target
  422. end
  423. if @StatusID is NULL
  424. begin
  425. select @target = -1
  426. return @target
  427. end
  428. select @target = @StatusID
  429. RETURN @target
  430. GO
  431. SET QUOTED_IDENTIFIER OFF
  432. GO
  433. SET ANSI_NULLS ON
  434. GO
  435. SET QUOTED_IDENTIFIER OFF
  436. GO
  437. SET ANSI_NULLS ON
  438. GO
  439. CREATE PROCEDURE CheckUserAccess
  440. @User varchar(50),
  441. @Domain varchar(50),
  442. @Level int
  443. As
  444. set nocount on
  445. -- local var to hold record id
  446. declare @target int
  447. SELECT @target= dbo.UserAccessLevels.AccessLevelID FROM
  448. dbo.UserAccessLevels INNER JOIN dbo.AuthorizedUsers ON dbo.UserAccessLevels.UserID = dbo.AuthorizedUsers.UserID
  449. WHERE (dbo.AuthorizedUsers.UserAlias = @User ) AND (dbo.AuthorizedUsers.UserDomain = @Domain) AND
  450. ((AccessLevelID = 0) OR (AccessLevelID = @Level AND (AuthorizedUsers.DateSignedDCP IS Not Null AND
  451. CURRENT_TIMESTAMP <= DATEADD(month,12,DateSignedDCP) ) ) )
  452. ORDER BY AccessLevelID DESC
  453. -- @target will point to the last value we read
  454. if @target IS NULL
  455. begin
  456. select @target = 0
  457. end
  458. else
  459. begin
  460. -- @target is 0 if access is denied, we return 2 in this case
  461. if @target = 0
  462. begin
  463. select @target = 2
  464. end
  465. else
  466. begin
  467. select @target = 1
  468. end
  469. end
  470. -- PRINT @target
  471. RETURN @target
  472. GO
  473. SET QUOTED_IDENTIFIER OFF
  474. GO
  475. SET ANSI_NULLS ON
  476. GO
  477. SET QUOTED_IDENTIFIER OFF
  478. GO
  479. SET ANSI_NULLS ON
  480. GO
  481. -- return values:
  482. -- 0 = user not in database
  483. -- 1 = user in database or approved in approvals table
  484. -- 2 = user access is denied
  485. -- 3 = user in table, but not yet approved
  486. -- 4 = user not in approvals table
  487. CREATE PROCEDURE CheckUserAccessApprovals
  488. @User varchar(50),
  489. @Domain varchar(50),
  490. @Level int,
  491. @ApprovalTypeID int
  492. As
  493. set nocount on
  494. -- local var to hold record id
  495. declare @target int
  496. declare @HasApproval int
  497. declare @DBUserID int
  498. declare @ApproverUserID int
  499. declare @Reason varchar(255)
  500. declare @DateApproved varchar(50)
  501. declare @ApprovalsUserID int
  502. SELECT @DBUserID=AuthorizedUsers.UserID, @target=UserAccessLevels.AccessLevelID,
  503. @HasApproval= Approvals.UserID
  504. FROM UserAccessLevels INNER JOIN
  505. AuthorizedUsers ON
  506. UserAccessLevels.UserID = AuthorizedUsers.UserID LEFT OUTER
  507. JOIN
  508. Approvals ON
  509. AuthorizedUsers.UserID = Approvals.UserID
  510. WHERE (AuthorizedUsers.UserAlias = @User) AND
  511. (AuthorizedUsers.UserDomain = @Domain) AND
  512. ((UserAccessLevels.AccessLevelID = 0) OR
  513. ((UserAccessLevels.AccessLevelID = @Level) AND
  514. (AuthorizedUsers.DateSignedDCP IS NOT NULL) AND
  515. (CURRENT_TIMESTAMP <= DATEADD(month, 12,
  516. AuthorizedUsers.DateSignedDCP))))
  517. ORDER BY UserAccessLevels.AccessLevelID DESC
  518. -- @target will point to the last value we read
  519. if @target IS NULL
  520. begin
  521. select @target = 0
  522. return @target
  523. end
  524. else
  525. begin
  526. -- @target is 0 if access is denied, we return 2 in this case
  527. if @target = 0
  528. begin
  529. select @target = 2
  530. return @target
  531. end
  532. end
  533. SELECT @ApprovalsUserID = UserID,@ApproverUserID = ApproverUserID, @Reason = Reason, @DateApproved = DateApproved
  534. FROM Approvals
  535. WHERE UserID = @DBUserID AND ApprovalTypeID = @ApprovalTypeID
  536. if @ApprovalsUserID is NULL
  537. begin
  538. -- print 'okay to pass'
  539. -- not in this table, okay to pass if ApprovalTypeID=1
  540. if @ApprovalTypeID=1
  541. begin
  542. select @target = 1
  543. return @target
  544. end
  545. else
  546. -- Otherwise, return 4
  547. begin
  548. select @target = 4
  549. return @target
  550. end
  551. end
  552. if @ApproverUserID is NULL or @Reason is NULL or @DateApproved is NULL
  553. begin
  554. -- print 'not approved yet'
  555. select @target = 3
  556. return @target
  557. end
  558. -- print 'approved'
  559. select @target = 1
  560. RETURN @target
  561. GO
  562. SET QUOTED_IDENTIFIER OFF
  563. GO
  564. SET ANSI_NULLS ON
  565. GO
  566. SET QUOTED_IDENTIFIER ON
  567. GO
  568. SET ANSI_NULLS ON
  569. GO
  570. CREATE PROCEDURE dbo.DefragIndexes
  571. @maxfrag decimal = 80.0 --Scan density < than this number
  572. , @ActualCountRatio int = 10 --The Actial count ratio > than this number
  573. , @ReportOnly char(1) = 'Y' --Change to 'Y' if you just want the report
  574. AS
  575. SET NOCOUNT ON
  576. DECLARE @tablename varchar (128)
  577. , @execstr varchar (255)
  578. , @objectid int
  579. , @LogicalFrag decimal
  580. , @ScanDensity decimal
  581. , @ratio varchar (25)
  582. , @IndexName varchar(64)
  583. , @StartTime datetime
  584. , @EndTime datetime
  585. PRINT '/*****************************************************************************************************/'
  586. PRINT 'Starting DEFRAG on ' + db_name()
  587. SELECT @StartTime = getdate()
  588. PRINT 'Start Time: ' + convert(varchar(20),@StartTime)
  589. -- Declare cursor
  590. DECLARE tables CURSOR FOR
  591. SELECT name FROM sysobjects
  592. WHERE type = 'u' --Tables only
  593. --and uid = 1 --dbo only
  594. ORDER BY name
  595. -- Create the table
  596. CREATE TABLE #fraglist (
  597. ObjectName varchar(255)
  598. , ObjectId int
  599. , IndexName varchar(255)
  600. , IndexId int
  601. , Lvl int
  602. , CountPages int
  603. , CountRows int
  604. , MinRecSize int
  605. , MaxRecSize int
  606. , AvgRecSize int
  607. , ForRecCount int
  608. , Extents int
  609. , ExtentSwitches int
  610. , AvgFreeBytes int
  611. , AvgPageDensity int
  612. , ScanDensity decimal
  613. , BestCount int
  614. , ActualCount int
  615. , LogicalFrag decimal
  616. , ExtentFrag decimal)
  617. -- Open the cursor & Loop through all the tables in the database
  618. OPEN tables FETCH NEXT
  619. FROM tables
  620. INTO @tablename
  621. WHILE @@FETCH_STATUS = 0
  622. BEGIN
  623. -- Do the showcontig of all indexes of the table
  624. INSERT INTO #fraglist
  625. EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''') WITH FAST, TABLERESULTS, NO_INFOMSGS')
  626. FETCH NEXT
  627. FROM tables INTO @tablename
  628. END
  629. -- Close and deallocate the cursor
  630. CLOSE tables
  631. DEALLOCATE tables
  632. --Print SHOW_CONTIG Results if 'Y'
  633. PRINT 'Fragmentation Before DBCC INDEXDEFRAG'
  634. SELECT left(ObjectName,35) 'TableName'
  635. ,left(IndexName,45) 'IndexName'
  636. ,LogicalFrag 'Logical Frag %'
  637. ,ScanDensity 'Scan Density %'
  638. , '[' + rtrim(convert(varchar(8),BestCount)) + ':' + rtrim(convert(varchar(8),ActualCount)) + ']' 'Ratio'
  639. FROM #fraglist
  640. WHERE ScanDensity <= @maxfrag
  641. AND ActualCount >= @ActualCountRatio
  642. AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
  643. -- Declare cursor for list of indexes to be defragged
  644. DECLARE indexes CURSOR FOR
  645. SELECT ObjectName,
  646. ObjectId,
  647. IndexName,
  648. LogicalFrag,
  649. ScanDensity,
  650. '[' + rtrim(convert(varchar(8),BestCount)) + ':' + rtrim(convert(varchar(8),ActualCount)) + ']'
  651. FROM #fraglist
  652. WHERE ScanDensity <= @maxfrag
  653. AND ActualCount >= @ActualCountRatio
  654. AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
  655. -- Open the cursor
  656. OPEN indexes FETCH NEXT FROM indexes INTO @tablename, @objectid, @IndexName, @LogicalFrag, @ScanDensity, @ratio
  657. WHILE @@FETCH_STATUS = 0
  658. BEGIN
  659. PRINT '--Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ','
  660. + RTRIM(@IndexName) + ') - Logical Frag Currently:'
  661. + RTRIM(CONVERT(varchar(15),@LogicalFrag)) + '% - Scan Density Currently:' + @ratio
  662. SELECT @execstr = 'DBCC INDEXDEFRAG (0, ''' + RTRIM(@tablename)
  663. + ''', ''' + RTRIM(@IndexName) + ''')'
  664. If @ReportOnly = 'Y'
  665. BEGIN
  666. EXEC (@execstr) --Report and Execute the DBCC INDEXDEFRAG
  667. END
  668. ELSE
  669. BEGIN
  670. Print @execstr --Report Only
  671. END
  672. FETCH NEXT FROM indexes INTO @tablename, @objectid, @IndexName, @LogicalFrag, @ScanDensity, @ratio END
  673. -- Close and deallocate the cursor
  674. CLOSE indexes
  675. DEALLOCATE indexes
  676. SELECT @EndTime = getdate()
  677. PRINT 'End Time: ' + convert(varchar(20),@EndTime)
  678. PRINT 'Elapses Time: ' + convert(varchar(20), DATEDIFF(mi, @StartTime, @EndTime) ) + ' minutes.'
  679. -- Delete the temporary table
  680. DROP TABLE #fraglist
  681. GO
  682. SET QUOTED_IDENTIFIER OFF
  683. GO
  684. SET ANSI_NULLS ON
  685. GO
  686. SET QUOTED_IDENTIFIER ON
  687. GO
  688. SET ANSI_NULLS ON
  689. GO
  690. CREATE PROCEDURE GetUserID
  691. @User varchar(50),
  692. @Domain varchar(50)
  693. As
  694. set nocount on
  695. -- local var to hold record id
  696. declare @target int
  697. SELECT @target= UserID From AuthorizedUsers Where UserAlias = @User and UserDomain = @Domain
  698. -- @target will point to the last value we read
  699. if @target IS NULL
  700. begin
  701. select @target = -1
  702. end
  703. -- PRINT @target
  704. RETURN @target
  705. GO
  706. SET QUOTED_IDENTIFIER OFF
  707. GO
  708. SET ANSI_NULLS ON
  709. GO
  710. SET QUOTED_IDENTIFIER OFF
  711. GO
  712. SET ANSI_NULLS ON
  713. GO
  714. CREATE PROCEDURE OcaAddUserAndLevel
  715. @User varchar(50),
  716. @Domain varchar(50),
  717. @CurrTime varchar(50),
  718. @Level int
  719. As set nocount on
  720. -- local var to hold record id
  721. declare @target int
  722. declare @levelDB int
  723. declare @userDB int
  724. declare @userID int
  725. -- do we already have this FileInCab?
  726. SELECT @userID = UserID FROM OcaAuthorizedUsers WHERE
  727. UserAlias =@User AND UserDomain = @Domain
  728. if @userID is NULL
  729. begin -- not there, create new record
  730. INSERT INTO OcaAuthorizedUsers (UserAlias, UserDomain, DateSignedDCP)
  731. VALUES (@User, @Domain,@CurrTime)
  732. SELECT @userID = UserID FROM OcaAuthorizedUsers WHERE
  733. UserAlias =@User AND UserDomain = @Domain
  734. end
  735. else
  736. begin -- update record with new signing date/time
  737. UPDATE OcaAuthorizedUsers SET DateSignedDCP = @CurrTime
  738. WHERE (UserID = @userID)
  739. end
  740. -- PRINT @userID
  741. -- we don't have the userid
  742. if @userID is NULL
  743. begin
  744. select @userID = 0
  745. return @userID
  746. end
  747. select @levelDB = AccessLevelID FROM OcaAccessLevels WHERE
  748. AccessLevelID = @Level
  749. -- bad level passed in
  750. if @levelDB is NULL
  751. begin
  752. select @levelDB = 0
  753. return @levelDB
  754. end
  755. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  756. UserID = @userID and AccessLevelID = @Level
  757. if @target is NULL
  758. begin -- not there, create new record
  759. INSERT INTO OcaUserAccessLevels (UserID, AccessLevelID)
  760. VALUES (@userID, @Level)
  761. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  762. UserID = @userID and AccessLevelID = @Level
  763. end
  764. if @target is NULL
  765. begin
  766. select @target = 0
  767. return @target
  768. end
  769. -- return the UserAccessLevelID
  770. RETURN @target
  771. GO
  772. SET QUOTED_IDENTIFIER OFF
  773. GO
  774. SET ANSI_NULLS ON
  775. GO
  776. SET QUOTED_IDENTIFIER OFF
  777. GO
  778. SET ANSI_NULLS ON
  779. GO
  780. CREATE PROCEDURE OcaAddUserForApproval
  781. @ApprovalType int,
  782. @User varchar(50),
  783. @Domain varchar(50),
  784. @Reason varchar(250)
  785. As set nocount on
  786. -- local var to hold record id
  787. declare @target int
  788. declare @userfound int
  789. declare @userApproved int
  790. SELECT @userfound = UserID FROM OcaAuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  791. if @userfound is NULL
  792. begin
  793. select @target = -1
  794. return @target
  795. end
  796. SELECT @userApproved = UserID FROM OcaApprovals WHERE UserID = @userfound
  797. if @userApproved is NULL
  798. begin -- not there, create new record
  799. INSERT INTO OcaApprovals (UserID,ApprovalTypeID, Reason)
  800. VALUES (@userfound,@ApprovalType,@Reason)
  801. SELECT @userApproved = UserID from OcaApprovals WHERE UserID = @userfound
  802. if @userApproved is NULL
  803. begin
  804. select @target = -1
  805. return @target
  806. end
  807. end
  808. select @target = @userApproved
  809. RETURN @target
  810. GO
  811. SET QUOTED_IDENTIFIER OFF
  812. GO
  813. SET ANSI_NULLS ON
  814. GO
  815. SET QUOTED_IDENTIFIER OFF
  816. GO
  817. SET ANSI_NULLS ON
  818. GO
  819. CREATE PROCEDURE OcaAddUserLevel
  820. @UserID int,
  821. @Level int
  822. As set nocount on
  823. -- local var to hold record id
  824. declare @target int
  825. declare @levelDB int
  826. declare @userDB int
  827. select @userDB = UserID from OcaAuthorizedUsers WHERE
  828. UserID = @UserID
  829. if @userDB is NULL
  830. begin
  831. select @userDB = 0
  832. return @userDB
  833. end
  834. select @levelDB = AccessLevelID FROM OcaAccessLevels WHERE
  835. AccessLevelID = @Level
  836. if @levelDB is NULL
  837. begin
  838. select @levelDB = 0
  839. return @levelDB
  840. end
  841. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  842. UserID = @UserID and AccessLevelID = @Level
  843. if @target is NULL
  844. begin -- not there, create new record
  845. INSERT INTO OcaUserAccessLevels (UserID, AccessLevelID)
  846. VALUES (@UserID, @Level)
  847. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  848. UserID = @UserID and AccessLevelID = @Level
  849. end
  850. RETURN @target
  851. GO
  852. SET QUOTED_IDENTIFIER OFF
  853. GO
  854. SET ANSI_NULLS ON
  855. GO
  856. SET QUOTED_IDENTIFIER ON
  857. GO
  858. SET ANSI_NULLS ON
  859. GO
  860. CREATE PROCEDURE OcaApproveUser
  861. @UserID int,
  862. @ApprovalTypeID int,
  863. @ApproverUserID int,
  864. @DateApproved varchar(50)
  865. As set nocount on
  866. -- local var to hold record id
  867. declare @target int
  868. declare @userfound int
  869. declare @userApproved int
  870. SELECT @userApproved = UserID FROM OcaApprovals WHERE UserID = @UserID and ApprovalTypeID = @ApprovalTypeID
  871. if @userApproved is not NULL
  872. begin -- not there, create new record
  873. UPDATE OcaApprovals SET ApproverUserID = @ApproverUserID,
  874. DateApproved = @DateApproved
  875. WHERE (UserID = @UserID and ApprovalTypeID = @ApprovalTypeID)
  876. end
  877. select @target = 0
  878. RETURN @target
  879. GO
  880. SET QUOTED_IDENTIFIER OFF
  881. GO
  882. SET ANSI_NULLS ON
  883. GO
  884. SET QUOTED_IDENTIFIER OFF
  885. GO
  886. SET ANSI_NULLS ON
  887. GO
  888. CREATE PROCEDURE OcaCheckUserAccess
  889. @User varchar(50),
  890. @Domain varchar(50),
  891. @Level int
  892. As set nocount on
  893. -- local var to hold record id
  894. declare @target int
  895. SELECT @target= dbo.OcaUserAccessLevels.AccessLevelID FROM
  896. dbo.OcaUserAccessLevels INNER JOIN dbo.OcaAuthorizedUsers ON dbo.OcaUserAccessLevels.UserID = dbo.OcaAuthorizedUsers.UserID
  897. WHERE (dbo.OcaAuthorizedUsers.UserAlias = @User ) AND (dbo.OcaAuthorizedUsers.UserDomain = @Domain) AND
  898. ((AccessLevelID = 0) OR (AccessLevelID = @Level AND (OcaAuthorizedUsers.DateSignedDCP IS Not Null AND
  899. CURRENT_TIMESTAMP <= DATEADD(month,12,DateSignedDCP) ) ) )
  900. ORDER BY AccessLevelID DESC
  901. -- @target will point to the last value we read
  902. if @target IS NULL
  903. begin
  904. select @target = 0
  905. end
  906. else
  907. begin
  908. -- @target is 0 if access is denied, we return 2 in this case
  909. if @target = 0
  910. begin
  911. select @target = 2
  912. end
  913. else
  914. begin
  915. select @target = 1
  916. end
  917. end
  918. -- PRINT @target
  919. RETURN @target
  920. GO
  921. SET QUOTED_IDENTIFIER OFF
  922. GO
  923. SET ANSI_NULLS ON
  924. GO
  925. SET QUOTED_IDENTIFIER OFF
  926. GO
  927. SET ANSI_NULLS ON
  928. GO
  929. -- return values:
  930. -- 0 = user not in database
  931. -- 1 = user in database or approved in approvals table
  932. -- 2 = user access is denied
  933. -- 3 = user not yet approved
  934. CREATE PROCEDURE OcaCheckUserAccessApprovals
  935. @User varchar(50),
  936. @Domain varchar(50),
  937. @Level int,
  938. @ApprovalTypeID int
  939. As set nocount on
  940. -- local var to hold record id
  941. declare @target int
  942. declare @HasApproval int
  943. declare @DBUserID int
  944. declare @ApproverUserID int
  945. declare @Reason varchar(255)
  946. declare @DateApproved varchar(50)
  947. declare @ApprovalsUserID int
  948. SELECT @DBUserID=OcaAuthorizedUsers.UserID, @target=OcaUserAccessLevels.AccessLevelID,
  949. @HasApproval= OcaApprovals.UserID
  950. FROM OcaUserAccessLevels INNER JOIN
  951. OcaAuthorizedUsers ON
  952. OcaUserAccessLevels.UserID = OcaAuthorizedUsers.UserID LEFT OUTER
  953. JOIN
  954. OcaApprovals ON
  955. OcaAuthorizedUsers.UserID = OcaApprovals.UserID
  956. WHERE (OcaAuthorizedUsers.UserAlias = @User) AND
  957. (OcaAuthorizedUsers.UserDomain = @Domain) AND
  958. ((OcaUserAccessLevels.AccessLevelID = 0) OR
  959. ((OcaUserAccessLevels.AccessLevelID = @Level) AND
  960. (OcaAuthorizedUsers.DateSignedDCP IS NOT NULL) AND
  961. (CURRENT_TIMESTAMP <= DATEADD(month, 12,
  962. OcaAuthorizedUsers.DateSignedDCP))))
  963. ORDER BY OcaUserAccessLevels.AccessLevelID DESC
  964. -- @target will point to the last value we read
  965. if @target IS NULL
  966. begin
  967. select @target = 0
  968. return @target
  969. end
  970. else
  971. begin
  972. -- @target is 0 if access is denied, we return 2 in this case
  973. if @target = 0
  974. begin
  975. select @target = 2
  976. return @target
  977. end
  978. end
  979. SELECT @ApprovalsUserID = UserID,@ApproverUserID = ApproverUserID, @Reason = Reason, @DateApproved = DateApproved
  980. FROM OcaApprovals
  981. WHERE UserID = @DBUserID AND ApprovalTypeID = @ApprovalTypeID
  982. -- not in this table, okay to pass
  983. if @ApprovalsUserID is NULL
  984. begin
  985. -- print 'okay to pass'
  986. select @target = 1
  987. return @target
  988. end
  989. if @ApproverUserID is NULL or @Reason is NULL or @DateApproved is NULL
  990. begin
  991. -- print 'not approved yet'
  992. select @target = 3
  993. return @target
  994. end
  995. -- print 'approved'
  996. select @target = 1
  997. RETURN @target
  998. GO
  999. SET QUOTED_IDENTIFIER OFF
  1000. GO
  1001. SET ANSI_NULLS ON
  1002. GO
  1003. SET QUOTED_IDENTIFIER ON
  1004. GO
  1005. SET ANSI_NULLS ON
  1006. GO
  1007. CREATE PROCEDURE OcaGetUserID
  1008. @User varchar(50),
  1009. @Domain varchar(50)
  1010. As set nocount on
  1011. -- local var to hold record id
  1012. declare @target int
  1013. SELECT @target= UserID From OcaAuthorizedUsers Where UserAlias = @User and UserDomain = @Domain
  1014. -- @target will point to the last value we read
  1015. if @target IS NULL
  1016. begin
  1017. select @target = -1
  1018. end
  1019. -- PRINT @target
  1020. RETURN @target
  1021. GO
  1022. SET QUOTED_IDENTIFIER OFF
  1023. GO
  1024. SET ANSI_NULLS ON
  1025. GO
  1026. SET QUOTED_IDENTIFIER OFF
  1027. GO
  1028. SET ANSI_NULLS ON
  1029. GO
  1030. CREATE PROCEDURE OcaRemoveUserLevel
  1031. @UserID int,
  1032. @Level int
  1033. As set nocount on
  1034. -- local var to hold record id
  1035. declare @target int
  1036. declare @levelDB int
  1037. declare @userDB int
  1038. select @userDB = UserID from OcaAuthorizedUsers WHERE
  1039. UserID = @UserID
  1040. if @userDB is NULL
  1041. begin
  1042. select @userDB = 0
  1043. return @userDB
  1044. end
  1045. select @levelDB = AccessLevelID FROM OcaAccessLevels WHERE
  1046. AccessLevelID = @Level
  1047. if @levelDB is NULL
  1048. begin
  1049. select @levelDB = 0
  1050. return @levelDB
  1051. end
  1052. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  1053. UserID = @UserID and AccessLevelID = @Level
  1054. if @target is not NULL
  1055. begin -- there, delete record
  1056. DELETE FROM OcaUserAccessLevels WHERE UserID = @UserID and AccessLevelID = @Level
  1057. end
  1058. RETURN @target
  1059. GO
  1060. SET QUOTED_IDENTIFIER OFF
  1061. GO
  1062. SET ANSI_NULLS ON
  1063. GO
  1064. SET QUOTED_IDENTIFIER OFF
  1065. GO
  1066. SET ANSI_NULLS ON
  1067. GO
  1068. CREATE PROCEDURE RemoveUserLevel
  1069. @UserID int,
  1070. @Level int
  1071. As
  1072. set nocount on
  1073. -- local var to hold record id
  1074. declare @target int
  1075. declare @levelDB int
  1076. declare @userDB int
  1077. select @userDB = UserID from AuthorizedUsers WHERE
  1078. UserID = @UserID
  1079. if @userDB is NULL
  1080. begin
  1081. select @userDB = 0
  1082. return @userDB
  1083. end
  1084. select @levelDB = AccessLevelID FROM AccessLevels WHERE
  1085. AccessLevelID = @Level
  1086. if @levelDB is NULL
  1087. begin
  1088. select @levelDB = 0
  1089. return @levelDB
  1090. end
  1091. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  1092. UserID = @UserID and AccessLevelID = @Level
  1093. if @target is not NULL
  1094. begin -- there, delete record
  1095. DELETE FROM UserAccessLevels WHERE UserID = @UserID and AccessLevelID = @Level
  1096. end
  1097. RETURN @target
  1098. GO
  1099. SET QUOTED_IDENTIFIER OFF
  1100. GO
  1101. SET ANSI_NULLS ON
  1102. GO
  1103. SET QUOTED_IDENTIFIER OFF
  1104. GO
  1105. SET ANSI_NULLS OFF
  1106. GO
  1107. -- return values:
  1108. -- 0 = user not in database
  1109. -- 1 = user in database or approved in approvals table
  1110. -- 2 = user access is denied
  1111. -- 3 = user in table, but not yet approved
  1112. -- 4 = user not in approvals table
  1113. CREATE PROCEDURE TestProc
  1114. As
  1115. set nocount on
  1116. -- local var to hold record id
  1117. declare @target int
  1118. select @target = 52
  1119. print 'foo' + CONVERT(char(19),@target)+'\n'
  1120. print @target
  1121. print '\n'
  1122. GO
  1123. SET QUOTED_IDENTIFIER OFF
  1124. GO
  1125. SET ANSI_NULLS ON
  1126. GO
  1127. SET QUOTED_IDENTIFIER ON
  1128. GO
  1129. SET ANSI_NULLS ON
  1130. GO
  1131. CREATE PROCEDURE rolemember
  1132. @rolename sysname = NULL
  1133. AS
  1134. if @rolename is not null
  1135. begin
  1136. -- VALIDATE GIVEN NAME
  1137. if not exists (select * from sysusers where name = @rolename and issqlrole = 1)
  1138. begin
  1139. raiserror(15409, -1, -1, @rolename)
  1140. return (1)
  1141. end
  1142. -- RESULT SET FOR SINGLE ROLE
  1143. select MemberName = u.name,DbRole = g.name
  1144. from sysusers u, sysusers g, sysmembers m
  1145. where g.name = @rolename
  1146. and g.uid = m.groupuid
  1147. and g.issqlrole = 1
  1148. and u.uid = m.memberuid
  1149. order by 1, 2
  1150. end
  1151. else
  1152. begin
  1153. -- RESULT SET FOR ALL ROLES
  1154. select MemberName = u.name,DbRole = g.name
  1155. from sysusers u, sysusers g, sysmembers m
  1156. where g.uid = m.groupuid
  1157. and g.issqlrole = 1
  1158. and u.uid = m.memberuid
  1159. order by 1, 2
  1160. end
  1161. return (0) -- sp_helprolemember
  1162. GO
  1163. SET QUOTED_IDENTIFIER OFF
  1164. GO
  1165. SET ANSI_NULLS ON
  1166. GO
  1167. SET QUOTED_IDENTIFIER ON
  1168. GO
  1169. SET ANSI_NULLS ON
  1170. GO
  1171. CREATE PROCEDURE updatestats
  1172. @resample CHAR(8)='FULLSCAN'
  1173. AS
  1174. DECLARE @dbsid varbinary(85)
  1175. SELECT @dbsid = sid
  1176. FROM master.dbo.sysdatabases
  1177. WHERE name = db_name()
  1178. /*Check the user sysadmin*/
  1179. IF NOT is_srvrolemember('sysadmin') = 1 AND suser_sid() <> @dbsid
  1180. BEGIN
  1181. RAISERROR(15247,-1,-1)
  1182. RETURN (1)
  1183. END
  1184. -- required so it can update stats on on ICC/IVs
  1185. set ansi_nulls on
  1186. set quoted_identifier on
  1187. set ansi_warnings on
  1188. set ansi_padding on
  1189. set arithabort on
  1190. set concat_null_yields_null on
  1191. set numeric_roundabort off
  1192. DECLARE @exec_stmt nvarchar(540)
  1193. DECLARE @tablename sysname
  1194. DECLARE @uid smallint
  1195. DECLARE @user_name sysname
  1196. DECLARE @tablename_header varchar(267)
  1197. DECLARE ms_crs_tnames CURSOR LOCAL FAST_FORWARD READ_ONLY FOR SELECT name, uid FROM sysobjects WHERE type = 'U'
  1198. OPEN ms_crs_tnames
  1199. FETCH NEXT FROM ms_crs_tnames INTO @tablename, @uid
  1200. WHILE (@@fetch_status <> -1)
  1201. BEGIN
  1202. IF (@@fetch_status <> -2)
  1203. BEGIN
  1204. SELECT @user_name = user_name(@uid)
  1205. SELECT @tablename_header = 'Updating ' + @user_name +'.'+ RTRIM(@tablename)
  1206. PRINT @tablename_header
  1207. SELECT @exec_stmt = 'UPDATE STATISTICS ' + quotename( @user_name , '[')+'.' + quotename( @tablename, '[')
  1208. if (UPPER(@resample)='FULLSCAN') SET @exec_stmt = @exec_stmt + ' WITH fullscan'
  1209. EXEC (@exec_stmt)
  1210. END
  1211. FETCH NEXT FROM ms_crs_tnames INTO @tablename, @uid
  1212. END
  1213. PRINT ' '
  1214. PRINT ' '
  1215. raiserror(15005,-1,-1)
  1216. DEALLOCATE ms_crs_tnames
  1217. RETURN(0) -- sp_updatestats
  1218. GO
  1219. SET QUOTED_IDENTIFIER OFF
  1220. GO
  1221. SET ANSI_NULLS ON
  1222. GO