tags:

views:

46

answers:

1
+1  Q: 

Pipe SQL to a file

Any idea how to pipe the results of ...

    DECLARE 
        @session_name VARCHAR(200) = 'test_trace'

    SELECT 
        pivoted_data.* 
    FROM 
    ( 
     SELECT MIN(event_name) AS event_name,
         MIN(event_timestamp) AS event_timestamp,
         unique_event_id,
         CONVERT ( BIGINT, MIN (
             CASE
                 WHEN d_name = 'cpu'
                 AND d_package IS NULL
                 THEN d_value
             END ) ) AS [cpu],
         CONVERT ( BIGINT, MIN (
             CASE
                 WHEN d_name = 'duration'
                 AND d_package IS NULL
                 THEN d_value
             END ) ) AS [duration],
         CONVERT ( BIGINT, MIN (
             CASE
                 WHEN d_name = 'object_id'
                 AND d_package IS NULL
                 THEN d_value
             END ) ) AS [object_id],
         CONVERT ( INT, MIN (
             CASE
                 WHEN d_name = 'object_type'
                 AND d_package IS NULL
                 THEN d_value
             END ) ) AS [object_type],
         CONVERT ( DECIMAL(28,0), MIN (
             CASE
                 WHEN d_name = 'reads'
                 AND d_package IS NULL
                 THEN d_value
             END ) ) AS [reads],
         CONVERT ( VARCHAR(MAX), MIN (
             CASE
                 WHEN d_name = 'session_id'
                 AND d_package IS NOT NULL
                 THEN d_value
             END ) ) AS [session_id],
         CONVERT ( INT, MIN (
             CASE
                 WHEN d_name = 'source_database_id'
                 AND d_package IS NULL
                 THEN d_value
             END ) ) AS [source_database_id],
         CAST((SELECT CONVERT ( VARCHAR(MAX), MIN (
             CASE
                 WHEN d_name = 'sql_text'
                 AND d_package IS NOT NULL
                 THEN d_value
             END ) )  AS [processing-instruction(x)] FOR XML PATH('') ) AS XML) AS [sql_text],
         CONVERT ( DECIMAL(28,0), MIN (
             CASE
                 WHEN d_name = 'writes'
                 AND d_package IS NULL
                 THEN d_value
             END ) ) AS [writes]
     FROM
        ( 
            SELECT 
                *, 
                CONVERT(VARCHAR(400), NULL) AS attach_activity_id 
            FROM 
            ( 
                SELECT 
                    event.value('(@name)[1]', 'VARCHAR(400)') as event_name, 
                    event.value('(@timestamp)[1]', 'DATETIME') as event_timestamp, 
                    DENSE_RANK() OVER (ORDER BY event) AS unique_event_id, 
                    n.value('(@name)[1]', 'VARCHAR(400)') AS d_name, 
                    n.value('(@package)[1]', 'VARCHAR(400)') AS d_package, 
                    n.value('((value)[1]/text())[1]', 'VARCHAR(MAX)') AS d_value, 
                    n.value('((text)[1]/text())[1]', 'VARCHAR(MAX)') AS d_text 
                FROM 
                ( 
                    SELECT 
                        ( 
                            SELECT 
                                CONVERT(xml, target_data) 
                            FROM sys.dm_xe_session_targets st 
                            JOIN sys.dm_xe_sessions s ON 
                                s.address = st.event_session_address 
                            WHERE 
                                s.name = @session_name 
                                AND st.target_name = 'ring_buffer' 
                        ) AS [x] 
                    FOR XML PATH(''), TYPE 
                ) AS the_xml(x) 
                CROSS APPLY x.nodes('//event') e (event) 
                CROSS APPLY event.nodes('*') AS q (n) 
            ) AS data_data 
        ) AS activity_data 
        GROUP BY 
            unique_event_id 

) AS pivoted_data; 

to a file using DbDataReader that is returned from DbCommand.ExecuteReader()?

+1  A: 

This CodeProject article should get you started:

http://www.codeproject.com/KB/database/Cs_CSV_import_export.aspx

Dave Swersky