2

我想将返回特定用户定义数据类型的存储过程的结果转换回表。

这是基于这里提出的想法:http: //sqlblogcasts.com/blogs/danny/archive/2008/01/06/for-xml-and-back-again.aspx

因此,例如,如果我将类型声明为:

CREATE TYPE [dbo].[MyType]
AS TABLE 
(
    Id INT IDENTITY,
    MonthNumber INT,     
    YearNumber INT,      
    NumberOfOfficersMakingReferrals INT, 
    TierLevel NVARCHAR(10),                       
    TierStrengthTotal INT                
)
GO

像这样调用:

DECLARE @Data [dbo].[MyType]

INSERT INTO @Data
EXEC [dbo].[sp_MyProc]

SET @Xml = (SELECT * FROM @Data FOR XML AUTO)
SELECT  @Xml

-- Get a table back
EXEC uSpShredUserDefinedTableType @Xml, '[dbo].[MyType]'
GO

但是,我编写的 SP 返回 NULL,因为 XML 的 NODE 名称不会以 @UserTableType 的形式返回:

CREATE PROC dbo.uSpShredUserDefinedTableType @Xml XML, @UserTableType SYSNAME AS

       DECLARE @Sql NVARCHAR(MAX)

       SELECT @Sql = 'SELECT ' +
           STUFF((SELECT '      ,T.Data.value(''@' +
                         c.name + ''', ''' +
                         t.name +
                         CASE WHEN c.user_type_id IN (165,167,173,175,231,239)
                              THEN '(' + CONVERT(VARCHAR, c.max_length) + ')'
                              WHEN c.user_type_id IN (106, 108)
                              THEN '(' + CONVERT(VARCHAR, c.precision)
                                   + ', ' + CONVERT(VARCHAR, c.scale) + ')'
                              ELSE '' END +
                         ''') AS ' + c.name + CHAR(10)
                  FROM   sys.columns c
                         INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
                  WHERE  object_id IN (SELECT type_table_object_id
                                       FROM sys.table_types
                                       WHERE name = @UserTableType)
                         AND t.name !='xml'
                  FOR XML PATH('')), 1, 7, '') +
           'FROM   @Xml.nodes(''/' + @UserTableType + ''') T(Data)'


    EXEC sp_executesql @Sql, N'@Xml XML', @Xml = @Xml
GO

那么任何人都可以看到我在上面的存储过程中哪里出错了,以便我可以使其功能与 Danny 的示例相同,但对于用户定义的表类型?

[编辑:最终解决方案]

我不得不将 Devart 的解决方案修改为:

  • 正确处理“[”,因为 SQL 正在生成错误“'['附近的语法错误,预期“节点测试”。
  • 修复任何声明为 (MAX) 的生成,因为这会生成 NVARCHAR(-1)

过程:

CREATE PROC [generator].[sp_ShredUserDefinedTableType] 
    @AXml XML, 
    @AUserTableType SYSNAME
AS
   DECLARE @Sql NVARCHAR(MAX)
   SELECT @Sql = 'SELECT ' +
        STUFF((SELECT '      ,T.Data.value(''@' +
                        c.name + ''', ''' +
                        t.name +
                        CASE WHEN c.user_type_id IN (165,167,173,175,231,239)
                             THEN '(' + CASE (c.max_length)
                                             WHEN -1 THEN 'MAX'
                                        ELSE
                                           CONVERT(VARCHAR, c.max_length) 
                                        END + ')'
                            WHEN c.user_type_id IN (106, 108)
                            THEN '(' + CONVERT(VARCHAR, c.precision) + ', ' + CONVERT(VARCHAR, c.scale) + ')'
                        ELSE 
                           '' 
                        END + ''') AS ' + c.name + CHAR(10)
        FROM sys.table_types tt 
        JOIN sys.columns c ON tt.type_table_object_id = c.[object_id]
        JOIN sys.types t ON c.user_type_id = t.user_type_id
        WHERE SCHEMA_NAME(tt.[schema_id]) + '.' + tt.name = @AUserTableType
            AND t.name != 'xml'
        FOR XML PATH('')), 1, 7, '') +
        'FROM   @Xml.nodes(''/' + @AUserTableType + ''') T(Data)'

    -- Print out the SQL that is necessary to query the XML that has been generated
    PRINT @sql
GO

可以按如下方式调用:

DECLARE @Xml XML
DECLARE @Data [dbo].[t_sp_Report_AR102_DataRetrieval_V1_1_Result]

INSERT INTO @Data
EXEC [dbo].[sp_Report_AR102_DataRetrieval_V1_1]
     @AOfficerParticipationNumberOfPriorDays = 30
    ,@AStartDate = '2013-04-01' 
    ,@AEndDate = '2013-06-30' 
    ,@AReferringServiceID = 4
    ,@AExcludePresentingIssueIDs = ''
    ,@AResultAsXML = @Xml OUTPUT


SET @Xml = (SELECT * FROM @Data FOR XML raw('dbo.t_sp_Report_AR102_DataRetrieval_V1_1_Result'))
EXEC [generator].[sp_ShredUserDefinedTableType] @Xml, 'dbo.t_sp_Report_AR102_DataRetrieval_V1_1_Result'
4

2 回答 2

3

这里有几个问题:

  • 您的表是表变量,因此要获取架构,您必须查询sys.table_types
  • 当您选择时for xml for auto,您的节点元素名称将是 xml safe @Data- <_x0040_Data ...,所以我建议用户为xml path.

你的代码变成:

CREATE PROC dbo.uSpShredUserDefinedTableType @Xml XML, @UserTableType SYSNAME AS

       DECLARE @Sql NVARCHAR(MAX)

   SELECT @Sql = 'SELECT ' +
        STUFF((SELECT '      ,T.Data.value(''@' +
                        c.name + ''', ''' +
                        t.name +
                        CASE WHEN c.user_type_id IN (165,167,173,175,231,239)
                            THEN '(' + CONVERT(VARCHAR, c.max_length) + ')'
                            WHEN c.user_type_id IN (106, 108)
                            THEN '(' + CONVERT(VARCHAR, c.precision)
                                + ', ' + CONVERT(VARCHAR, c.scale) + ')'
                            ELSE '' END +
                        ''') AS ' + c.name + CHAR(10)
        FROM sys.table_types tt 
            JOIN sys.columns c ON tt.type_table_object_id = c.[object_id]
            JOIN sys.types t ON c.user_type_id = t.user_type_id
        WHERE tt.name = @UserTableType
            AND t.name != 'xml'
        FOR XML PATH('')), 1, 7, '') +
        'FROM   @Xml.nodes(''/' + @UserTableType + ''') T(Data)'

    --select @sql
    EXEC sp_executesql @Sql, N'@Xml XML', @Xml = @Xml
GO

你这样称呼它

SET @Xml = (SELECT * FROM @Data FOR XML raw('MyType'))

EXEC uSpShredUserDefinedTableType @Xml, 'MyType'

sql fiddle demo

于 2013-08-19T07:57:42.307 回答
2

试试这个——

   DECLARE @Xml XML, @UserTableType SYSNAME = '[dbo].[MyType]'
   DECLARE @Sql NVARCHAR(MAX)
   SELECT @Sql = 'SELECT ' +
        STUFF((SELECT '      ,T.Data.value(''@' +
                        c.name + ''', ''' +
                        t.name +
                        CASE WHEN c.user_type_id IN (165,167,173,175,231,239)
                            THEN '(' + CONVERT(VARCHAR, c.max_length) + ')'
                            WHEN c.user_type_id IN (106, 108)
                            THEN '(' + CONVERT(VARCHAR, c.precision)
                                + ', ' + CONVERT(VARCHAR, c.scale) + ')'
                            ELSE '' END +
                        ''') AS ' + c.name + CHAR(10)
        FROM sys.table_types tt 
        JOIN sys.columns c ON tt.type_table_object_id = c.[object_id]
        JOIN sys.types t ON c.user_type_id = t.user_type_id
        -- your mistake: [dbo].[MyType] != MyType          
        WHERE '[' + SCHEMA_NAME(tt.[schema_id]) + '].[' + tt.name + ']' = @UserTableType
            AND t.name != 'xml'
        FOR XML PATH('')), 1, 7, '') +
        'FROM   @Xml.nodes(''/' + @UserTableType + ''') T(Data)'

PRINT @Sql

输出 -

SELECT T.Data.value('@Id', 'int') AS Id
      ,T.Data.value('@MonthNumber', 'int') AS MonthNumber
      ,T.Data.value('@YearNumber', 'int') AS YearNumber
      ,T.Data.value('@NumberOfOfficersMakingReferrals', 'int') AS NumberOfOfficersMakingReferrals
      ,T.Data.value('@TierLevel', 'nvarchar(20)') AS TierLevel
      ,T.Data.value('@TierStrengthTotal', 'int') AS TierStrengthTotal
FROM   @Xml.nodes('/[dbo].[MyType]') T(Data)
于 2013-08-19T07:48:39.220 回答