@manhng

Welcome to my blog!

TransactSql.ScriptDom

August 20, 2020 08:56

TransactSql.ScriptDom (edit)

SQL Server Versions:

  • 80 = SQL Server 2000
  • 90 = SQL Server 2005
  • 100 = SQL Server 2008
  • 110 = SQL Server 2012
  • 120 = SQL Server 2014

Parsing T-SQL statements to tokens

  • Using the Microsoft.SqlServer.Management.SqlParser.dll
  • Using the Microsoft.SqlServer.TransactSql.ScriptDom.dll

https://stackoverflow.com/questions/30452864/parsing-t-sql-statements-to-tokens

https://www.sqlservercentral.com/blogs/parsing-t-sql-%E2%80%93-the-easy-way

https://www.andriescu.nl/sql/sql-how-to-parse-microsoft-transact-sql-statements-in-c_sharp_view_column_binding/

https://blogs.msmvps.com/bsonnino/2018/08/10/parsing-sql-server-code-with-c/

https://sqlblog.org/2011/11/03/the-case-against-information_schema-views

Microsoft® SQL Server® 2014 Feature Pack:
https://www.microsoft.com/en-us/download/details.aspx?id=42295

Microsoft® SQL Server® 2014 Transact-SQL Language Service: TSqlLanguageService.msi

The SQL Server Transact-SQL Language Service is a component based on the .NET
Framework which provides parsing validation and IntelliSense services for Transact-SQL for SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005. Filename: X86 and x64 Package(tsqllanguageservice.msi)

Add the following reference for 32 bit in your C# project:
C:\Program Files (x86)\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.TransactSql.ScriptDom.dll

100 SQL Server Tips & Tricks (HAY)

https://jalukadev.blogspot.com/2008/06/sql-server-programming-hacks-100-list.html

10 SQL Server Functions That You Do Not Know

http://sqlservercode.blogspot.com/2007/02/ten-sql-server-functions-that-you.html

Find all Primary Keys and Foreign Keys In A Database

http://sqlservercode.blogspot.com/

http://sqlservercode.blogspot.com/2006/01/top-5-sql-server-posts-for-december.html

SELECT t.table_schema AS PrimarySchemaName ,
t.TABLE_NAME AS PrimaryKeyTable,
tc.CONSTRAINT_NAME AS PrimaryKey,
COALESCE(tc2.constraint_schema,'N/A') AS ForeignSchemaName,
COALESCE(rc1.CONSTRAINT_NAME,'N/A') AS ForeignKey ,
COALESCE(tc2.TABLE_NAME,'N/A') AS ForeignKeyTable
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.TABLES t ON tc.TABLE_NAME = t.TABLE_NAME
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON tc.CONSTRAINT_NAME =rc1.UNIQUE_CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME
WHERE TC.CONSTRAINT_TYPE ='PRIMARY KEY'
ORDER BY tc.TABLE_NAME,tc.CONSTRAINT_NAME,rc1.CONSTRAINT_NAME
SELECT TC.CONSTRAINT_SCHEMA + '.'+ TC.TABLE_NAME AS PRIMARYKEYTABLE
,TC.CONSTRAINT_NAME AS PRIMARYKEY
,CU.COLUMN_NAME as [Primary Column Name]
,COALESCE(RC1.CONSTRAINT_NAME,'N/A') AS FOREIGNKEY
,coalesce(CFU.Column_Name, 'N/A') as [Foreign Column Name]
,CASE WHEN TC2.TABLE_NAME IS NULL THEN 'N/A'
ELSE TC.CONSTRAINT_SCHEMA + '.' + TC2.TABLE_NAME
END AS FOREIGNKEYTABLE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CU
on TC.TABLE_NAME = CU.TABLE_NAME
and TC.TABLE_SCHEMA = CU.TABLE_SCHEMA and Tc.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC1
ON TC.CONSTRAINT_NAME =RC1.UNIQUE_CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC2
ON TC2.CONSTRAINT_NAME =RC1.CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CFU
on RC1.CONSTRAINT_NAME = CFU.CONSTRAINT_NAME
WHERE TC.CONSTRAINT_TYPE ='PRIMARY KEY'
ORDER BY TC.TABLE_NAME,TC.CONSTRAINT_NAME,RC1.CONSTRAINT_NAME

Categories

Recent posts