Tuesday, January 8, 2013

flaging duplicate without using any temp table and row_id



with TBLCTE(slno,C1,C2,C3,c4,Ranking)
AS
(
select
slno,C1,c2,c3,c4,
Ranking = DENSE_RANK() over (PARTITION BY slno,C1,C2,C3 order by slno,newID())
from trans where C4 <> 'Y'
)
update TBLCTE set C4 = 'Y' where Ranking > 1

select * from trans order by Slno,c1,C2,C3

Monday, January 7, 2013

Excel validate filename


  public void Main()
        {
            // TODO: Add your code here
            string strFileName = string.Empty;
            bool bFileSuccess;
            string strUniqueFileName = string.Empty;

            strFileName = GetSourceFileName(out bFileSuccess);
            if (!strFileName.Equals(string.Empty))
            {
                if (bFileSuccess)
                {
                    LogFileName(strFileName, bFileSuccess);
                    Dts.Variables["pExtractDate"].Value = dtFileExtract;//Set File extract date
                }
                else
                {
                    Dts.TaskResult = (int)ScriptResults.Failure;
                    LogFileName(strFileName,bFileSuccess, "Wrong source file format");
                }
            }
            else
            {
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            Dts.Variables["pVarFileName"].Value = strFileName;

            ////strUniqueFileName = System.DateTime.Now.ToString("dd-MM-yyyy") + System.DateTime.Now.TimeOfDay.Milliseconds.ToString() + ".xls";
     
            //////Create a copy of file
            ////FileInfo fiSourceFile = new FileInfo(strSourceFile);
            ////fiSourceFile.CopyTo(Dts.Variables["InputFileFolder"].Value.ToString() + strUniqueFileName, true);
         
            ////Dts.Variables["TabFileSource"].Value = Dts.Variables["InputFileFolder"].Value.ToString() + strUniqueFileName;
        }
        public String GetSourceFileName(out bool bFileSuccess)
        {
            String strRetSourceFile = string.Empty;
            Boolean blDisableTaskFlag = false;
            String strSourceFolder = Dts.Variables["InputFileFolder"].Value.ToString();
            String strSourceFileTemplate = Dts.Variables["FileNameTemplate"].Value.ToString().ToLower();
            DirectoryInfo diSourceFolder = new DirectoryInfo(strSourceFolder);
            FileInfo[] fiSourceFiles = diSourceFolder.GetFiles();
            bFileSuccess = true;
            foreach (FileInfo fiSourceFile in fiSourceFiles)
            {
                if (fiSourceFile.Name.ToLower().Contains(strSourceFileTemplate))
                {
                    strRetSourceFile = fiSourceFile.Name;
                    String strDatePart = strRetSourceFile.Substring(strSourceFileTemplate.Length);
                    strDatePart = strDatePart.Substring(0, strDatePart.Length - 4);
                    Dts.Variables["FileSource"].Value = fiSourceFile.FullName;
                 
                    //Form File extraction date

                    if (!DateTime.TryParse(strDatePart, out dtFileExtract))
                    {
                        bFileSuccess = false;
                    }
                    blDisableTaskFlag = false;
                    break;
                }
                else
                {
                    blDisableTaskFlag = true;
                }
            }
         
            if (strRetSourceFile.Equals(string.Empty))
            {
                blDisableTaskFlag = true;
                //blDisableTaskFlag = false;
            }
            Dts.Variables["DisableTask"].Value = blDisableTaskFlag;
            return strRetSourceFile;
        }
        public void LogFileName(string strFileName,bool bSuccess)
        {
            LogFileNameEx(strFileName,bSuccess, "");
         
        }
        public void LogFileName(string strFileName,bool bSuccess,string strMessage)
        {
            LogFileNameEx(strFileName,bSuccess, strMessage);
        }
        private void LogFileNameEx(string strFileName, bool bSuccess,string strMessage)
        {
            OleDbConnection sqlConnection = new OleDbConnection(Dts.Connections["ITGWarehouse"].ConnectionString);
            using (OleDbCommand cmdUspFileProcessingDetailsOnBegin = new OleDbCommand("MD.usp_GetFileProcessingList"))
            {
                cmdUspFileProcessingDetailsOnBegin.Connection = sqlConnection;
                cmdUspFileProcessingDetailsOnBegin.CommandType = CommandType.StoredProcedure;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExecutionLogId", SqlDbType.Int).Value = Dts.Variables["LogID"].Value;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pFileType", SqlDbType.VarChar).Value = Dts.Variables["FileNameTemplate"].Value;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pFileName", SqlDbType.VarChar).Value = strFileName;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pMessage", SqlDbType.VarChar).Value = strMessage;
                if (bSuccess)
                    cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExtractDate", SqlDbType.DateTime).Value = dtFileExtract;
                else
                    cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExtractDate", SqlDbType.DateTime).Value = DBNull.Value;
                try
                {
                    sqlConnection.Open();
                    cmdUspFileProcessingDetailsOnBegin.ExecuteNonQuery();
                }
                finally
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
            }

        }

Excel retrieve sheet by sheet

Excel connection:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Source\Product\20081202_Product.xls;Extended Properties="EXCEL 8.0;HDR=NO";

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+@[User::filenameProduct]+";Extended Properties=\"Excel 8.0;HDR=NO\";"

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\assignment related\Source\Product\20081202_Product.xls;Extended Properties="Excel 8.0;HDR=YES";



  public void Main()
        {
            ////Microsoft.SqlServer.Dts.Runtime.ConnectionManager cm = Dts.Connections["HS_Incidents"];
            ////IDTSConnectionManagerDatabaseParameters100 cmParams = cm.InnerObject as IDTSConnectionManagerDatabaseParameters100;
            ////OleDbConnection conn = cmParams.GetConnectionForSchema() as OleDbConnection;
            ////conn.Close();
         
            string excelFile;
            string connectionString;
            OleDbConnection excelConnection;
            DataTable tablesInFile;
            int tableCount = 0;
            string currentTable;
            int tableIndex = 0;

            excelFile = Dts.Variables["FileSource"].Value.ToString();
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + excelFile + ";Extended Properties=Excel 8.0";
            excelConnection = new OleDbConnection(connectionString);
            excelConnection.Open();
            tablesInFile = excelConnection.GetSchema("Tables");
            tableCount = tablesInFile.Rows.Count;

            string[] excelTables = new string[tableCount];
         
            foreach (DataRow tableInFile in tablesInFile.Rows)
            {
                currentTable = tableInFile["TABLE_NAME"].ToString();
                //GetRowsOfCurrentTable(currentTable, excelConnection.GetOleDbSchemaTable(
                excelTables[tableIndex] = "Select [" + currentTable + "].* From " + "["+ currentTable + "]";
                tableIndex += 1;
            }
            excelConnection.Close();
            //System.IO.FileInfo x = new System.IO.FileInfo(Dts.Variables["FileSource"].Value.ToString() );
            //x.Delete();
            //System.IO.File.Move(excelFile, Dts.Variables["ValidSourceFile"].Value.ToString());
            Dts.Variables["ExcelTables"].Value = excelTables;

            Dts.TaskResult = (int)ScriptResults.Success;

        }
    }