sql 服务器从 xml 插入名为 table 的动态全局

sql server insert into dynamic global named table from from xml

我想将数据插入临时全局 table 动态命名为:

SET @SQL = '
CREATE TABLE '+Cast(@TableName as VARCHAR(60))+'
(
    Adr VARCHAR(1000)
)';
execute (@SQL);

我能够检索我需要的值,例如:

SELECT Recipient.query('.').value('.','varchar(15)')
FROM   @not.nodes('Data/MgRec' ) xmlData(ref) CROSS APPLY
    ref.nodes('Recipient') AS Recipients(Recipient) ;

哪个给我列出了一些值.. 现在,当我想插入这些值时:

SET @SQL = 'INSERT INTO '+ Cast(@TableName as VARCHAR(60))+' (Adr) 
 Select Recipient.query(''.'').value(''.'',''varchar(15)'')
FROM  '+ Cast(@not as VARCHAR(60))  +'.nodes(''Data/MgRec'' ) xmlData(ref) CROSS APPLY
    ref.nodes(''Recipient'') AS Recipients(Recipient)' ;
    execute (@SQL);
SET @SQL = 'select * from ' +Cast(@TableName as VARCHAR(60))
execute (@SQL);

我卡在这里了,错误:

Target string size is too small to represent the XML instance

有什么建议吗?

吼叫@not xml:

<Data>
  <MgRec>
    <Recipient>10800234</Recipient>
    <Recipient>24900005</Recipient>
    <Recipient>24900004</Recipient>
    <Recipient>10201026</Recipient>
    <Recipient>66600019</Recipient>
    <Recipient>14042243</Recipient>
  </MgRec>
</Data>

为什么不正确地参数化您的查询?

DECLARE @SchemaName sysname = N'dbo',
        @TableName sysname = N'YourTable';

DECLARE @SQL nvarchar(MAX);
SET @SQL = '
CREATE TABLE ' + QUOTENAME(@SchemaName) + N'.' + QUOTENAME(@TableName)+ N'
(
    Adr VARCHAR(1000)
)
INSERT INTO ' + QUOTENAME(@SchemaName) + N'.' + QUOTENAME(@TableName)+ N' (Adr)
SELECT Recipient.value(''(./text())[1]'',''varchar(15)'')
FROM @not.nodes(''Data/MgRec'' ) xmlData(ref)
     CROSS APPLY ref.nodes(''Recipient'') AS Recipients(Recipient) ;';
EXEC sys.sp_executesql @SQL, N'@not xml', @not;

db<>fiddle