我在Sql中使用Xml数据类型不太好, 我有一个名为RSN_ALL的表, 我需要从我的One Xml文件中向该表插入数据..为此我创建了一个存储过程。
ALTER procedure [dbo].[SPInsertXmlData]
(
@xmlData XML
)
as
begin
insert into RSN_All
(
Batch_M_id,
RSN,
Parent_RSN,
Pkg_Location,
CompanyId
)
SELECT
COALESCE
([Table].[Column].value('Batch_M_id[1]', 'bigint'),0) as 'Batch_M_id',
[Table].[Column].value('RSN [1]', 'varchar(20)') as ' RSN ',
[Table].[Column].value(' Parent_RSN[1]', 'varchar(20)') as ' Parent_RSN',
[Table].[Column].value(' Pkg_Location [1]', 'nvarchar(100)') as ' Pkg_Location',
[Table].[Column].value(' CompanyId [1]', 'int') as ' CompanyId'
FROM @xmlData.nodes('/ NewDataSet / Table') as [Table]([Column])
IF(@@ROWCOUNT > 0 )
select 'SUCCESS' as msg
end
它完美无缺,
但我需要一些修改我想检查这一行是否已经存在而不是更新该行其他明智的插入行。 (RSN列是我的唯一列),
我没有得到如何做到这一点,请帮助我
根据@Hoots回复这是我的新商店程序
ALTER procedure [dbo].[SPInsertXmlData]
(
@xmlData XML
)
as
begin
-- import xml into temp table
SELECT
COALESCE
([Table].[Column].value('Batch_M_id[1]', 'bigint'),0) as 'Batch_M_id',
[Table].[Column].value('RSN [1]', 'varchar(20)') as ' RSN ',
[Table].[Column].value(' Parent_RSN[1]', 'varchar(20)') as ' Parent_RSN',
[Table].[Column].value(' Pkg_Location [1]', 'nvarchar(100)') as ' Pkg_Location',
[Table].[Column].value(' CompanyId [1]', 'int') as ' CompanyId'
into #temp_xml
FROM @xmlData.nodes('/ NewDataSet / Table') as [Table]([Column])
BEGIN TRY
-- start the transaction
BEGIN TRANSACTION
-- now do the updates
update ra
set
ra.Batch_M_id =tx. Batch_M_id,
ra.RSN=tx.RSN ,
ra.Parent_RSN =tx.Parent_RSN,
ra.Pkg_Location=tx.Pkg_Location,
ra.CompanyId= tx.CompanyId
from RSN_ALL ra
inner join #temp_xml tx on (tx.RSN = ra.RSN ) -- the fields that identify existence
select 'Ok' as msg
-- now do the inserts
insert into RSN_All (Batch_M_id,RSN,Parent_RSN,Pkg_Location,CompanyId)
select *
from #temp_xml tx
where not exists (select 1 from RSN_All ra where tx.RSN= ra.RSN) -- the same fields that identify existence
-- commit the transaction
COMMIT TRANSACTION
END TRY
BEGIN CATCH
-- Any problems rollback transaction
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
END CATCH;
-- drop temp table
drop table #temp_xml
end
但仍然无法正常工作.. 它显示错误无效列名称'RSN',这是我的XML文件
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<Batch_M_id>10</Batch_M_id>
<RSN>01HE55WV</RSN>
<Parent_RSN />
<Pkg_Location>1 </Pkg_Location>
<CompanyId>21</CompanyId>
</Table>
<Table>
<Batch_M_id>10</Batch_M_id>
<RSN>01DK7KNH</RSN>
<Parent_RSN />
<Pkg_Location>1 </Pkg_Location>
<CompanyId>21</CompanyId>
</Table>
</NewDataSet>
Parent_RSN标记其NuLl可能是它创建问题,因此给出一些值但仍显示相同错误无效列名称RSN
答案 0 :(得分:0)
你不能在单个sql语句中进行插入和更新,你需要为每个更新和插入使用一个,实际上我可能先将xml数据导入临时表然后再进行操作...
-- import xml into temp table
select COALESCE([Table].[Column].value('Batch_M_id[1]', 'bigint'),0) as 'Batch_M_id',
[Table].[Column].value('RSN [1]', 'varchar(20)') as ' RSN ',
[Table].[Column].value(' Parent_RSN[1]', 'varchar(20)') as ' Parent_RSN',
[Table].[Column].value(' Pkg_Location [1]', 'nvarchar(100)') as ' Pkg_Location',
[Table].[Column].value(' CompanyId [1]', 'int') as ' CompanyId'
into #temp_xml
FROM @xmlData.nodes('/ NewDataSet / Table') as [Table]([Column])
BEGIN TRY
-- start the transaction
BEGIN TRANSACTION
-- now do the updates
update ra ...
set ....
from RSN_ALL ra
inner join #temp_xml tx on (tx... = ra...) -- the fields that identify existence
-- now do the inserts
insert into RSN_All (Batch_M_id,RSN,Parent_RSN,Pkg_Location,CompanyId)
from #temp_xml tx
where not exists (select 1 from RSN_All ra where tx... = ra...) -- the same fields that identify existence
-- commit the transaction
COMMIT TRANSACTION
END TRY
BEGIN CATCH
-- Any problems rollback transaction
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
END CATCH;
-- drop temp table
drop table #temp_xml
答案 1 :(得分:0)
按照@HOOT建议我创建以下程序及其完美工作
CREATE procedure [dbo].[SPInsertXmlData]
(
@xmlData XML
)
as
begin
-- import xml into temp table
SELECT
COALESCE
([Table].[Column].value('Batch_M_id[1]', 'bigint'),0) as Batch_M_id,
[Table].[Column].value('RSN [1]', 'varchar(20)') as RSN,
[Table].[Column].value(' Parent_RSN[1]', 'varchar(20)') as Parent_RSN,
[Table].[Column].value(' Pkg_Location [1]', 'nvarchar(100)') as Pkg_Location,
[Table].[Column].value(' CompanyId [1]', 'int') as CompanyId
into #temp_xml
FROM @xmlData.nodes('/ NewDataSet / Table') as [Table]([Column])
BEGIN TRY
-- start the transaction
BEGIN TRANSACTION
---- now do the updates
update ra
set
ra.Batch_M_id =tx.Batch_M_id,
ra.Parent_RSN =tx.Parent_RSN,
ra.Pkg_Location=tx.Pkg_Location,
ra.CompanyId= tx.CompanyId
from RSN_ALL ra
inner join #temp_xml tx on (ra.RSN =tx.RSN ) -- the fields that identify existence
-- now do the inserts (Batch_M_id,RSN,Parent_RSN,Pkg_Location,CompanyId)
insert into RSN_All
select *
from #temp_xml tx
where not exists (select 1 from RSN_All ra where tx.RSN= ra.RSN) -- the same fields that identify existence
-- commit the transaction
COMMIT TRANSACTION
END TRY
BEGIN CATCH
-- Any problems rollback transaction
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
END CATCH;
-- drop temp table
drop table #temp_xml
end