SQL Server存储过程创建具有动态列数的表

发布于 2025-01-02 00:07:22 字数 1307 浏览 1 评论 0原文

我是存储过程的新手,并试图弄清楚如何创建具有动态列数的表。

我正在使用 Microsoft SQL Server 2008。

我有以下查询,该查询旨在用于将从 XML 源传递的数据存储到应由该存储过程创建的表中。

CREATE PROCEDURE AddClaimData_newV2
    @xml_text VARCHAR(4000),
    @devYearColumnNumber INT
AS

  DECLARE @i INT
  DECLARE @tempTable TABLE (
       ProductName       VARCHAR(50), 
       Year              INT, 
       Value1            FLOAT , 
       Value2            FLOAT , 
       Value3            FLOAT , 
       Value4            FLOAT )

  EXEC sp_xml_preparedocument @i OUTPUT, @xml_text 

  INSERT INTO @tempTable
    SELECT * FROM 
      OPENXML(@i, '/root/Product/Year/Value1/Value2/Value3/Value4', 1) 
      WITH 
        (ProductName    VARCHAR(50) '../../../../../@Name', 
         Year     INT  '../../../../@Year', 
         Value1   FLOAT  '../../../@Value', 
         Value2   FLOAT  '../../@Value',
         Value3   FLOAT  '../@Value',
         Value4   FLOAT  '@Value')

/* create a new table and store all the data from @tempTable */

EXEC sp_xml_removedocument @i

基本上,ProductNameYear 是固定列,但 Value[i] 列由参数 devYearColumnNumber 决定。

我的问题是:

  1. 如何使用参数动态创建这些 Value[i]
  2. ,然后如何使用这些列创建一个新表来存储来自 @tempTable 的数据

I'm new to stored procedure, and trying to figure out how to create a table with a dynamic number of columns.

I'm using Microsoft SQL Server 2008.

I have the following query which is meant to be used to store data passed from a XML source into a table which should be created by this stored procedure.

CREATE PROCEDURE AddClaimData_newV2
    @xml_text VARCHAR(4000),
    @devYearColumnNumber INT
AS

  DECLARE @i INT
  DECLARE @tempTable TABLE (
       ProductName       VARCHAR(50), 
       Year              INT, 
       Value1            FLOAT , 
       Value2            FLOAT , 
       Value3            FLOAT , 
       Value4            FLOAT )

  EXEC sp_xml_preparedocument @i OUTPUT, @xml_text 

  INSERT INTO @tempTable
    SELECT * FROM 
      OPENXML(@i, '/root/Product/Year/Value1/Value2/Value3/Value4', 1) 
      WITH 
        (ProductName    VARCHAR(50) '../../../../../@Name', 
         Year     INT  '../../../../@Year', 
         Value1   FLOAT  '../../../@Value', 
         Value2   FLOAT  '../../@Value',
         Value3   FLOAT  '../@Value',
         Value4   FLOAT  '@Value')

/* create a new table and store all the data from @tempTable */

EXEC sp_xml_removedocument @i

Basically, ProductName and Year are fixed columns, but the Value[i] columns are decided by parameter devYearColumnNumber.

My questions are :

  1. how to use the parameter to dynamically create those Value[i] columns
  2. then, how to create a new table with those columns to store data from @tempTable

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

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

发布评论

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

评论(1

只是一片海 2025-01-09 00:07:22
declare @currentColumnNumber int, @sqlCommandToCreateTable nvarchar(4000)
set @currentColumnNumber = 1
set @sqlCommandToCreateTable = 'CREATE TABLE testClaimData (
        ProductName       VARCHAR(50),
        Year              INT '
while @currentColumnNumber <= @devYearColumnNumber
begin
    set @sqlCommandToCreateTable = @sqlCommandToCreateTable + ' , 
        Value' + convert(varchar, @currentColumnNumber) + '            FLOAT'
    set @currentColumnNumber = @currentColumnNumber + 1
end
set @sqlCommandToCreateTable = @sqlCommandToCreateTable + ' )'
exec sp_executeSql @sqlCommandToCreateTable

--[Test this with:]-- exec sp_executeSql N'select * from testClaimData'

--similar mechanism for the "INSERT INTO" here
declare @currentColumnNumber int, @sqlCommandToCreateTable nvarchar(4000)
set @currentColumnNumber = 1
set @sqlCommandToCreateTable = 'CREATE TABLE testClaimData (
        ProductName       VARCHAR(50),
        Year              INT '
while @currentColumnNumber <= @devYearColumnNumber
begin
    set @sqlCommandToCreateTable = @sqlCommandToCreateTable + ' , 
        Value' + convert(varchar, @currentColumnNumber) + '            FLOAT'
    set @currentColumnNumber = @currentColumnNumber + 1
end
set @sqlCommandToCreateTable = @sqlCommandToCreateTable + ' )'
exec sp_executeSql @sqlCommandToCreateTable

--[Test this with:]-- exec sp_executeSql N'select * from testClaimData'

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