[英]How can I get queries in a SQL Server stored procedure text using antrl4 lexer and parser?
I want to get queries in a SQL Server stored procedure text using antrl4 lexer and parser. 我想使用antrl4 lexer和解析器在SQL Server存储过程文本中获取查询。 For example, I have procedure text as follows:
例如,我的过程文本如下:
ALTER PROCEDURE [dbo].[BookUpdate]
@Name nvarchar(max),
@BookId int,
@PublishingHouse nvarchar(255),
@PublicationYear smallint,
@Authors nvarchar(255),
@Description nvarchar(max),
@BookShelf int,
@UserId int,
@Cover varbinary(max),
@Id int OUT
AS
BEGIN
DECLARE @haveDouble int = 0
DECLARE @haveUser int = 1
SELECT @haveDouble = Id
FROM dbo.Books
WHERE Name = @Name
AND Authors = @Authors
AND PublicationYear = @PublicationYear
AND Id != @BookId
IF @UserId > 0
BEGIN
SELECT @haveUser = UserId
FROM dbo.Users
WHERE UserId = @UserId
END
SET @Id = 0
IF @haveDouble = 0 AND @haveUser > 0
BEGIN
UPDATE dbo.Books
SET [Name] = @Name,
[PublishingHouse] = @PublishingHouse,
[PublicationYear] = @PublicationYear,
[Authors] = @Authors,
[Description] = @Description,
[BookShelf] = @BookShelf,
[Cover] = @Cover,
[UserId] = @UserId
WHERE
Id = @BookId
SET @Id = @BookId
END
END
I want to get positions of queries "DECLARE @haveDouble int = 0", "DECLARE @haveUser int = 1" and others. 我想获取查询位置“ DECLARE @haveDouble int = 0”,“ DECLARE @haveUser int = 1”等。 How can I do that?
我怎样才能做到这一点?
[1] SQL Server comes with a dedicated API for parsing T-SQL source coude, API included within Microsoft.SqlServer.TransactSql.ScriptDom
namespace. [1] SQL Server带有用于解析T-SQL源代码的专用API,该API包含在
Microsoft.SqlServer.TransactSql.ScriptDom
命名空间中。 This namespace include parsers for every SQL Server version starting with SQL Server 2000 (= TSql80Parser
). 此命名空间包括从SQL Server 2000(=
TSql80Parser
)开始的每个SQL Server版本的解析器。
[3] You have to find all TSqlStatement
s having DeclareVariableStatement
type (see DeclareVariableStatement Class ). [3]你必须找到所有
TSqlStatement
小号具有DeclareVariableStatement
类型(见DeclareVariableStatement类 )。 This class has following properties (among other properties): StartLine
, StartColumn
, StartOffset
, FirstTokenIndex
. 此类具有以下属性(除其他属性外):
StartLine
, StartColumn
, StartOffset
, FirstTokenIndex
。
[4] Example: [4]范例:
using Microsoft.SqlServer.TransactSql.ScriptDom;
....
TSql120Parser SqlParser = new TSql120Parser(false);
IList<ParseError> parseErrors;
TSqlFragment result = SqlParser.Parse(new StringReader(SqlTextBox.Text),
out parseErrors);
TSqlScript SqlScript = result as TSqlScript;
foreach (TSqlBatch sqlBatch in SqlScript.Batches)
{
foreach (TSqlStatement sqlStatement in sqlBatch.Statements)
{
ProcessViewStatementBody(sqlStatement);
}
}
Source: http://www.andriescu.nl/sql/sql-how-to-parse-microsoft-transact-sql-statements-in-c_sharp_view_column_binding/ 来源: http : //www.andriescu.nl/sql/sql-how-to-parse-microsoft-transact-sql-statements-in-c_sharp_view_column_binding/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.