Update SQLQuery_SpeedCameras.sql#1
Open
Imran-imtiaz48 wants to merge 1 commit intojanluksoft:mainfrom
Open
Conversation
I improved your dynamic pivot script by first removing the DROP TABLE statement at the end, since it would delete your source data unless you were intentionally using a staging table. I also replaced the old FOR XML PATH method with STRING_AGG, which makes building dynamic column lists much cleaner and easier to read. To improve clarity, I renamed variables like @U, @uzeros, and @UGB into more descriptive names such as @quote, @emptycell, and @gbSuffix. Instead of relying heavily on the FORMAT() function, which can be slow, I used string concatenation with RIGHT() for padding dates, which performs better. For executing the dynamic SQL, I replaced EXECUTE() with sp_executesql to make the query safer and parameterized. Overall, the script is now cleaner, faster, safer, and easier to maintain.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I improved your dynamic pivot script by first removing the DROP TABLE statement at the end, since it would delete your source data unless you were intentionally using a staging table. I also replaced the old FOR XML PATH method with STRING_AGG, which makes building dynamic column lists much cleaner and easier to read. To improve clarity, I renamed variables like @U, @uzeros, and @UGB into more descriptive names such as @quote, @emptycell, and @gbSuffix. Instead of relying heavily on the FORMAT() function, which can be slow, I used string concatenation with RIGHT() for padding dates, which performs better. For executing the dynamic SQL, I replaced EXECUTE() with sp_executesql to make the query safer and parameterized. Overall, the script is now cleaner, faster, safer, and easier to maintain.