如何在SQL Server 2008函数中动态生成表列定义

发布于 2024-07-20 06:25:40 字数 1074 浏览 3 评论 0原文

在SQL Server 2008中,我有一种情况,我需要返回动态生成的表,并且列也是动态生成的。

其中的所有内容都是由查询生成的,因为我从一个 id 开始,然后获得列名称和类型,这就是强制转换的原因。

以下是最终查询,在一种情况下,它将返回我想要的表,但这个类似的查询可用于返回多个表。

目前这是在 C# 中实现的,但我希望它应该可以在存储过程或函数中实现,但是,我不确定如何进行多个查询来构建此查询,然后返回表。

谢谢。

SELECT ResultID, ResultName, ResultDescription, 
CAST([233] AS Real) as [ResultColA], 
CAST([234] AS Int) as [ResultColB], 
CAST([236] AS NVarChar) as [ResultColC], 
CAST([237] AS Int) as [ResultColD]
FROM (
    SELECT st.*, avt.ResultID as avtID, avt.SomeAttrID, avt.Value 
    FROM Result_AV avt 
    JOIN Result st ON avt.ResultID = st.ResultID) as p 
    PIVOT ( 
        MAX(Value) FOR AttributeID IN ([233], [234], [236], [237])) as pvt

更新:如果我有一个包含车辆制造商的表,并且我想要通用汽车制造的汽车的所有属性。 我会查找汽车的信息,用它来获取所有汽车制造商,即通用汽车,然后我想获取通用汽车制造的所有汽车的信息,但是,由于信息是动态生成的列是不同的。 这就是我所在的地方,需要动态生成该表。 我很好奇是否可以调用 Web 服务,获取动态生成的查询,然后让存储过程或函数执行该查询。

更新 2:获得此表后的下一步是,我需要将汽车上的所有选项相加(在本例中),以确定汽车的实际价格。 这就是为什么它需要采用表格格式。 此时,我正在查看 GM 制造的汽车,但我可以对 SeaRay 制造的船只进行另一个查询,即使船只上的表列与汽车上的表列不同,查询也应该工作相同。

IN SQL Server 2008 I have a situation where I need to return a dynamically generated table, and the columns are also dynamically generated.

Everything in this was generated by queries, as I started with one id, then I get the column names and types, which is why the casting.

Following is the final query, that will return the table I want, in one situation, but this similar query can be used to return several tables.

This is currently implemented in C# but I expect it should be doable in a stored procedure or function, but, I am not certain how I can make several queries to build up this query, to then return the table.

Thank you.

SELECT ResultID, ResultName, ResultDescription, 
CAST([233] AS Real) as [ResultColA], 
CAST([234] AS Int) as [ResultColB], 
CAST([236] AS NVarChar) as [ResultColC], 
CAST([237] AS Int) as [ResultColD]
FROM (
    SELECT st.*, avt.ResultID as avtID, avt.SomeAttrID, avt.Value 
    FROM Result_AV avt 
    JOIN Result st ON avt.ResultID = st.ResultID) as p 
    PIVOT ( 
        MAX(Value) FOR AttributeID IN ([233], [234], [236], [237])) as pvt

Update: If I have a table that has manufacturer of vehicles, and I want all the attributes of cars made by GM. I would look up the information for cars, use that to get all the manufacturers of cars, which would be GM, then I want to get the information of all the cars made by GM, but, since the information has been dynamically generated the columns are different.
That is the place I am at, needing to dynamically generate this table. I am curious if I can perhaps just call out to a webservice, get the dynamically generated query and then have the stored procedure or function execute that query.

Update 2: The next step after I get this table is that I will need to add up all the options on the car (in this case), to determine the actual price of the car. That is why it needs to be in a tabular format. At this point I am looking at cars made by GM, but I could do another query on boats made by SeaRay and the query should work the same even though the table columns on the boats are different than the cars were.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-07-27 06:25:40

您可以使用动态 SQL 来完成此操作。 您需要使用查询和/或元数据适当地构建两个列表,然后将它们插入到 SQL 中:

DECLARE @template AS varchar(MAX)

SET @template = '
SELECT ResultID, ResultName, ResultDescription
{@SELECT_COLUMN_LIST}
FROM (
    SELECT st.*, avt.ResultID as avtID, avt.SomeAttrID, avt.Value 
    FROM Result_AV avt 
    JOIN Result st ON avt.ResultID = st.ResultID) as p 
    PIVOT ( 
        MAX(Value) FOR AttributeID IN ({@PIVOT_COLUMN_LIST})) as pvt'

DECLARE @sql AS varchar(MAX)

SET @sql = REPLACE(REPLACE(@template, '{@SELECT_COLUMN_LIST}', @SELECT_COLUMN_LIST), '{@PIVOT_COLUMN_LIST}', @PIVOT_COLUMN_LIST)

EXEC (@sql)

要填充 @SELECT_COLUMN_LIST@PIVOT_COLUMN_LIST,您需要有几个选项,但类似这样的东西可以避免对游标或尴尬的字符串操作的任何需要:

SELECT @PIVOT_COLUMN_LIST = COALESCE(@PIVOT_COLUMN_LIST, '') + ',' + QUOTENAME(value)
FROM (SELECT DISTINCT value FROM table)

显然,对于@SELECT_COLUMN_LIST,您需要将此信息与一些元数据结合起来。

为了便于维护,我已经做了类似的事情,因为 PIVOT 和 UNPIVOT 不允许列表是动态 SQL(因为它们会导致输出表上的架构更改)。 对于不解释列的客户端报告样式或列表框用法,这并不重要。

You can do this with dynamic SQL. You need to build your two lists appropriately with a query and/or metadata and then insert them into the SQL:

DECLARE @template AS varchar(MAX)

SET @template = '
SELECT ResultID, ResultName, ResultDescription
{@SELECT_COLUMN_LIST}
FROM (
    SELECT st.*, avt.ResultID as avtID, avt.SomeAttrID, avt.Value 
    FROM Result_AV avt 
    JOIN Result st ON avt.ResultID = st.ResultID) as p 
    PIVOT ( 
        MAX(Value) FOR AttributeID IN ({@PIVOT_COLUMN_LIST})) as pvt'

DECLARE @sql AS varchar(MAX)

SET @sql = REPLACE(REPLACE(@template, '{@SELECT_COLUMN_LIST}', @SELECT_COLUMN_LIST), '{@PIVOT_COLUMN_LIST}', @PIVOT_COLUMN_LIST)

EXEC (@sql)

To populate the @SELECT_COLUMN_LIST and @PIVOT_COLUMN_LIST, you've got several options, but something like this avoids any need for a cursor or awkward string operations:

SELECT @PIVOT_COLUMN_LIST = COALESCE(@PIVOT_COLUMN_LIST, '') + ',' + QUOTENAME(value)
FROM (SELECT DISTINCT value FROM table)

Obviously for @SELECT_COLUMN_LIST you need to combine this information with some metadata.

I have done things like that for ease of maintenance because PIVOT and UNPIVOT do not allow lists to be dynamic SQL (because they result in a schema change on the output table). For for client-side report-style or listbox usage where the columns aren't interpreted, it doesn't matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文