在本快速入门中,你将了解业务逻辑解释器如何帮助开发人员了解和使用在 SQL、ORM(Object-Relational 映射)框架中实现的复杂应用程序逻辑,或直接在数据库中实现。 助手分析 SQL 代码、ORM 模型或现有数据库架构,以解释基础业务规则并提供可作的文档。
开始
确保已连接到数据库,并使用 MSSQL 扩展打开活动编辑器窗口。 通过此连接, @mssql 聊天参与者可以了解数据库环境的上下文,从而提供准确的上下文感知建议。 如果没有数据库连接,聊天参与者将没有架构或数据上下文来提供有意义的响应。
以下示例使用 AdventureWorksLT2022 示例数据库,可以从 Microsoft SQL Server 示例和社区项目 主页下载该数据库。
为了获得最佳结果,请调整表和架构名称以匹配自己的环境。
确保聊天包含 @mssql 前缀。 例如,键入 @mssql 后跟问题或提示。 这可确保聊天参与者了解你请求与 SQL 相关的帮助。
使用 GitHub Copilot 了解业务逻辑
GitHub Copilot 可以帮助你了解和解释嵌入在数据库代码、ORM 模型和应用程序查询中的业务规则。 从存储过程到 LINQ 查询和 Sequelize 表达式,GitHub Copilot 提供自然语言见解,使复杂的逻辑更易于访问。
下面是通过聊天参与者可以询问的常见用例和示例:
解释 T-SQL 逻辑
使用 GitHub Copilot 了解和解释 Transact-SQL(T-SQL)逻辑,包括从存储过程到内联条件语句。 无论是查看折扣规则、过程逻辑还是优化条件,GitHub Copilot 都可以分析和记录 T-SQL 中实现的业务规则。
说明存储过程
Explain what the `SalesLT.uspGetCustomerOrderHistory` stored procedure does and suggest ways to optimize it.
调试存储过程
Debug the `SalesLT.uspGetTopSellingProducts` stored procedure and suggest improvements.
解释代码片段中的业务逻辑
Analyze the following SQL code snippet from my current database. Document the business rules implemented in this discount application process, including conditions for eligibility, discount rate adjustments, and any limits imposed on the discount amount. Also, provide actionable insights or suggestions to improve clarity or performance if necessary.
DECLARE @OrderTotal AS DECIMAL (10, 2) = 1500.00;
DECLARE @DiscountCode AS NVARCHAR (20) = 'DISCOUNT10';
DECLARE @DiscountPct AS DECIMAL (5, 2) = CASE WHEN @OrderTotal > 1000.00 THEN 5.0 ELSE 0.0 END;
IF @DiscountCode = 'DISCOUNT10'
BEGIN
SET @DiscountPct = CASE WHEN @DiscountPct < 10.0 THEN 10.0 ELSE @DiscountPct END;
END
DECLARE @DiscountAmount AS DECIMAL (10, 2) = (@OrderTotal * @DiscountPct / 100.0);
IF @DiscountAmount > 200.00
BEGIN
SET @DiscountAmount = 200.00;
END
SELECT @OrderTotal AS OrderTotal,
@DiscountPct AS DiscountPercentage,
@DiscountAmount AS DiscountAmount;
解释 ORM 逻辑
解释 SQLAlchemy 查询
Explain what the following SQLAlchemy query does:
from sqlalchemy import func
top_customers = (
session.query(SalesOrderHeader.CustomerID, func.count().label("OrderCount"))
.group_by(SalesOrderHeader.CustomerID)
.order_by(func.count().desc())
.limit(10)
)
说明 Entity Framework LINQ 查询
What does this Entity Framework LINQ query do? Describe how it groups customers by tier based on their total purchases.
var customerTiers = context.SalesOrderHeaders
.GroupBy(o => o.CustomerID)
.Select(g => new {
CustomerID = g.Key,
TotalSpent = g.Sum(o => o.TotalDue),
Tier = g.Sum(o => o.TotalDue) >= 10000 ? "Gold" :
g.Sum(o => o.TotalDue) >= 5000 ? "Silver" : "Bronze"
});
解释 Prisma 查询中的业务逻辑
Analyze the logic of this Prisma query and explain how it determines which products are considered "low inventory".
const lowInventoryProducts = await prisma.product.findMany({
where: {
SafetyStockLevel: {
lt: 50
}
},
select: {
ProductID: true,
Name: true,
SafetyStockLevel: true
}
});
解释和注释 Sequelize 查询
Review and explain what this Sequelize query does. Add inline comments to clarify how it calculates total revenue per customer and filters for customers with significant spending:
const results = await SalesOrderHeader.findAll({
attributes: ['CustomerID', [sequelize.fn('SUM', sequelize.col('TotalDue')), 'TotalSpent']],
group: ['CustomerID'],
having: sequelize.literal('SUM(TotalDue) > 5000')
});
为产品列表生成 SQLAlchemy 查询
Using SQLAlchemy, generate a query to list products that have never been ordered and ask GitHub Copilot to explain the join logic and filtering behavior.
使用 Prisma 查询检索客户信息
In Prisma, write a query that retrieves customers who placed an order in the last 30 days. Explain what the following Prisma query does. Add inline comments to clarify how the date filtering works and how recent orders are determined:
通过查询了解业务意向
GitHub Copilot 帮助开发人员不仅了解查询的工作原理,还有助于开发人员了解查询存在的原因。 此说明包括数据筛选器、分组和聚合背后的实际用途。 这些解释在载入期间特别有用,使开发人员能够掌握 SQL 和 ORM 代码中嵌入的报表、逻辑门或系统指标背后的目标。
描述 T-SQL 查询中的业务目标
Describe the business goal of the following SQL query. What insight is it trying to surface?
SELECT TOP 10 CustomerID,
COUNT(*) AS OrderCount
FROM SalesLT.SalesOrderHeader
GROUP BY CustomerID
ORDER BY OrderCount DESC;
总结 T-SQL 查询的意图
Summarize what this query is intended to achieve from a business perspective.
SELECT ProductID,
SUM(LineTotal) AS TotalSales
FROM SalesLT.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) > 10000;
描述存储过程中的业务逻辑
Analyze the `SalesLT.uspGetCustomerOrderHistory` stored procedure and describe the business logic it implements.
介绍 Entity Framework LINQ 查询中的业务逻辑
Explain this Entity Framework LINQ query and describe what business logic it implements:
var highValueCustomers = context.SalesOrderHeaders
.Where(o => o.TotalDue > 1000)
.GroupBy(o => o.CustomerID)
.Select(g => new { CustomerID = g.Key, OrderCount = g.Count() })
.OrderByDescending(x => x.OrderCount)
.Take(10)
.ToList();
解释 Sequelize 查询中的业务假设
Using Sequelize, explain what this query does and describe any business assumptions it makes:
const customerRevenue = await SalesOrderHeader.findAll({
attributes: ['CustomerID', [sequelize.fn('SUM', sequelize.col('TotalDue')), 'TotalSpent']],
group: ['CustomerID'],
having: sequelize.literal('SUM(TotalDue) > 5000')
});
共享您的体验
为了帮助我们优化和改进 MSSQL 扩展的 GitHub Copilot,请使用以下 GitHub 问题模板提交反馈: GitHub Copilot 反馈
提交反馈时,请考虑包括:
测试方案 – 告诉我们你关注哪些领域,例如架构创建、查询生成、安全性、本地化。
效果良好 - 描述任何感觉流畅、有用或超出预期的体验。
问题或漏洞 – 包括任何问题、不一致或令人困惑的行为。 屏幕截图或屏幕录制特别有用。
改进建议 - 分享改进可用性、扩大覆盖范围或增强 GitHub Copilot 响应的想法。