{"id":19907485,"url":"https://github.com/bocharovf/tsqlcustomcodeanalysis","last_synced_at":"2026-05-13T17:37:59.520Z","repository":{"id":91050617,"uuid":"107900581","full_name":"bocharovf/TSqlCustomCodeAnalysis","owner":"bocharovf","description":"Custom code analysis rules for T-SQL","archived":false,"fork":false,"pushed_at":"2017-12-04T18:44:44.000Z","size":1183,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-11T22:14:28.578Z","etag":null,"topics":["codeanalysis","sqlserver","tsql","visual-studio"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bocharovf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-22T20:33:24.000Z","updated_at":"2017-11-05T09:40:18.000Z","dependencies_parsed_at":"2023-03-01T17:00:12.956Z","dependency_job_id":null,"html_url":"https://github.com/bocharovf/TSqlCustomCodeAnalysis","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bocharovf%2FTSqlCustomCodeAnalysis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bocharovf%2FTSqlCustomCodeAnalysis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bocharovf%2FTSqlCustomCodeAnalysis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bocharovf%2FTSqlCustomCodeAnalysis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bocharovf","download_url":"https://codeload.github.com/bocharovf/TSqlCustomCodeAnalysis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241335590,"owners_count":19946085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codeanalysis","sqlserver","tsql","visual-studio"],"created_at":"2024-11-12T20:41:10.050Z","updated_at":"2025-11-23T21:03:32.547Z","avatar_url":"https://github.com/bocharovf.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About\n\nThe project demonstrate ability to extend default code analysis (CA) capabilities in VS database projects with custom CA rules. It also contains CLR user-defined function (UDF) to run code analysis directly from T-SQL.\n\n# Content\n\nThe solution consists of projects:\n* **SqlCodeAnalysisRules** - custom CA rule for VS database project. See \"Run custom CA rule in Visual Studio\".\n* **SqlClrAnalysisObjects** - CLR UDF to run code analysis from T-SQL. See \"Run custom CA rule from T-SQL\".\n* **SqlAnalysisCommon** - shared library with generic rule logic and resources.\n* **SampleDBWithCA** - sample database project with CA enabled. Just to demonstrate custom CA rule.\n* WebScraper - secondary project used to parse microsoft documentation and determine supported SQL Server versions for each function.\n\n## Run custom CA rule in Visual Studio\n1. Install SQL Server Data Tools\n3. Build solution\n4. Copy both SqlAnalysisCommon.dll and SqlAnalysisCommon.dll to VS extension folder:\n%Visual Studio Install Dir%\\Common7\\IDE\\Extensions\\Microsoft\\SQLDB\\DAC\\140\\Extensions\\\n5. Restart Visual Studio\n6. Run code analysis for SampleDBWithCA project.\n7. Check custom CA rule warnings for TestStoredProcedure\n\n## Run custom CA rule from T-SQL\n\n1. Enable CLR in SQL Server:\n```\nsp_configure 'show advanced options', 1;  \nGO  \nRECONFIGURE;  \nGO  \nsp_configure 'clr enabled', 1;  \nGO  \nRECONFIGURE;  \nGO \n```\n2. Set database trustworthy:\n```\nALTER DATABASE CURRENT SET TRUSTWORTHY ON;\n```\n3. Create required assemblies:\n```\nCREATE ASSEMBLY [Microsoft.SqlServer.TransactSql.ScriptDom]\nFROM '... path to Microsoft.SqlServer.TransactSql.ScriptDom'\nWITH PERMISSION_SET = UNSAFE;\n\nCREATE ASSEMBLY [SqlAnalysisCommon]\nFROM '... path to SqlAnalysisCommon.dll'\nWITH PERMISSION_SET = UNSAFE;\n\nCREATE ASSEMBLY [SqlClrAnalysisObjects]\nFROM '... path to SqlClrAnalysisObjects.dll'\nWITH PERMISSION_SET = UNSAFE;\n```\n4. Create UDF:\n```\nCREATE OR ALTER FUNCTION [dbo].[GetCodeAnalysisRuleErrors](@ruleName [nvarchar](max), @sqlText [nvarchar](max))\nRETURNS  TABLE (\n\t[StartLine] [int] NULL,\n\t[StartColumn] [int] NULL,\n\t[Code] [nvarchar](4000) NULL,\n\t[Description] [nvarchar](4000) NULL\n) WITH EXECUTE AS CALLER\nAS \nEXTERNAL NAME [SqlClrAnalysisObjects].[UserDefinedFunctions].[GetCodeAnalysisRuleErrors]\n```\n5. Run sample query to analyse all database objects:\n```\nSELECT \n\tQUOTENAME(OBJECT_SCHEMA_NAME(modules.object_id)) + '.' +\n\tQUOTENAME(OBJECT_NAME(modules.object_id)) ObjectName, \n\terrors.StartLine, \n\terrors.StartColumn, \n\terrors.Code, \n\terrors.[Description]\nFROM sys.sql_modules modules\nCROSS APPLY dbo.GetCodeAnalysisRuleErrors('IncompatibleFunctionsSql2012Rule', modules.[definition]) errors \n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbocharovf%2Ftsqlcustomcodeanalysis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbocharovf%2Ftsqlcustomcodeanalysis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbocharovf%2Ftsqlcustomcodeanalysis/lists"}