Last active
October 12, 2024 16:36
-
-
Save alivarzeshi/0bc30386a53ae5cd3eea58fd382589fc to your computer and use it in GitHub Desktop.
Main Folder Security
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @IsPrimary INT; | |
DECLARE @JobName NVARCHAR(128); | |
-- Check if the current node is the primary replica | |
SELECT @IsPrimary = CASE | |
WHEN role_desc = 'PRIMARY' THEN 1 | |
ELSE 0 | |
END | |
FROM sys.dm_hadr_availability_replica_states AS ars | |
JOIN sys.availability_replicas AS ar | |
ON ars.replica_id = ar.replica_id | |
WHERE ars.is_local = 1; | |
-- If this node is primary, enable all jobs. If secondary, disable all jobs. | |
IF @IsPrimary = 1 | |
BEGIN | |
-- Enable all SQL Agent jobs | |
DECLARE @EnableJobs TABLE (JobName NVARCHAR(128)); | |
-- Insert all jobs into the temporary table | |
INSERT INTO @EnableJobs (JobName) | |
SELECT name | |
FROM msdb.dbo.sysjobs; | |
-- Enable all jobs in the table | |
WHILE EXISTS (SELECT * FROM @EnableJobs) | |
BEGIN | |
SELECT TOP 1 @JobName = JobName FROM @EnableJobs; | |
EXEC msdb.dbo.sp_update_job @job_name = @JobName, @enabled = 1; | |
PRINT 'Enabled ' + @JobName + ' on the primary replica'; | |
-- Remove the job from the table after processing | |
DELETE FROM @EnableJobs WHERE JobName = @JobName; | |
END | |
END | |
ELSE | |
BEGIN | |
-- Disable all SQL Agent jobs | |
DECLARE @DisableJobs TABLE (JobName NVARCHAR(128)); | |
-- Insert all jobs into the temporary table | |
INSERT INTO @DisableJobs (JobName) | |
SELECT name | |
FROM msdb.dbo.sysjobs; | |
-- Disable all jobs in the table | |
WHILE EXISTS (SELECT * FROM @DisableJobs) | |
BEGIN | |
SELECT TOP 1 @JobName = JobName FROM @DisableJobs; | |
EXEC msdb.dbo.sp_update_job @job_name = @JobName, @enabled = 0; | |
PRINT 'Disabled ' + @JobName + ' on the secondary replica'; | |
-- Remove the job from the table after processing | |
DELETE FROM @DisableJobs WHERE JobName = @JobName; | |
END | |
END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This query retrieves information about employees along with their associated projects, if any. | |
The OUTER APPLY operator ensures that all employees are listed, even if they have no associated projects. | |
*/ | |
SELECT | |
e.EmployeeID, | |
e.FirstName, | |
e.LastName, | |
p.ProjectName, | |
p.StartDate, | |
p.EndDate, | |
p.Budget | |
FROM | |
Employees e | |
OUTER APPLY | |
(SELECT ProjectName, StartDate, EndDate, Budget | |
FROM Projects | |
WHERE Projects.EmployeeID = e.EmployeeID) p; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This query retrieves unique locations from both the Departments table and the combination of | |
Employees and Departments, avoiding duplicates by using UNION. | |
*/ | |
SELECT Location FROM Departments | |
UNION | |
SELECT d.Location | |
FROM Employees e | |
INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This query retrieves all locations from both the Departments table and the combination of | |
Employees and Departments, including duplicates, using UNION ALL. | |
*/ | |
SELECT Location FROM Departments | |
UNION ALL | |
SELECT d.Location | |
FROM Employees e | |
INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This query retrieves locations that are common between the Departments table and | |
the combination of Employees and Departments using INTERSECT. | |
*/ | |
SELECT Location FROM Departments | |
INTERSECT | |
SELECT d.Location | |
FROM Employees e | |
INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the revised version of your original code incorporating the recommendations for error handling, safe cursor usage, logging, and documentation. The updated script includes improved structure, error logging, and more robust logic to handle job enabling/disabling during failovers in an Always On Availability Group:
Detailed Review of Changes:
Overall Comments:
Global Error Handling:
CATCH
block with detailed error information (error number, severity, state, procedure, and line) to provide complete debugging information.Primary/Secondary Detection:
RETURN
) in case the role cannot be determined, which prevents further processing in an invalid state.Cursors:
CLOSE
andDEALLOCATE
to ensure no resource leaks.Exclusion of Critical Jobs:
'CriticalJob1'
,'CriticalJob2'
) that should not be disabled/enabled. You can modify this list as needed for your environment.Logging:
PRINT
statements are included for interactive debugging, but an example of logging to a table (JobExecutionLog
) is provided in the comments. This could be useful for long-term tracking or integration with a monitoring system.Testing and Deployment:
With these changes, the script is now robust, safe, and ready for a production environment, providing clear error handling, easy troubleshooting, and protection against resource leaks or unintended job disabling.