如何在c#a程序中获得所有T-SQL保留关键字的列表

本文关键字:T-SQL 保留 关键字 列表 c#a 程序 | 更新日期: 2023-09-27 18:26:56

我希望在C#程序的运行时有一个所有t-sql保留关键字的列表。

bool iskeyword = IsSqlReservedKeyword("string");

list<string> ReservedKeywordList = new list<string>();
ReservedKeywordList = GetSQLReservedKeywords();

和C#中一样,还有一个方法IsValidIdentifier("string")。SQL类型是否有类似的方法?

如何在c#a程序中获得所有T-SQL保留关键字的列表

MSDN上列出了所有保留的(以及将来可能保留的)关键字。您需要设置一个属性,以便自己返回此单词列表。有点不确定,但你只需要做一次。

我可能会写一个Perl或Python脚本来解析该页面并为我创建属性。这可能需要更长的时间,但比手动操作更有趣,而且在这个过程中可能会学到一些东西。

这是C#中的列表(您可以在这里找到列表:https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql):

List<string> reservedKeywords = new List<string>()
{
    { "ADD" },
    { "EXTERNAL" },
    { "PROCEDURE" },
    { "ALL" },
    { "FETCH" },
    { "PUBLIC" },
    { "ALTER" },
    { "FILE" },
    { "RAISERROR" },
    { "AND" },
    { "FILLFACTOR" },
    { "READ" },
    { "ANY" },
    { "FOR" },
    { "READTEXT" },
    { "AS" },
    { "FOREIGN" },
    { "RECONFIGURE" },
    { "ASC" },
    { "FREETEXT" },
    { "REFERENCES" },
    { "AUTHORIZATION" },
    { "FREETEXTTABLE" },
    { "REPLICATION" },
    { "BACKUP" },
    { "FROM" },
    { "RESTORE" },
    { "BEGIN" },
    { "FULL" },
    { "RESTRICT" },
    { "BETWEEN" },
    { "FUNCTION" },
    { "RETURN" },
    { "BREAK" },
    { "GOTO" },
    { "REVERT" },
    { "BROWSE" },
    { "GRANT" },
    { "REVOKE" },
    { "BULK" },
    { "GROUP" },
    { "RIGHT" },
    { "BY" },
    { "HAVING" },
    { "ROLLBACK" },
    { "CASCADE" },
    { "HOLDLOCK" },
    { "ROWCOUNT" },
    { "CASE" },
    { "IDENTITY" },
    { "ROWGUIDCOL" },
    { "CHECK" },
    { "IDENTITY_INSERT" },
    { "RULE" },
    { "CHECKPOINT" },
    { "IDENTITYCOL" },
    { "SAVE" },
    { "CLOSE" },
    { "IF" },
    { "SCHEMA" },
    { "CLUSTERED" },
    { "IN" },
    { "SECURITYAUDIT" },
    { "COALESCE" },
    { "INDEX" },
    { "SELECT" },
    { "COLLATE" },
    { "INNER" },
    { "SEMANTICKEYPHRASETABLE" },
    { "COLUMN" },
    { "INSERT" },
    { "SEMANTICSIMILARITYDETAILSTABLE" },
    { "COMMIT" },
    { "INTERSECT" },
    { "SEMANTICSIMILARITYTABLE" },
    { "COMPUTE" },
    { "INTO" },
    { "SESSION_USER" },
    { "CONSTRAINT" },
    { "IS" },
    { "SET" },
    { "CONTAINS" },
    { "JOIN" },
    { "SETUSER" },
    { "CONTAINSTABLE" },
    { "KEY" },
    { "SHUTDOWN" },
    { "CONTINUE" },
    { "KILL" },
    { "SOME" },
    { "CONVERT" },
    { "LEFT" },
    { "STATISTICS" },
    { "CREATE" },
    { "LIKE" },
    { "SYSTEM_USER" },
    { "CROSS" },
    { "LINENO" },
    { "TABLE" },
    { "CURRENT" },
    { "LOAD" },
    { "TABLESAMPLE" },
    { "CURRENT_DATE" },
    { "MERGE" },
    { "TEXTSIZE" },
    { "CURRENT_TIME" },
    { "NATIONAL" },
    { "THEN" },
    { "CURRENT_TIMESTAMP" },
    { "NOCHECK" },
    { "TO" },
    { "CURRENT_USER" },
    { "NONCLUSTERED" },
    { "TOP" },
    { "CURSOR" },
    { "NOT" },
    { "TRAN" },
    { "DATABASE" },
    { "NULL" },
    { "TRANSACTION" },
    { "DBCC" },
    { "NULLIF" },
    { "TRIGGER" },
    { "DEALLOCATE" },
    { "OF" },
    { "TRUNCATE" },
    { "DECLARE" },
    { "OFF" },
    { "TRY_CONVERT" },
    { "DEFAULT" },
    { "OFFSETS" },
    { "TSEQUAL" },
    { "DELETE" },
    { "ON" },
    { "UNION" },
    { "DENY" },
    { "OPEN" },
    { "UNIQUE" },
    { "DESC" },
    { "OPENDATASOURCE" },
    { "UNPIVOT" },
    { "DISK" },
    { "OPENQUERY" },
    { "UPDATE" },
    { "DISTINCT" },
    { "OPENROWSET" },
    { "UPDATETEXT" },
    { "DISTRIBUTED" },
    { "OPENXML" },
    { "USE" },
    { "DOUBLE" },
    { "OPTION" },
    { "USER" },
    { "DROP" },
    { "OR" },
    { "VALUES" },
    { "DUMP" },
    { "ORDER" },
    { "VARYING" },
    { "ELSE" },
    { "OUTER" },
    { "VIEW" },
    { "END" },
    { "OVER" },
    { "WAITFOR" },
    { "ERRLVL" },
    { "PERCENT" },
    { "WHEN" },
    { "ESCAPE" },
    { "PIVOT" },
    { "WHERE" },
    { "EXCEPT" },
    { "PLAN" },
    { "WHILE" },
    { "EXEC" },
    { "PRECISION" },
    { "WITH" },
    { "EXECUTE" },
    { "PRIMARY" },
    { "WITHIN GROUP" },
    { "EXISTS" },
    { "PRINT" },
    { "WRITETEXT" },
    { "EXIT" },
    { "PROC" },
    { "RELEASE" }
};

您需要创建一个列表,并通过将所有字符串放入该列表来填充该列表。你可以从迈克尔给出的路径中选择字符串。类似于:

private List<String> CreateSQLReservedKeywordList()
{
  List<String> list = new List<String>();
  list.Add("Exists");
  list.Add("RaiseError");
  ...
}

出于高级目的,您可以创建excel或CSV,并从这些文件中填充列表。