Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Sql150ScriptGenerator.GenerateScript ignores WHERE statement

未关闭
#79 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp, sql
领域
databases

调研方向

首先使用 issue 正文中的 TSql150Parser、QuerySpecification 和 Sql150ScriptGenerator.GenerateScript 进行复现。将解析后的 QuerySpecification 与生成的输出进行比较,然后运行示例,确认格式化后的 SQL 中保留了 WHERE 子句。

由索引模型根据 Issue 内容生成。

描述

I try to format my sql script using GenerateScript method but WHERE clause is missing from its output.
Nuget version Microsoft.SqlServer.TransactSql.ScriptDom 161.9109.0.
Under debug I see parsed WHERE statement.

input and output:

ORIGINAL:
SELECT
        x2_0.KeyPowerPlantUnit
   ,x3_0.KeyEnergyPlantTypeParent
   ,x2_0.KeyEnergyPlantType
   ,x2_0.MostRecentSequence
FROM SNLEdit_new.dbo.PowerPlantUnitFuel x2_0
LEFT JOIN Lookup.dbo.EnergyPlantType x3_0
        ON x2_0.KeyEnergyPlantType = x3_0.KeyEnergyPlantType
                AND x3_0.updoperation < 2
WHERE x2_0.MostRecentSequence IN (1, 2, 3, 4, 5, 6);


FORMATTED:
SELECT x2_0.KeyPowerPlantUnit,
       x3_0.KeyEnergyPlantTypeParent,
       x2_0.KeyEnergyPlantType,
       x2_0.MostRecentSequence
FROM SNLEdit_new.dbo.PowerPlantUnitFuel AS x2_0
     LEFT OUTER JOIN
     Lookup.dbo.EnergyPlantType AS x3_0
     ON x2_0.KeyEnergyPlantType = x3_0.KeyEnergyPlantType
        AND x3_0.updoperation < 2

Code to reproduce:

using Microsoft.SqlServer.TransactSql.ScriptDom;
using System.Diagnostics;

var sql = @"
SELECT
	x2_0.KeyPowerPlantUnit
   ,x3_0.KeyEnergyPlantTypeParent
   ,x2_0.KeyEnergyPlantType
   ,x2_0.MostRecentSequence
FROM SNLEdit_new.dbo.PowerPlantUnitFuel x2_0
LEFT JOIN Lookup.dbo.EnergyPlantType x3_0
	ON x2_0.KeyEnergyPlantType = x3_0.KeyEnergyPlantType
		AND x3_0.updoperation < 2
WHERE x2_0.MostRecentSequence IN (1, 2, 3, 4, 5, 6);
";

Console.WriteLine("ORIGINAL:" + sql);

var reader = new StringReader(sql);
var parser = new TSql150Parser(true);

var fragment = (TSqlScript)parser.Parse(reader, out var errors);

if (errors.Count > 0)
{
    Console.WriteLine("Failed to parse SQL script:");
    foreach (var error in errors)
    {
        Console.WriteLine(error.Message);
    }

    Debugger.Break();
}

var options = new SqlScriptGeneratorOptions
{
    IncludeSemicolons = true,
    IndentationSize = 4
};
var generator = new Sql150ScriptGenerator(options);

var statement = (SelectStatement)fragment.Batches[0].Statements[0];

var expression = (QuerySpecification)statement.QueryExpression;

generator.GenerateScript(expression, out var formattedSql);

Console.WriteLine();
Console.WriteLine("FORMATTED:" + Environment.NewLine + formattedSql);
Console.ReadLine();
主要语言
GAP
星标
277
派生
43
平均合并
6 天 17 小时
30 天内合并 PR
3

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/SqlScriptDOM 的其他 Issue

查看 microsoft/SqlScriptDOM 的全部 Issue

相似的 Issue

更多 Databases Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。