本文介紹了Sql SERVER從FORM XML插入動態全局命名表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我要將數據插入到動態名為:
的臨時全局表中
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);
這里我被卡住了,錯誤:
目標字符串大小太小,無法表示XML實例
有什么建議嗎?
Bellow@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
這篇關于Sql SERVER從FORM XML插入動態全局命名表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,