SQL Server Unit Test (ssut - . ) xml, . , , . , <original_record_set><original_record /></original_record_set>, ,
<test_record_set><test_record /></ test_record_set >.
, , :
SET @output = (SELECT col1, col2
FROM @test_object_result
FOR xml path ( test_record '), root( test_record_set '));
:
SET @output = (SELECT col1, col2
FROM @test_object_result
FOR xml path ( original_record'), root( original_record_set '));
, SAME , " xml" path('...') root('...'), .
xml , node @relation_name @tuple_name. , .
, ! , . / , , , , , .
USE [unit_test];
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[standardize_record_set]') AND type IN ( N'FN', N'IF', N'TF', N'FS', N'FT' ))
DROP FUNCTION [dbo].[standardize_record_set];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
SET nocount ON;
GO
CREATE FUNCTION [dbo].[standardize_record_set] (@relation_name nvarchar(150)= N'record_set',
@tuple_name nvarchar(150)= N'record', @xml xml )
returns XML
AS
BEGIN
DECLARE
@attribute_index int = 1,
@attribute_count int = 0,
@record_set xml = N'<' + @relation_name + ' />',
@record_name nvarchar(50) = @tuple_name,
@builder nvarchar(max),
@record xml,
@next_record xml;
DECLARE @record_table TABLE (
record xml );
INSERT INTO @record_table
SELECT t.c.query('.') AS record
FROM @xml.nodes('/*/*') T(c);
DECLARE record_table_cursor CURSOR FOR
SELECT cast([record] AS xml)
FROM @record_table
OPEN record_table_cursor
FETCH NEXT FROM record_table_cursor INTO @next_record
WHILE @@FETCH_STATUS = 0
BEGIN
SET @attribute_index=1;
SET @attribute_count = @next_record.query('count(/*[1]/@*)').value('.', 'int');
SET @builder = N'<' + @record_name + N' ';
-- build up attribute string
WHILE @attribute_index <= @attribute_count
BEGIN
SET @builder = @builder + @next_record.value('local-name((/*/@*[sql:variable("@attribute_index")])[1])',
'varchar(max)') + '="' + @next_record.value('((/*/@*[sql:variable("@attribute_index")])[1])',
'varchar(max)') + '" ';
SET @attribute_index = @attribute_index + 1
END
-- build record and add to record_set
SET @record = @builder + ' />';
SET @record_set.modify('insert sql:variable("@record") into (/*)[1]');
FETCH NEXT FROM record_table_cursor INTO @next_record
END
CLOSE record_table_cursor;
DEALLOCATE record_table_cursor;
RETURN @record_set;
END;
GO