适用于:Microsoft Fabric 中的✅ 仓库
在本教程中,你将学习如何使用T-SQL内置 的AI函数 ,用自然语言推理仓库表数据。 使用 dbo.fact_sale 表格探索简单查询中的 SELECT AI功能,在更高级的分析查询中使用,并将结果持久化为列。
用AI功能探索数据
在这个任务中,学习如何在简单 SELECT 查询中使用内置的AI功能,探索它们返回的内容,而无需更改表格。
请确保在 第一个教程中创建的工作区 处于打开状态。
从描述中提取范畴
在 “主页 ”功能区上,选择“ 新建 SQL 查询”。
在查询编辑器中,粘贴以下代码。 该代码使用
AI_EXTRACT从例如Alien officer hoodie (Black) XXL这类描述中提取颜色和大小。AI_EXTRACT返回一个JSON对象,所以可以把OPENJSON属性分成独立的列。-- Extract color and size attributes. SELECT SaleKey, Description, extracted.Color, extracted.Size FROM dbo.fact_sale CROSS APPLY OPENJSON(AI_EXTRACT(Description, 'Color', 'Size')) WITH (Color varchar(50), Size varchar(20)) AS extracted;运行该查询。
执行完成后,将查询重命名为
Color & Size Extraction。 确认Color和Size被分开列填充。
将描述格式化为 HTML
创建一个新查询,粘贴以下代码,然后运行。 该代码使用
AI_GENERATE_RESPONSE将Description改写为包含关键属性列表的 HTML 产品摘要,可直接在网页上渲染。-- Generate a catalog description formatted as HTML, with a summary and an attribute list. SELECT SaleKey, Description, AI_GENERATE_RESPONSE( 'Rewrite as a short e-commerce catalog description. Format the result as HTML: wrap the summary in a <p> tag, then list key attributes as a <dl>, with each <dt> label bolded using <strong> and followed by its <dd> value.', Description ) AS CatalogDescription FROM dbo.fact_sale;将查询重命名为
Short Catalog Description Generation。 检查CatalogDescription并确认其中包含所需的<p>、<dl>、<dt>和<dd>元素。
总结销售项目信息
创建一个新查询,粘贴以下代码,然后运行。 该代码将
AI_GENERATE_RESPONSE数字列(而非Description)转换为销售项目的纯文本摘要。-- Summarize each sales item in plain text, based on numeric columns only. DECLARE @prompt nvarchar(max) = N'Summarize this sales item in one short sentence for a business report.'; SELECT *, AI_GENERATE_RESPONSE( @prompt, CONCAT( 'Package: ', Package, '. Quantity: ', Quantity, '. Dry items: ', TotalDryItems, '. Chiller items: ', TotalChillerItems, '. Unit price: ', UnitPrice, '. Tax rate: ', TaxRate, '. Profit: ', Profit, '. Total including tax: ', TotalIncludingTax ) ) AS SalesItemSummary FROM dbo.fact_sale;将查询重命名为
Sales Item Summary Generation。 读几个SalesItemSummary数值,确认它们是否反映同一行的数值列。
确定每个销售项目的优先级
在此任务中,使用 AI_GENERATE_RESPONSE 更高级的查询,结合多个列以支持业务决策,而不是只针对单一列进行推理。
创建一个新查询,粘贴以下代码,然后运行。 该代码将发票日期、交货日期、参考日期、行总额、利润和需要冷藏处理的冷藏设备数量合并为一个输入,然后要求AI返回一个优先级分数,将这些信号合并成一个数字。
fact_sale它保存了历史销售,所以@AsOfDate可以代替你在实时履约队列中使用的当前营业日期。-- Score sales items for fulfillment priority based on invoice-to-delivery time, line total, profit, and chiller items. DECLARE @AsOfDate date = '2016-05-01'; DECLARE @prompt nvarchar(max) = N'Given the invoice date, delivery date, reference date, line total, profit, and number of chiller items below, return a single integer priority score from 1 (low) to 100 (urgent) for sales-item fulfillment. Consider how much time was allowed from invoice to delivery, and how close the delivery date is to the reference date. Larger line totals, closer delivery dates, higher profit, and more chiller items should raise the score. Return only the number.'; SELECT SaleKey, InvoiceDateKey, DeliveryDateKey, TotalIncludingTax, Profit, TotalChillerItems, TRY_CAST(AI_GENERATE_RESPONSE( @prompt, CONCAT( 'Invoice date: ', CONVERT(char(10), InvoiceDateKey, 23), '. Delivery date: ', CONVERT(char(10), DeliveryDateKey, 23), '. Reference date: ', CONVERT(char(10), @AsOfDate, 23), '. Line total: ', TotalIncludingTax, '. Profit: ', Profit, '. Chiller items: ', TotalChillerItems ) ) AS int) AS PriorityScore FROM dbo.fact_sale ORDER BY PriorityScore DESC;将查询重命名为
Sales Item Fulfillment Priority Scoring。 确认PriorityScore值介于 1 到 100 之间,并按值从高到低排序。
持久化AI结果
到目前为止,示例返回的 AI 结果为 SELECT。 为了复用这些结果,避免每次都重新计算,可使用 UPDATE 将其写回。
修正描述中的语法错误
创建一个新查询,粘贴以下代码,然后运行。 代码使用
AI_FIX_GRAMMAR就地修正Description,如果 AI 没有可修正的内容,则保留原始文本。-- Fix grammar issues in the description, in place. UPDATE dbo.fact_sale SET Description = ISNULL(AI_FIX_GRAMMAR(Description), Description) WHERE Description IS NOT NULL;将查询重命名为
Grammar Correction Update。 对比已更改Description的几行,并确认更正是否准确。
生成新列
该任务首先添加两个目标列,然后在一次更新中填充它们。
创建一个新查询,粘贴以下代码,然后运行。 该代码增加了用于翻译描述和产品类别的列。
-- Add columns for the translated description and the product category. ALTER TABLE dbo.fact_sale ADD DescriptionFr nvarchar(max) NULL, ProductCategory nvarchar(50) NULL;将查询重命名为
Add Translation & Category Columns。创建一个新查询,粘贴以下代码,然后运行。 该代码在一次更新中填充了两列,使用
AI_TRANSLATE和DescriptionAI_CLASSIFY将每行排序为固定的产品类别,这些类别与表中实际内容相符,如新奇杯、服装和包装用品。-- Populate the translated description and product category columns. UPDATE dbo.fact_sale SET DescriptionFr = AI_TRANSLATE(Description, 'fr'), ProductCategory = AI_CLASSIFY( Description, 'Apparel', 'Footwear', 'Novelty & Gifts', 'Packaging Materials', 'Toys & Costumes', 'Office Supplies' ) WHERE Description IS NOT NULL;将查询重命名为
Populate Translation & Category Columns。 确认每一行的DescriptionFr和ProductCategory都已填写。