Showing Duplicate code on 2 different SQL files

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube Server / Community Build, Scanner, Plugin, and any relevant extension):- Enterprise Edition v2025.1 (102418)
  • how is SonarQube deployed: zip, Docker, Helm:- Zip
  • what are you trying to achieve: Scanning SQL files
  • what have you tried so far to achieve this:- Added Quality gate

Do not share screenshots of logs – share the text itself (bonus points for being well-formatted)!

We use SQL stored procedures across multiple projects and have implemented stored procedure scanning. However, we’re facing a challenge with code duplication detection. Several stored procedures contain identical SELECT statements and logic (shared across 3-4 different procedures). Our quality gate is configured to fail builds when duplicate code exceeds 10%, but this is problematic for SQL files since reusing common SELECT statements or logic across multiple stored procedures is a standard practice.

How can we configure the quality gate to detect duplication only within individual files while ignoring duplicated code that appears across different files?

Thanks

Sunil Bindra

Hey @sunil.bindra,

SonarQube’s duplication engine always looks for duplicated blocks across all files that are in the analysis scope for the project/branch; it isn’t possible to limit duplication detection strictly to “within a single file only” and ignore cross-file duplicates.

For cases like yours, the supported workaround is to exclude those files (or folders) from duplication detection, while still analyzing them for bugs, code smells, and security hotspots. You can do this either:

  • In the UI: go to Administration > General Settings > Analysis Scope > Duplication Exclusions on the project, and add path-matching patterns for the SQL files you want to ignore for duplication.
  • Or via analysis configuration: set sonar.cpd.exclusions to a comma-separated list of path patterns for those SQL files.

It’s not the first time we hear about this need from users, so I’m going to pass this on to the PMs in case they want to consider supporting this.