/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Data.OleDb;
namespace ST_3.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
List<string> list_foxpro_sql = new List<string>();
List<string> list_ms_sql = new List<string>();
string foxprofilters = "";
string mssqlfilters = "";
string foxprosql = "SELECT * FROM tbname WHERE (entrykey + lineno) IN ("; ---1 (please see at end )
string mssql = "DELETE FROM [tbname ] WHERE (key + [lineno]) IN ("; --2 (please see at end )
int index = 0;
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
da.Fill(dt, Dts.Variables["QueryResultObject"].Value);
foreach (DataRow dr in dt.Rows)
{
string linenumber = dr.ItemArray[1].ToString().Trim();
int linenumber_length = linenumber.Length;
linenumber = " " + linenumber;
linenumber = linenumber.Substring(linenumber_length, 8);
foxprofilters += "'" + (dr.ItemArray[0].ToString() +" ").Substring(0,36) + linenumber + " ',";
mssqlfilters += "'" + (dr.ItemArray[0].ToString() + " ").Substring(0,36) + linenumber + " ',";
index++;
if (index >= 100)
{
foxprofilters = foxprofilters.Substring(0, foxprofilters.Length - 1);
mssqlfilters = mssqlfilters.Substring(0, mssqlfilters.Length - 1);
list_foxpro_sql.Add(foxprosql + foxprofilters + ");");
list_ms_sql.Add(mssql + mssqlfilters + ");");
index = 0;
foxprofilters = "";
mssqlfilters = "";
}
}
if (index > 0)
{
foxprofilters = foxprofilters.Substring(0, foxprofilters.Length - 1);
mssqlfilters = mssqlfilters.Substring(0, mssqlfilters.Length - 1);
list_foxpro_sql.Add(foxprosql + foxprofilters + ");");
list_ms_sql.Add(mssql + mssqlfilters + ");");
}
Dts.Variables["FoxproSQLObject"].Value = list_foxpro_sql;
Dts.Variables["MSSQLObject"].Value = list_ms_sql;
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
Explanation:
1. string foxprosql = "SELECT * FROM tbname WHERE (entrykey + lineno) IN (";
it fetches data from an application which utilizes foxpro. I want to edit this query to something like
select ltrim(rtrim(entrykey)), ltrim(rtrim(lineno)+3, phone from tbname where...
is this possible ?
2. string mssql = "DELETE FROM [tbname] WHERE (entrykey + [lineno]) IN (";
this basically deletes like
delete from tbname WHERE (entrykey + [lineno]) IN ( 1,2,3,4,5,6546,6,46,35,35,35,3345,3,636,36,3,436,364,36,346,3,63,43)
this affects the performance. .We want to write something like
delete from tbname WHERE (entrykey + [lineno]) IN ( 1)
delete from tbname WHERE (entrykey + [lineno]) IN ( 2)
delete from tbname WHERE (entrykey + [lineno]) IN ( 3)
so performance is reduced.
I dont know c#; so seeking advice