从存储过程中删除XML中的行

时间:2016-06-06 10:55:58

标签: sql sql-server xml

我想看看是否可以使用存储过程从xml中删除一些数据。

我有一个XML文件,每次我将数据添加到我的表中时,它都会将它添加到我的XML文件中。 以下是我的XML文件示例:

Name=79

正如您所见,名称和时间都是我表格中的字段。 哪个Name是我表中记录的id及其唯一。

我想知道是否有办法,当我删除SmsSCh_ID表单时,我的数据库也从XML文件中删除原始数据? 这是我的删除存储过程。 Name值与XML中的ALTER PROCEDURE [dbo].[Cp_SmsSchPrj_Delete] @SmsSCh_ID bigint = null, @SmsSCh_IDs nvarchar(1000)=null ,@SmsSCh_UserIDReg int = null AS BEGIN BEGIN TRY BEGIN TRANSACTION SmsSCh if (@SmsSCh_ID is not null) begin Delete from Prj_SmsSch where SmsSCh_ID=@SmsSCh_ID select 1 end else begin delete from Prj_SmsSch where SmsSCh_ID in (SELECT * FROM UF_CSVToTable(@SmsSCh_IDs)) end COMMIT TRANSACTION SmsSCh END TRY BEGIN CATCH ROLLBACK TRANSACTION SmsSCh RETURN ERROR_MESSAGE() END CATCH END 相同

public class UpdateCurriculumGui extends javax.swing.JDialog {

    public UpdateCurriculumGui() {
        super(null, ModalityType.APPLICATION_MODAL);
        //setAlwaysOnTop(true); set modal instead
        setTitle("Update Curriculum Information");
        add(new JLabel("i'm content!"));  //this is content!      
    }
}

谢谢

3 个答案:

答案 0 :(得分:1)

DECLARE @x XML = N'
<appSchedule>
    <task time="08/06/2016 9:25:00" name="79"/>
    <task time="08/06/2016 9:25:00" name="80"/>
    <task time="08/06/2016 9:25:00" name="81"/>
    <task time="08/06/2016 9:25:00" name="82"/>
</appSchedule>'

DECLARE @ignore_list TABLE (id INT)
INSERT INTO @ignore_list VALUES (79), (82)

DECLARE @t VARCHAR(50) = ''
SELECT @t += ',' + CAST(id AS VARCHAR(8000)) FROM @ignore_list
SET @t += ','

SET @x.modify('delete /appSchedule/task[(contains(sql:variable("@t"), concat(",", @name, ",")))]')

SELECT @x

答案 1 :(得分:1)

了解XML DML delete https://msdn.microsoft.com/en-us/library/ms190254.aspx
实施例

declare @x XML =
'<?xml version="1.0"?>
<appSchedule>
<task time="08/06/2016 9:25:00" name="79"/>
<task time="08/06/2016 9:25:00" name="80"/>
<task time="08/06/2016 9:25:00" name="81"/>
</appSchedule>'
--- read from file. Path is relative to server machine
SELECT @x = cast(x.bulkColumn as XML) 
FROM OPENROWSET(BULK 'c:\mydir\myfile.xml', SINGLE_CLOB) AS x

set @x.modify('delete  /appSchedule/task[@name="79"]');

select @x;

关于OPENROWSET BULK SINGLE_BLOB https://msdn.microsoft.com/en-us/library/ms190312.aspx

答案 2 :(得分:1)

这是我的方法

CREATE TABLE #yourXmlStore (ID INT, YourXML XML);
INSERT INTO #yourXmlStore VALUES
(1,'<appSchedule>
    <task time="08/06/2016 9:25:00" name="79"/>
    <task time="08/06/2016 9:25:00" name="80"/>
    <task time="08/06/2016 9:25:00" name="81"/>
    </appSchedule>');

CREATE TABLE #yourData (ID INT IDENTITY, name VARCHAR(100),time DATETIME);
INSERT INTO #yourData(name,time) VALUES
 ('79',{ts'2016-06-08 09:25:00'})
,('80',{ts'2016-06-08 09:25:00'})
,('81',{ts'2016-06-08 09:25:00'});
GO

CREATE PROCEDURE DeleteTask @name VARCHAR(100)
AS
BEGIN
    DELETE FROM #yourData WHERE name=@name;
    UPDATE #yourXmlStore SET YourXML.modify('delete /appSchedule/task[@name=sql:variable("@name")]');
END
GO

SELECT * FROM #yourData;
SELECT * FROM #yourXmlStore;
EXEC DeleteTask @name='79';
SELECT * FROM #yourData;
SELECT * FROM #yourXmlStore;
GO

DROP TABLE #yourData;
DROP TABLE #yourXmlStore;   
DROP PROCEDURE DeleteTask;