Advertisement
SqlQuantumLeap

SQLCLR SP Parses CSV file to Result Set - Testing

Apr 15th, 2016 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 7.17 KB | None | 0 0
  1. /*
  2.     This script relates to the following SQL Server Central Forum topic:
  3.     Processing strings ( https://www.sqlservercentral.com/forums/topic/processing-strings )
  4.  
  5.     This script provides several tests for the [ParseCSV] SQLCLR Stored Procedure.
  6.  
  7.     A T-SQL installation script (no external DLL) containing only two Stored Procedures is located at:
  8.     https://pastebin.com/aqsWiX1e
  9.  
  10.     The source code for the [ParseCSV] and [GarbageCollect] SQLCLR Stored Procedures is located at:
  11.     https://pastebin.com/BY8F994R
  12.  
  13.     Date: 2016-04-11
  14.     Version: 1.0.0
  15.  
  16.     For more functions like this, please visit: https://SQLsharp.com
  17.  
  18.     Stairway to SQLCLR series: https://www.sqlservercentral.com/stairways/stairway-to-sqlclr
  19.  
  20.     Copyright (c) 2016 Sql Quantum Leap. All rights reserved.
  21.     https://SqlQuantumLeap.com
  22. */
  23.  
  24. SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
  25. SET NUMERIC_ROUNDABORT OFF;
  26. SET NOCOUNT ON;
  27. GO
  28.  
  29.  
  30. USE [CSVParser];
  31. GO
  32.  
  33. ------------------------  BEGIN TEST #1 ------------------------------------
  34. -- Simple functional test to make sure that various scenarios are properly handled:
  35. -- 1) text-qualfied fields
  36. -- 2) not all fields need to be text-qualified
  37. -- 3) embedded text-qualifier
  38. -- 4) embedded field delimiter
  39. -- 5) embedded row delimiter
  40.  
  41. DECLARE @CSV NVARCHAR(MAX) = N'12,12,1231231,fgd,231
  42. g,h,j,"w
  43. ggg",y
  44. "sdf,hhh",32,"dfg"",z""ghd",55,,5564
  45. 45
  46. 5,6,77
  47. a,';
  48.  
  49. EXEC dbo.ParseCSV ',', @CSV;
  50.  
  51. ------------------------  END TEST #1 ------------------------------------
  52.  
  53.  
  54. ------------------------  BEGIN TEST #2 ------------------------------------
  55. USE [CSVParser];
  56.  
  57. -- FIRST, create the destinaton table.
  58. -- Please note that all fields are NVARCHAR(MAX) because all result set
  59. -- fields coming back from [ParseCSV] are NVARCHAR(MAX).
  60.  
  61. -- DROP TABLE [dbo].[Destination];
  62. IF (OBJECT_ID(N'dbo.Destination') IS NULL)
  63. BEGIN
  64.     PRINT 'Creating table: Destination...';
  65.     CREATE TABLE [dbo].[Destination](
  66.         [object_id] NVARCHAR(MAX) NOT NULL,
  67.         [definition] NVARCHAR(MAX) NULL,
  68.         [uses_ansi_nulls] NVARCHAR(MAX) NULL,
  69.         [uses_quoted_identifier] NVARCHAR(MAX) NULL,
  70.         [is_schema_bound] NVARCHAR(MAX) NULL,
  71.         [uses_database_collation] NVARCHAR(MAX) NULL,
  72.         [is_recompiled] NVARCHAR(MAX) NULL,
  73.         [null_on_null_input] NVARCHAR(MAX) NULL,
  74.         [execute_as_principal_id] NVARCHAR(MAX) NULL,
  75.         [uses_native_compilation] NVARCHAR(MAX) NULL,
  76.         [name] NVARCHAR(MAX) NOT NULL,
  77.         [principal_id] NVARCHAR(MAX) NULL,
  78.         [schema_id] NVARCHAR(MAX) NOT NULL,
  79.         [parent_object_id] NVARCHAR(MAX) NOT NULL,
  80.         [type] NVARCHAR(MAX) NOT NULL,
  81.         [type_desc] NVARCHAR(MAX) NULL,
  82.         [create_date] NVARCHAR(MAX) NOT NULL,
  83.         [modify_date] NVARCHAR(MAX) NOT NULL,
  84.         [is_ms_shipped] NVARCHAR(MAX) NULL,
  85.         [is_published] NVARCHAR(MAX) NULL,
  86.         [is_schema_published] NVARCHAR(MAX) NULL,
  87.         [extra] VARCHAR(MAX) NULL
  88.     ) ON [UserData] TEXTIMAGE_ON [UserData];
  89. END;
  90.  
  91.  
  92.  
  93. -- SECOND, generate the test CSV file. The following query should produce about
  94. -- 80 MB of result data (there is slight variation due to the last column being
  95. -- a random number of repetitions per row of a GUID, and not all systems have
  96. -- the same number of entries in the two tables used in the following query.
  97. --
  98. -- On my install of SQL Server 2012, SP2 the following query generated 185,288
  99. -- rows. I adjust the "207" value in the REPLACE on the top SELECT line to get
  100. -- the output to be almost exactly 80 MB. Without the REPLACE function, the same
  101. -- number of rows produced a file that was 674 MB. I would suggest not going over
  102. -- 100 MB for the file since the test uses the INSERT...EXEC construct which
  103. -- stores the results in memory until the EXEC call finishes, before it starts
  104. -- inserting those results into the table.
  105. --
  106. -- To generate the file:
  107. -- 1) Run the query using Results to Grid (the default setting unless you changed it).
  108. -- 2) Right-click in the grid and select "Save Results As...".
  109. -- 3) "Save as type" should be set to "CSV (Comma delimited) (*.csv)"
  110. -- 4) Choose a path and enter in a name (path should be easily accessible; "C:\" is
  111. --    usually restricted; I create a "C:\TEMP" folder).
  112. -- 5) Click the "Save" button.
  113. -- 6) In File Explorer, go to the path you just saved the file in, right-click on the
  114. --    file, and select "Properties" (bottom option).
  115. -- 7) Make a note of the "Size" value, not the "Size on disk" value.
  116.  
  117. SELECT sasm.[object_id], '"' + REPLACE(LEFT(sasm.[definition], 207), N'"', N'""') + '"' AS [definition],
  118.        sasm.[uses_ansi_nulls], sasm.[uses_quoted_identifier], sasm.[is_schema_bound],
  119.        sasm.[uses_database_collation], sasm.[is_recompiled], sasm.[null_on_null_input],
  120.        sasm.[execute_as_principal_id], CONVERT(BIT, 0) AS [uses_native_compilation],
  121.        sao.[name], sao.[principal_id], sao.[schema_id], sao.[parent_object_id], sao.[type], sao.[type_desc],
  122.        sao.[create_date], sao.[modify_date], sao.[is_ms_shipped], sao.[is_published], sao.[is_schema_published],
  123.        '"' + REPLICATE(NEWID(), (CRYPT_GEN_RANDOM(1) % 5) + 1) + '"' AS [extra]
  124. FROM   msdb.[sys].[all_sql_modules] sasm
  125. INNER JOIN  msdb.[sys].[all_objects] sao
  126.         ON  sao.[object_id] = sasm.[object_id]
  127. CROSS JOIN  msdb.sys.views;
  128. -- 185,288 rows / 80.0 MB (83,936,807 bytes)
  129.  
  130.  
  131.  
  132. -- THIRD, run the SQLCLR stored proc once with simple input to
  133. -- ensure that the App Domain has been created and that the Assembly
  134. -- has been loaded. This initialization sometimes takes a second or
  135. -- two and we don't want that time throwing off the test results.
  136. EXEC dbo.ParseCSV @Delimiter = N',', @InputString = N'1,"2a,2b,2c",3';
  137.  
  138.  
  139.  
  140. -- FOURTH, make sure that the @FilePath parameter has the correct path
  141. -- and file name for the CSV file that you created in the second step.
  142.  
  143. -- TRUNCATE TABLE dbo.Destination;
  144. INSERT INTO dbo.Destination ([object_id], [definition], uses_ansi_nulls, uses_quoted_identifier,
  145.                              is_schema_bound, uses_database_collation, is_recompiled, null_on_null_input,
  146.                              execute_as_principal_id, uses_native_compilation, name, principal_id,
  147.                              [schema_id], parent_object_id, [type], type_desc, create_date, modify_date,
  148.                              is_ms_shipped, is_published, is_schema_published, extra)
  149.   EXEC dbo.ParseCSV
  150.           @Delimiter = N',',
  151.           @InputString = NULL, -- default value not allowed so specify empty or NULL
  152.           @FilePath = N'C:\TEMP\TestImportData.csv'; -- replace this value with your path + filename
  153.  
  154.  
  155.  
  156. -- FIFTH, check to make sure that all of the data got loaded. NULL values will import as the word
  157. -- NULL instead of being an actual NULL, but this is just a simple test and that inaccuracy does
  158. -- not affect the timing.
  159. SELECT * FROM dbo.Destination;
  160.  
  161.  
  162.  
  163. -- SIXTH, run again, just to not rely on a single timing. Before we can re-run, however, we
  164. -- should clear out the Destination table:
  165. CHECKPOINT;
  166. TRUNCATE TABLE dbo.Destination;
  167. CHECKPOINT;
  168. -- now go back and repeat the THIRD through FIFTH steps
  169.  
  170.  
  171.  
  172. -- EXEC dbo.GarbageCollect;
  173. -- DBCC DROPCLEANBUFFERS
  174. -- DBCC FREESYSTEMCACHE('ALL')
  175. -- CHECKPOINT;
  176.  
  177. ------------------------  END TEST #2 ------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement