Create Batch file to Execute SSIS Package

SET PackageDir = D:\Job\Package
SET PackageName = Package.dtsx
SET ConfigDir = D:\Job\Config
SET ConfigName = date.dtsConfig

DTEXEC  /F  %PackageDir%\%PackageName%  /CONFIGFILE  %ConfigDir%\%ConfigName
 /CHECKPOINTING OFF  /REPORTING V

If you need to change the variable value in the package using batch  you need to add following code
/SET "\Package.Variables[vServerType].Value";"P"

Full code:
DTEXEC  /F  %PackageDir%\%PackageName%  /CONFIGFILE  %ConfigDir%\%ConfigName /SET "\Package.Variables[vServerType].Value";"P"  /CHECKPOINTING OFF  /REPORTING V

Note: If this "vServerType" variable present in your config file , the value of this variable will not change during the execution time.

Note: If we use same Config file for different package,we should ensure  the variables present in your config file whether present in package which are used.


read more

To Find Day, Month and Year of System Date using Dos batch

@echo off
echo %date%  // It shows system date Thu 02/13/2014

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (
    SET Day=%%b
    SET Month=%%a
    SET Year=%%c)   // Tokens it will tokenize the date format from 2,3&4 , using delimiter / and space   
from date (Thu 02/13/2014) and it assign the value into a,b,c a contains month,b contains day and c contains Year
echo %Day%

Pause
if %Day%==06 goto :l1
 goto end
:l1
 call date.bat // We can all other bat
:end

read more

Delete Files From A Particular Folder in Script Task using C# Code


Step 1
Create  a SSIS variable "Folder_Path" and  assign the value of file path
Step 2
Use Script Task
Step 3
Read the variable in ReadOnlyVariables Pane.


Step 4
Add the following code

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;

namespace ST_21af426c5ff24863a648aab041051234.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

       

      
 //To Delete the files from folder
        public void Main()
        {
         String Folder = Dts.Variables["Folder_Path"].Value.ToString(); // Path
            Array.ForEach(Directory.GetFiles(Folder), delegate(String path) { File.Delete(path); });
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }

}
read more

Database Attached failed, Unable to open physical file, operating system error 5: "5 (Access is denied.)" And Version Error



Some times when you try to attach database files (.mdf, . ldf) to sql server , you might get the errors
I     Permission Error
II   Version Error

I     Permission Error

"Unable to open physical file, operating system error" 










This is because of you don't have the full permission for (.mdf) file from where you are accessing. For this reason you need to give the full permission for that file.

1. Right click on file
2  Go to Properties
3. Select Security Pane.
4. Click Edit
5.  Select the Appropriate User Name
6. Give Full control 
7. Apply and Ok




Now you try to Attach the file to Database.

II   Version Error
Some times we can't open mdf file due to lower version of SQL Server. The file will be higher version than the Server version. In that case we need to add supported version where you are loading the file.



read more

Read Excel Data

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text.RegularExpressions; //split words from a line
namespace ReadExcelData
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamWriter sw = new StreamWriter(@"c:\text.txt");
            string connString = "";
            string fileExtension = "";
            string fileName = "";

            string ExcelPath = @"C:\Test.xlsx";
            fileName = Path.GetFileName(ExcelPath);
            fileExtension = Path.GetExtension(ExcelPath);


            if (fileExtension == ".xls")
            {
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
            }
            else if (fileExtension == ".xlsx")
            {
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
            }

            OleDbConnection con = new OleDbConnection(connString);
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = System.Data.CommandType.Text; // Declaration of cmd as text
            cmd.Connection = con;
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd); //Take data from source to data table
            DataTable dtExcelRecords = new DataTable();
            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //Adding all sheet Names into tables

            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
            DataTable dtColumnName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, getExcelSheetName, null });

            DataView dv = dtColumnName.DefaultView;
            dv.Sort = "ORDINAL_POSITION";
            dtColumnName = dv.ToTable();

            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";

            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            con.Close();
            List<string> listColumn = new List<string>();
            foreach (DataRow row in dtColumnName.Rows)
            {
                listColumn.Add(row["Column_name"].ToString());
            }
            foreach (string prime in listColumn) // Loop through List with foreach
            {
                sw.Write(prime + '\t');
            }
            sw.WriteLine();
            foreach (DataRow row in dtExcelRecords.Rows)
            //for(int i=0 ;i<dtExcelRecords.Rows.Count;i++)
            {

                foreach (var item in row.ItemArray) // Loop over the items.
                //for(int j=0;j<dtExcelRecords.Columns .Count ;j++)
                {
                    //sw.Write(dtExcelRecords.Rows[i][j].ToString());
                    sw.Write(item); // Invokes ToString abstract method.
                    sw.Write('\t');
                }
                sw.WriteLine();


            }

            //Console.Write(getExcelSheetName);
            sw.Close();
            StreamReader sr = new StreamReader(@"c:\text.txt");


            string[] columns;
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                columns = line.Split('\t'); //using for single character as delimiter
                //Regex.Split(line, "\t"); //using for string as delimiter
                foreach (string part in columns)
                {
                    Console.WriteLine(part);
                }

            }
            Console.Read();
            sr.Close();
        }
    }
}

read more

Multiple CTEs Join with Other Tables

Multiple CTEs Join with Other Tables

1. Create  1st CTE : cte_year

2.Create 2nd CTE: cte_date

3. Use Joining Table at Left Side :Job_Plan

Example

WITH cte_year
AS (
        SELECT year(getdate()) AS current_year,1 AS cnt

        UNION ALL

        SELECT current_year + 1 AS current_year,cnt + 1 AS cnt FROM cte_year WHERE cnt < 5

        )

,cte_date

AS (

        SELECT CASE
                       WHEN month(getdate()) >= 7  THEN year(getdate())
                       ELSE year(dateadd(yy, - 1, getdate()))
                       END start_dt

               ,CASE
                       WHEN month(getdate()) <= 6 THEN year(getdate())
                       ELSE year(dateadd(yy, 1, getdate()))
                       END end_dt
               ,1 AS cnt
   

        UNION ALL

        SELECT start_dt + 1 start_dt ,end_dt + 1 end_dt ,cnt + 1 FROM cte_date  WHERE cnt < 5

        )

SELECT DISTINCT A.* FROM Job_Plan(NOLOCK) a

INNER JOIN (

        SELECT cast(current_year AS VARCHAR(100)) AS Plan_Name  FROM cte_year

        UNION ALL

        SELECT cast(right(current_year, 2) + 'FS' AS VARCHAR(100)) AS Plan_Name  FROM cte_year

        UNION ALL

        SELECT cast(right(current_year, 2) + 'PS' AS VARCHAR(100)) AS Plan_Name  FROM cte_year

        UNION ALL

        SELECT cast(right(start_dt, 2) + '-' + right(end_dt, 2) AS VARCHAR(100)) AS Plan_Name   FROM cte_date

        UNION ALL

        SELECT cast(right(start_dt, 2) + ' - ' + right(end_dt, 2) AS VARCHAR(100)) AS Plan_Name   FROM cte_date

        ) F ON ltrim(rtrim(a.Plan_Name_Long)) LIKE '%' + ltrim(rtrim(F.Plan_Name)) + '%'

        AND a.Plan_Event_Name_Long NOT LIKE '%' + cast(year(dateadd(yy, - 1, getdate())) AS VARCHAR(100)) + '%'

        AND Plan_ID NOT IN (  SELECT Plan_ID    FROM Job_In_Plan(NOLOCK)  )

ORDER BY a.Plan_ID


output of cte_year
current_year cnt
2013        1
2014        2
2015        3
2016        4

2017        5


output of cte_date
start_dt end_dt cnt
2013 2014 1
2014 2015 2
2015 2016 3
2016 2017 4

2017 2018 5

read more

How to Send Web API request Through Script Component

Step 1 : Create Data flow Task
           
 1) Oledb Source
2) Script Component
3) Oledb Destination


Main Part of Script Component
--------------------------------
/* Microsoft SQL Server Integration Services Script Component
*  Write scripts using Microsoft Visual C# 2008.
*  ScriptMain is the entry point class of the script.*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Web;
using System.Net;
using System.Xml;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    string XmlPath = @"D:\Error.xml";
    public override void PreExecute()
    {
        base.PreExecute();
        /*
          Add your code here for preprocessing or remove if not needed
        */
    }

    public override void PostExecute()
    {
        base.PostExecute();
        /*
          Add your code here for postprocessing or remove if not needed
          You can set read/write variables here, for example:
          Variables.MyIntVar = 100
        */
    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        string newsletterNames1 = "";
        string newsletterNames2 = "";

        if (File.Exists(XmlPath))
        {
            File.Delete(XmlPath);
        }
        Int64 Cust_SK = Row.CustSK;
        string SourceName = Row.SourceName;
        string dob = Row.DateOfBirth;
        string url = Row.RegistrationUrl;
        string state = Row.State;
        string password = Row.Password;
        string mobilenumber = Row.MobileNumber;
        string userName = Row.UserName;
        string firstName = Row.FirstName;
        string addressLine2 = Row.AddressLine2;
        string addressLine1 = Row.AddressLine1;
        string zip = Row.Zip;
        string lastName = Row.LastName;
        string status = Row.Status;
        string city = Row.City;
        string country = Row.Country;
        string gender = Row.Gender;
        string Email = Row.EmailAddress;
        string acceptTerms = Row.AcceptTerms;
        string newsletters = Row.NewsLetterNames;
        if ((newsletters != "") && newsletters.Contains(","))
        {
            string[] words = newsletters.Split(',');
            newsletterNames1 = words[0];
            newsletterNames2 = words[1];
        }


        string requestUrl1 = "user.dateOfBirth=" + dob + "&registration_url=" + url + "&user.address.state=" + state + "&passwordCriteria.password=" + password;
        string requestUrl2 = "&user.mobileInfoList[0].number=" + mobilenumber + "&user.userName=" + userName + "&user.firstName=" + firstName;
        string requestUrl3 = "&user.address.addressLine2=" + addressLine2 + "&user.address.addressLine1=" + addressLine1;
        string requestUrl4 = "&passwordCriteria.confirmPassword=" + password + "&user.address.zip=" + zip + "&user.lastName=" + lastName;
        string requestUrl5 = "&user.userAttributes['status']=" + status + "&user.address.city=" + city + "&user.address.country=" + country;
        string requestUrl6 = "&user.userAttributes['gender']=" + gender + "&user.primaryEmailAddress.value=" + Email + "+&confirmEmail=" + Email;
        string requestUrl7 = "&options['acceptTerms']=" + acceptTerms + "&newsletterCriteria.newsletterNames=" + newsletterNames1 + "&newsletterCriteria.newsletterNames=" + newsletterNames2;
        string requestUrl = @"https://addresses.com/services/flow/ext-register?" + requestUrl1 + requestUrl2 + requestUrl3 + requestUrl4 + requestUrl5 + requestUrl6 + requestUrl7;
        HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(response.GetResponseStream());
        XmlDocument doc = xmlDoc;

        XmlElement root = doc.DocumentElement;

        ResultBuffer.AddRow();                    /// Add row() we are adding each row for output for these we                                                                   //need to make  SynchronousInput_ID as "None".
                                                            //  Output 0  in Inputs and Outputs Pane.
        ResultBuffer.CustSK = Cust_SK;
        ResultBuffer.SourceName = SourceName;

        string type = root.Attributes["type"].Value;


        if (type == "failure")
        {
            ResultBuffer.MSIBResponseID = 0;
            doc.Save(XmlPath);
            getXMLResponse(Cust_SK, SourceName, XmlPath);
        }
        if (type == "success")
        {
            ResultBuffer.MSIBResponseID = 1;
        }
    }
    public void getXMLResponse(Int64 Cust_SK, string SourceName, string XmlPath)
    {
        var conn = this.Connections.Connection.AcquireConnection(null);
        SqlConnection Connection = (SqlConnection)conn;
        SqlCommand sqlCommand = new SqlCommand();
        if (Connection.State == ConnectionState.Closed)
            Connection.Open();
        sqlCommand.Connection = Connection;
        String updateQuery = "Update  A Set A.Xml_Response= B.Xml_Response From (Select " + Cust_SK + " Cust_SK, '" + SourceName + "' Source_Name,CONVERT(xml, BulkColumn) Xml_Response FROM OPENROWSET(Bulk '" + XmlPath + "', SINGLE_BLOB) [rowsetresults])B Inner join  Response A On  A.cust_sk= B.cust_sk and A.Source_Name=B.Source_Name";
        sqlCommand.CommandText = updateQuery;
        sqlCommand.CommandType = CommandType.Text;
        sqlCommand.ExecuteNonQuery();

        String insertQuery = "Insert Into MSIB_Response(Cust_SK,Source_Name,Xml_Response) Select Cust_SK,Source_Name,Xml_Response From (Select " + Cust_SK + " Cust_SK,'" + SourceName + "' Source_Name,CONVERT(xml, BulkColumn) Xml_Response FROM OPENROWSET(Bulk '" + XmlPath + "', SINGLE_BLOB) [rowsetresults])B Where not exists (Select 1 from Response A where A.cust_sk= B.cust_sk and A.Source_name=B.Source_name)";
        sqlCommand.CommandText = insertQuery;
        sqlCommand.CommandType = CommandType.Text;
        sqlCommand.ExecuteNonQuery();
        Connection.Close();
        sqlCommand.Dispose();

    }

}

----------------------------------
To find CData message and Attributes we need to use this code. It reads data from XML response

XmlNode node = doc.DocumentElement.SelectSingleNode(@"//errors");
        XmlNodeList pNode = node.ChildNodes;
        foreach (XmlNode xn in pNode)
        {
            string Code = xn.Attributes["code"].Value;
            string Field = xn.Attributes["field"].Value;

            XmlNode childNode = xn.ChildNodes[0];
            if (childNode is XmlCDataSection)
            {
                XmlCDataSection cdataSection = childNode as XmlCDataSection;
                // MessageBox.Show(cdataSection.Value);

            }
            //MessageBox.Show(Code + ':' + Field);

        }
-------------------------------------------



read more