BUG! The following code has a bug. Add sb.length=0 so the StringBuilder resets. See Post with correction. This code is now producing 150-200 files/second.

Going from best backwards, I took the code from yesterday and threw it into a script task (SQL Server 2008 R2).  Code is below, and surprisingly, running out of debug mode, it ran very closely to the c# console application. I had to make only a few changes when migrating it to BIDS, so check it out.

Come up with anything faster than mine yet???

Lee Everest

 

---------------------

 

Script Task code - c#
  1. using System;
  2. using System.Data;
  3. using Microsoft.SqlServer.Dts.Runtime;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Data.SqlClient;
  9.  
  10. namespace ST_eabdae14a6634c11b8193c743bc05b6a.csproj
  11. {
  12.     [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
  13.     public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
  14.     {
  15.  
  16.         StringBuilder sb = new StringBuilder();
  17.         #region VSTA generated code
  18.         enum ScriptResults
  19.         {
  20.             Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
  21.             Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  22.         };
  23.         #endregion
  24.  
  25.         public void SaveToFile(string fileName)
  26.         {
  27.             System.IO.TextWriter w = new System.IO.StreamWriter(@"c:\temp\files\" + fileName + ".dat");
  28.             w.Write(sb.ToString());
  29.             w.Flush();
  30.             w.Close();
  31.         }
  32.    
  33.         public void Main()
  34.         {
  35.    
  36.         SqlConnection sqlConnection = null;
  37.         int maxvalue=151000;
  38.         string dbconn = "Application Name=SqlTest;Data Source=SQLVM1;Trusted_Connection=yes;database=tempdb";
  39.         using(sqlConnection = new SqlConnection(dbconn))
  40.         {
  41.         sqlConnection.Open();
  42.         SqlParameter param = new SqlParameter("@id", SqlDbType.Int, 2);
  43.         SqlDataReader reader;  
  44.       
  45.         using (SqlCommand cmd = new SqlCommand(null, sqlConnection))
  46.         {
  47.             param.Value = 1;
  48.             cmd.CommandType = CommandType.Text;
  49.             param.Direction = ParameterDirection.Input;
  50.             cmd.Parameters.Add(param);            
  51.             while ((int)param.Value<maxvalue)
  52.             {   
  53.             cmd.CommandText = "SELECT id FROM deleteTablexus WHERE idx = @id";
  54.             reader = cmd.ExecuteReader();     
  55.             while (reader.Read())
  56.                     sb.Append(reader[0].ToString() + "\r\n");
  57.           
  58.             SaveToFile(param.Value.ToString());               
  59.             reader.Close();
  60.             param.Value = (int)param.Value + 1;
  61.             }
  62.         }
  63.        }     
  64.        
  65.          Dts.TaskResult = (int)ScriptResults.Success;
  66.     }    
  67.   }
  68. }

 

Almost forgot – the code to create the table.  Use this to generate the data, in case you decide to take on my challenge.  L8rs.

TSQL to Gen the data...
  1.  
  2.  
  3. DROP TABLE deleteTablexus
  4. GO
  5. SELECT a1.idx, a1.idx + x.xx as id
  6. INTO deleteTablexus
  7. FROM
  8.     (
  9.       SELECT 1 AS ID UNION ALL
  10.       SELECT 2 UNION ALL
  11.       SELECT 3 UNION ALL
  12.       SELECT 4  
  13.       ) a0
  14.    CROSS JOIN
  15.      (SELECT DISTINCT number AS idx FROM master..spt_values WHERE number BETWEEN 1 AND 2070
  16.       UNION ALL
  17.       SELECT DISTINCT number+2070 AS idx FROM master..spt_values WHERE number BETWEEN 1 AND 1699
  18.       ) a1
  19.     , (SELECT TOP 10 CAST(RAND() AS decimal (10,2)) as xx from sys.syscolumns)x
  20. ORDER BY 1,2
  21. GO
  22. CREATE NONCLUSTERED INDEX IDX ON [dbo].[deleteTablexus] ([idx])
  23. GO

Posted in: SSIS , Etc. (Off-topic)  Tags:
blog comments powered by Disqus

by Lee Everest, M.S.

Info

Poll

Do you use Azure or cloud in your organization?



Show Results

Ads

Search


Month List

Calendar

«  May 2012  »
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2012 Lee Everest's SQL Server, etc. weblog