Excel

Thursday, September 13, 2012

SSIS - Creating SQL server Agent Proxy Account for SSIS Package Execution

       When you are trying to perform some file system task in SSIS or while attempting to read/write some files from the remote locations, the job which uses SSIS package will require the permission to access the network location.
       If your windows NT account has all the rights to access the particular loaction then the package will work fine while runnig the package from BIDS. But the same will not work if you run the package from SQL Server Agent Job. Since the SQL Server Agent will be running on the 'sa' account by default. And this SQL admin will not have permission to access the file sytems.
      So, to make it run from the SQL Server Agent, we have to create new credential (of Windows NT account) which will have all the access to the specific file server or network location.
       And we have to create Proxy for the SQL Server Agent which uses the newly created credential. And the Proxy should be associated with the Subsystem SSIS.

 Please find the detailed steps written in the following blogs :
http://www.vincepergolizzi.com/2012/03/setting-up-sql-server-agent-proxies-for-ssis-packagese/
http://www.mssqltips.com/sqlservertip/2163/running-a-ssis-package-from-sql-server-agent-using-a-proxy-account/
Thanks for the authors of those blogs. :-)

Sunday, September 9, 2012

Search the Column on the DB (SQL Server)

          To find a particular column from all the tables available on the particular  DB, We can use the following sql query :

SELECT tab.name AS table_name,
col.name AS column_name
FROM sys.tables AS tab
INNER JOIN sys.columns col ON tab.object_id  = col.object_id
WHERE col.name LIKE '%SalesDoc%'  -- Column Name

Tuesday, May 29, 2012

SSRS - Implementing Drill Through action in the same RDL



Scenario:
           User wants a SSRS Chart report which will display the Product Category wise Sales in the initial view. On clicking of the bar chart, the report should display the Product wise Sales of that selected Category.
          We can enable this drill through action in a single RDL as given below.

Note :  I haven't given the SSRS chart creation steps detaisl by considering that the reader knows how to create SSRS Chart reports. 

To implement the above said requirement, we can do some tweaks in the SQL query if the report data set and achieve the same.

The given below sample query can be used in the data set, which will return the Category wise sales if the in parameter (@ProdCategory) value is not available in the Category table.

if exists(
select 'x' from dbo.Dim_Category  where Category=@ProdCategory
    )
begin
select ProductName as Item ,SalesAmt as Sales_Amt from dbo.Dim_Category where Category=@ProdCategory
end
else
begin
select Category as Item,sum(SalesAmt) Sales_Amt from dbo.Dim_Category group by Category
end

Follow the given below steps:

-In the above created Dataset, @ProdCategory is parameter and its default value is 'a' initially.

-Create a Chart report and drag the field Item in to the category field and Sales_Amt into the Data field of the chart.

-In the Action pane write an expression to Go to this same report in the Chart series

-Pass the Field ‘Item’ as a parameter to the report

-On clicking on the Category that Category will be taken as a new parameter. And new result set for the selected Category (Products under the selected Category) will be produced in the same report.

-Now, when the report initially gets opened, the default parameter will be passed to the data set and the value will not be available in the Dim_category table, and the report will generate Category wise Sales Amount in the Chart.

-If the user clicks on the Category series, then the selected Category will be passed as an in parameter to eth data set and the selected categories Products will be returned by the data set and the chart will provide the Product wise Sales amount. 

Saturday, February 4, 2012

To Find Duplicate Rows using Common Table Expressions and perform DML operations



Handling the Duplicates using CTE and Row_Number()  - (ie, To Delete or Update the Duplicates from SQL server Tables Using CTE, Row_Number() ) 

            Just to select the duplicate rows from a table, we can use the following kind of simple query. The below mentioned query will return duplicated rows based on the Product_Id :
                     
SELECT Product_id,
      COUNT(Product_id) AS NumOccurrences
FROM InventoryDetail
      GROUP BY Product_id
      HAVING ( COUNT(Product_id) > 1 )

If you want to Delete or Update the duplicate, we can use the following sample which will use the CTE (Common Table Expression) , Row_Number function and the MDL statement (Delete/ Update).
            
                      
WITH CTE (Product_id,Time_Id, DuplicateCount)
AS
(    
      SELECT Product_id,Time_Id,
                  ROW_NUMBER() OVER(PARTITION BY Product_id,Time_Id ORDER BY Product_id) AS DuplicateCount
      FROM InventoryDetail
)

DELETE
      FROM CTE
WHERE DuplicateCount > 1

Sunday, December 18, 2011

SSRS : New Line Expression

SSRS : New Line Expression

       If you want to display the text in a new line at the text box, the following expression can be used.

="Line 1 " + VBCRLF + "Line 2"

Ex : =Fields!AddressLine1 + VBCRLF + Fields!AddressLine2 + VBCRLF + Fields!AddressLine3

OP:
 Address1
 Address2
 Address3


Note: If VBCRLF is used first or last part the expression, it has no impact on result. If you want a line in the first or last, then use one more extra VBCRLF.

Friday, December 16, 2011

SSIS- Pivot transformation


The following article neatly explains how to use Pivot Transformation in SSIS


This tutorial provides a complete pivot sample on AdventureWorks sample SQL Server database with SSIS 2008...
Assume you want to find order quantities for each product by year.
you may use this query to get the result:
 select
    Product.ProductID,
    Product.Name,
    year(SalesOrderHeader.OrderDate) as OrderYear,
    sum(SalesOrderDetail.OrderQty) as OrderQuantity
from Sales.SalesOrderDetail
inner join Production.Product
on SalesOrderDetail.ProductID=Product.ProductID
inner join Sales.SalesOrderHeader
on SalesOrderDetail.SalesOrderID=SalesOrderHeader.SalesOrderID
group by Product.ProductID,Product.Name,year(SalesOrderHeader.OrderDate)
order by Product.Name,year(SalesOrderHeader.OrderDate)
and this is result of query above:

Now , create a new SSIS package
Add a Data Flow Task
in dataflow tab :
Add a OLE DB Source and connect it to AdventureWorks database in your SQL Server
write the query in SQL command Text of oledb data source,
then add a PIVOT Transformation after OLE DB Data Source

in Pivot transformation advanced editor , go to input columns tab and select all input columns

then go to input and output properties tab, select pivot default input, under input columns you will find all columns which you selected in previous tab.
the only property you must set for each input column is the PivotUsage.
this describe the values you can use in PivotUsage property:

So , in this example , PivotUsage property for each input column will be as below:
ProductID           0
Name                 1
OrderYear           2
OrderQuantity     3
Notice that you must have at least one input column with PivotUsage 2, one input column with PivotUsage 3,one input column with PivotUsage 0 OR 1 at least in your pivot transformation.

After setting input columns, go to Pivot Default output , and under output columns add these columns:
ID, Name , 2001 ,2002 , 2003 , 2004
ID and Name columns will show the exact values form ProductID and Name input columns.
So, set SourceColumn property of ID output column to LineAgeID of ProductID input column.
Note: this is critical to use the lineageID value of input columns not the ID value. if you use ID value you will face error.
but how can you find lineageID of input columns?
simply select ProductID under input columns and see the lineageID value there.
Note: the lineageID is different on each machine, so you can not use exact lineage numbers used in this example.you must use your lineageID values.
this is lineageID of ProductID input column:
and this is the SourceColumn property in ID output column:

leave the PivotKeyValue of ID and Name output column as empty.
Do above steps for Name output column with Name input column too
Now it's Pivot columns turn, select 2001 under output columns
this columns will show the results for the year 2001, so enter 2001 in PivotKeyValue property.
and set SourceColumn with lineageID of OrderQuantity input column.
In fact you must set SourceColumn property with the lineageID of the input column which has the PivotUsage 3 value.
in this example , lineageID of OrderQuantity is 69 (Remember you must use your own lineageIDs)

So, we set SourceColumn with 69 in 2001 output column:

Now , do the same for 2002,2003,2004 columns.the only difference is that you must set PivotKeyValue to 2002,2003,2004 in these columns. butlineageID will be the same in all 2001 - 2004 columns.
Pivot configuration finished now.
Just add a destination, and fill the result of Pivot in destination.
I used a RecodeSet Destination with a Grid Data Viewer to show the result,
you can add Grid dataviewer by right click on green arrow between pivot transformation and  your destination, then select data viewers, then add Grid.
this is the full desing of my DataFlow Task:

and this is the final Result:

 Courtesy : Reza Rad (www.rad.pasfu.com)