0

I need to get attribute information (name, type, null/not null, constraints) from CREATE TABLE script, but I can't find any libraries or frameworks for it. I checked this one but this parser doesn't get information about table being created. Are there ways to do this without manual parsing?

My app doesn't use DB. The information should be extracted from the script text.

For example, from this script

CREATE TABLE [dbo].[person]
(
    [id] [int] NOT NULL ,
    [Name] [VARCHAR](1) NULL,
    [fk] [int] NULL,

    CONSTRAINT [PK_person] 
        PRIMARY KEY CLUSTERED ([id])
)

I need to extract the following information:

strgin tblName = ParseResult.TableName; //"person"
Attribute a0 = ParseResult.Attr[0]; //Attribute{"id", "int" "NOT NULL", "PK"}
Attribute a1 = ParseResult.Attr[1]; 
Attribute a2 = ParseResult.Attr[2];
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
KindFrog
  • 358
  • 4
  • 17
  • You need to parse schema information from the `CREATE` statement in C#? Is there a reason you can’t let your DBMS create the table and report on the schema for you? – D M May 24 '21 at 17:07
  • You can use GetSchema to get info on existing tables : https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.getschema?view=dotnet-plat-ext-5.0 – jdweng May 24 '21 at 17:08
  • 1
    @DM My app doesn't use DB. It should get the information from the script text. – KindFrog May 24 '21 at 17:12
  • Well it is difficult. You have even not specified which SQL dialect do you want to parse. Better to find library which can do that from real database. – Svyatoslav Danyliv May 24 '21 at 17:13
  • It might be worth looking at this Dan Guzman blog, it's complex though http://www.dbdelta.com/tag/t-sql/, further examples here https://devblogs.microsoft.com/azure-sql/programmatically-parsing-transact-sql-t-sql-with-the-scriptdom-parser/ – Steve Ford May 24 '21 at 22:17

0 Answers0