(4.31) xml data manipulation

Keywords: xml data into rows and columns display

General Case:

  

declare @data xml
declare @h int
set @data='
<bookstore>
<row>
<province_id>0</province_id>
<code>11</code>
<name>北京市</name>
</row>
<row>
<province_id>1</province_id>
<code>22</code>
<name>上海</name>
</row>
</bookstore>
'

exec sp_xml_preparedocument @h output,@data
select * from openxml(@h,'//row',2)
with
(
province_id int,
code Varchar(100),
name Varchar(100)
) 
exec sp_xml_removedocument @h

 

 

 XML parsing triggers

[ 1 ] database-level DDL operation monitoring audit 

SQL Server 2005 began to support DDL triggers, it is not limited to the CREATE / the ALTER / effective DROP operations, supported DDL events as well, such as: rights GRANT / DENY / REVOEK, RENAME object, update statistics and so on, to see more support through the DMV event types are as follows: 

the SELECT  *  from sys.trigger_event_types
 the WHERE type_name not  like  ' % the CREATE% ' 
  and type_name not  like  ' % the ALTER% ' 
  and type_name not  like  ' % DROP% ' 
Note: 

1Event Type TRUNCATE DDL triggers are not in, SQL Server will be classified as DML operations Truncate statement, though it does not trigger DML triggers, just turn on the switch of the bulk import operation ( Bulk Import the Operations) as; 

2 . DDL trigger information captured by EVENTDATA () function returns, the return type is XML format, you need to use XQuery to read; 

 

case: Transfer from 2012 sample library, only the database level, not at the instance level 

copy the code 
use  database 
Go 

the SET ANSI_NULLS the ON 
the GO 
the SET the QUOTED_IDENTIFIER the ON 
the GO 
Create  Table databaseLog ( [ PostTime ]  datetime ,
 [ DatabaseUser ]  VARCHAR ( 500 ),
 [ the Event ]  VARCHAR ( 500),
[Schema] varchar(50),
[Object] varchar(4000),
[TSQL] varchar(4000),
[XmlEvent] xml)

 
CREATE TRIGGER [ddlDatabaseTriggerLog] ON DATABASE --all server 实例级别
FOR DDL_DATABASE_LEVEL_EVENTS AS  --DDL_SERVER_LEVEL_EVENTS 实例级别
BEGIN
    SET NOCOUNT ON;
 
    DECLARE @data XML;
    DECLARE @schema sysname;
    DECLARE @object sysname;
    DECLARE @eventType sysname;
 
    SET @data = EVENTDATA();
    SET @eventType = @data.value('(/EVENT_INSTANCE/EventType)[1]', 'sysname');
    SET @schema = @data.value('(/EVENT_INSTANCE/SchemaName)[1]', 'sysname');
    SET @object = @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'sysname')
 
    IF @object IS NOT NULL
        PRINT '  ' + @eventType + ' - ' + @schema + '.' + @object;
    ELSE
        PRINT '  ' + @eventType + ' - ' + @schema;
 
    IF @eventType IS NULL
        PRINT CONVERT(nvarchar(max), @data);
 
    INSERT [dbo].[DatabaseLog]
        (
        [PostTime],
        [DatabaseUser],
        [Event],
        [Schema],
        [Object],
        [TSQL],
        [XmlEvent]
        )
    VALUES
        (
        GETDATE(),
        CONVERT(sysname, CURRENT_USER),
        @eventType,
        CONVERT(sysname, @schema),
        CONVERT(sysname, @object),
        @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)'),
        @data
        );
END;
 
GO
 
SET ANSI_NULLS OFF
GO
 
SET QUOTED_IDENTIFIER OFF
GO

--开启/关闭 
ENABLE TRIGGER [ddlDatabaseTriggerLog ]  the ON  DATABASE 
the DISABLE TRIGGER  [ ddlDatabaseTriggerLog ]  the ON  DATABASE 
the GO 

- Remove 
the DROP  TRIGGER tri_LogServerEvent the ON  DATABASE ; 

- add extended attributes to a database object (i.e., data dictionary adding annotations) 
EXEC sys.sp_addextendedproperty the @name = N ' MS_Description ' , @ value = N ' Database The Trigger to Audit All of the DDL Changes Made to the AdventureWorks2008R2 The Database. ' , 
 @ level0type =N'TRIGGER',@level0name=N'ddlDatabaseTriggerLog' GO

 

Guess you like

Origin www.cnblogs.com/gered/p/10948928.html