Thursday, June 19, 2008

SQL Server 2005 :Insert a flat file in a OLTP table

This article will show you 4 different ways to import a flat file in a SQL Server table.

The first question that you must have is:" Have I to manipulate some of my data?"

If no, you can use the following methods:

BULK INSERT

BULK INSERT is a T-SQL command that allows to import data from a flat file into a table.

Here an example:

BULK INSERT .dbo.[TheDestinationTableName] FROM 'FullPathToTheFlatFile'
WITH
(
FIELDTERMINATOR = '',
ROWTERMINATOR = '\n'
)

Here the end character between each column is '' and the final character to pass to the next record is '\n'

bcp

bcp can copy Microsoft® SQL Server™ data to or from a data file. bcp is a command prompt utility and not a T-SQL command. bcp is used in a batch file or in your .NET application, for instance. The bcp utility is written using the ODBC bulk copy application programming interface (API).

Import/Export Tasks

As the Copy tool of SQL Server 2005, it exists also an import tool in it.

From SSMS, connect you on your destination database (which one that contains the destination table). Right click on this database and select Tasks --> Import Data... The Sql Server Import and Export Wizard window opens itself.

Select Flat File Source as Datasource. Specify the File path and the formatting. You can preview the result in the Columns tab for the columns name and the data in the Preview tab.

Click Next and follow the steps.

if you have a manipulation:

In this case, you will need to use a tool that will make the changes. This tool is SSIS. SSIS is provided with the Developer and Enterprise version of SQL Server 2005.With SSIS you can create a connection to your flat file, make some changes on the values in your DataFlow and importing them in a table thanks to an OLE DB connection.

I hope that this post will help you ...

Wednesday, May 21, 2008

SSAS 2005 : My Best Practices

1. OLTP database

2. OLAP database

  • for query performance, use user defined hierarchy as much as possible except if you use custom rollup. In this case, Parent-Child hierarchies are more performant.
  • Case When mdx has been more performant than IIF in some case. When I used the Case When in a calculation of my cubes the time query was faster. Personaly, I don't hesitate to compare the response time of my query with an IIF statement and with a case when in the cube calculations part.
  • Decrease the number of cube calculation. Cause these calculations are evaluated on the fly.

In plus of my best practices, you can find more on the microsoft website (http://www.microsoft.com/technet/prodtechnol/sql/bestpractice/olapdbpssas2005.mspx)

Monday, May 19, 2008

SSAS 2005 : Aggregation...Important thing to know!

Aggregations are pre-calculated summaries of data. Specifically, an aggregation contains the summarized values of all measures in a measure group by a combination of different attributes.

How aggregations are used ?

To understand aggregations, consider the cube shown in Figure.


This is a small cube consisting of two dimensions, Date and Product, and one measure, Sales Amount.

The cube has three aggregations contributed by different combinations of attribute hierarchies.
  • The all-level aggregation stores the grand total Sales Amount.
  • The intermediate aggregation stores the total Sales Amount for the year 2004 and Road Bikes product subcategories.
  • Finally, assuming that the fact table references Month and Product dimensions, there is also a fact-level aggregation for July 2004 and product Road-42.


Suppose a query asks for the overall Sales Amount value. The server discovers that this query can be satisfied by the all-level aggregation alone. Rather than having to scan the partition data, the server promptly returns the result from the pre-calculated Sales Amount value taken from this aggregation. Even if there isn't a direct aggregation hit, the server may be able to use intermediate aggregations to derive the result. For example, if the query asks for Sales Amount by product category, the server could use the intermediate aggregation, Year 2004 and Subcategory Road Bikes, to derive the total.


Why not create all possible aggregations?


The short answer is that this will be counter-productive because all those aggregations will require enormous storage space and long processing times. The reason is that aggregations are created when the cube is processed.

Last thing, to help the Aggregation Design Wizard make a correct cost/benefit analysis, be sure to keep the EstimatedCount (attribute property) and EstimatedRows (Measure group property) counts up to date.

To make the good estimation you can use a very useful tool named BIDSHelper .

Tuesday, May 13, 2008

SQL Server 2005 : How to copy a database using SQL Server 2005?

To do this manipulation, you have a lot of way.

Backup-Restore? too long

Create an SSIS package using BIDS? too much manipulation


The solution is ...

the integrated copy tool of SQL Server 2005.


This tool can copy one or several OLTP database and "paste" them on a server. It creates a package and launch the SQL server Agent that executes this one.


You can find it by right-clicking on a database in your SSMS.


After click on Copy Database.

The Copy Database Wizard opens.
1st step: select the source server and the authentification mode
2nd step: select the destination server and the authentification mode
3rd step: choose the transfer method (Detach-Attach or the SQL Management object method to keep the source db online)
4th step: choose the database to move/copy
5th step: specify file names and wether to overwrite database(s) at the destination
... creation of the package that will copy/move the database
Easy and useful!






Friday, May 9, 2008

SSIS 2005 : How to write annotations in a package?

In the SSIS Designer, right-click on the design surface of your Control Flow, Data Flow or Event Handler tab and then click Add annotation.

After you have created your annotation, you can either change the font formatting or group it to an object of your design surface. How to do it? --> right-click on it :-)

Wednesday, April 30, 2008

SSRS 2005 : How to create a dataset of member from an OLAP database

Requirements:
Having a knowledge of MDX

Having a knowledge of SSRS (being able to create a report, a datasource and a report parameter)

Having an OLAP database with at least one cube and one dimension

Creating the dataset
  • Name = your dataset name
  • Datasource = your OLAP datasource
  • Command type = Text
  • Query string =


WITH
MEMBER [MEASURES].[ParameterValue]
AS Axis(1).Item(0).Item(0).Dimension.CurrentMember.UNIQUE_NAME


MEMBER [MEASURES].[ParameterCaption]
AS String( Axis(1).Item(0).Item(0).Dimension.CurrentMember.Level.Ordinal * 1 , ' ' ) + Axis(1).Item(0).Item(0).Dimension.CurrentMember.Member_Caption


--the 5 last members
Set [theSet] as Order( Tail( [DimensionName].[HierarchyName].Members, 5), [DimensionName].[HierarchyName].CurrentMember.UniqueName, Asc)


SELECT
{ [ParameterCaption]
, [ParameterValue]
} ON COLUMNS
, Non Empty [theSet]
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME on ROWS
from [CubeName]

  • Click on the OK button to validate

To use this list of member, you have to create a new Report Parameters.

You can use the following picture as example

Good Luck!

SSRS 2005 : Using Stored Procedure with Report Parameters

Requirements:
Having a knowledge of T-SQL
Having a knowledge of SSRS (being able to create a report, a datasource and a dataset)

1. Creating the stored procedure.

The following example receives 2 input variables (a month and a 2-digit year) and returns a set of the previous month until the month of the previous year.

A picture is better than 1000 words!


So now the code.

CREATE PROCEDURE [dbo].[STP Name]
@ActMonth varchar(15),@ActYear varchar(4)
AS
BEGIN

DECLARE @id_Selected_Month tinyint

CREATE TABLE #Tmp_Table
(
[id_Month] [tinyint] NOT NULL,
[Month_Name] [varchar](3) NOT NULL
)

INSERT INTO #Tmp_Table VALUES (1,'Jan')
INSERT INTO #Tmp_Table VALUES (2,'Feb')
INSERT INTO #Tmp_Table VALUES (3,'Mar')
INSERT INTO #Tmp_Table VALUES (4,'Apr')
INSERT INTO #Tmp_Table VALUES (5,'May')
INSERT INTO #Tmp_Table VALUES (6,'Jun')
INSERT INTO #Tmp_Table VALUES (7,'Jul')
INSERT INTO #Tmp_Table VALUES (8,'Aug')
INSERT INTO #Tmp_Table VALUES (9,'Sep')
INSERT INTO #Tmp_Table VALUES (10,'Oct')
INSERT INTO #Tmp_Table VALUES (11,'Nov')
INSERT INTO #Tmp_Table VALUES (12,'Dec')

SET @id_Selected_Month = (SELECT [id_Month] FROM #Tmp_Table WHERE [Month_Name] = LEFT(@ActMonth,3))

CREATE TABLE #Tmp_OrderdedTable
(
[id_Act_Month] [tinyint] IDENTITY(1,1) NOT NULL,
[Month_Name] [varchar](10) NOT NULL
)

INSERT INTO #Tmp_OrderdedTable SELECT [Month_Name] FROM #Tmp_Table WHERE id_Month > @id_Selected_Month

INSERT INTO #Tmp_OrderdedTable SELECT [Month_Name] FROM #Tmp_Table WHERE id_Month <= @id_Selected_Month CREATE TABLE #Tmp_ActMonthTable ( ActMonth_1 [varchar](6) NULL, ActMonth_2 [varchar](6) NULL, ActMonth_3 [varchar](6) NULL, ActMonth_4 [varchar](6) NULL, ActMonth_5 [varchar](6) NULL, ActMonth_6 [varchar](6) NULL, ActMonth_7 [varchar](6) NULL, ActMonth_8 [varchar](6) NULL, ActMonth_9 [varchar](6) NULL, ActMonth_10 [varchar](6) NULL, ActMonth_11 [varchar](6) NULL, ActMonth_12 [varchar](6) NULL ) DECLARE @First_Month_ShortName varchar(6), @Month_ShortName varchar(6), @counter tinyint, @idMonth tinyint, @Output varchar(100), @ColumnName varchar(11) /* Act Month - 1 */ IF(@id_Selected_Month = 1)--if equal to January SET @First_Month_ShortName = (SELECT Month_Name FROM #Tmp_OrderdedTable WHERE [id_Act_Month] = 11)+ ' 0' + CAST((@ActYear - 1)AS varchar(2)) ELSE SET @First_Month_ShortName = (SELECT Month_Name FROM #Tmp_OrderdedTable WHERE [id_Act_Month] = 11)+ ' ' + @ActYear INSERT INTO #Tmp_ActMonthTable (ActMonth_1) VALUES(@First_Month_ShortName) /* Act Month - 2 to Act Month - 11 */ SET @counter = 1 SET @idMonth = 10 WHILE @counter <= 10 BEGIN SET @counter = @counter + 1 SET @ColumnName = 'ActMonth_' + CAST(@counter AS varchar(2)) IF((SELECT [id_Month] FROM #Tmp_Table WHERE [Month_Name] in(SELECT Month_Name FROM #Tmp_OrderdedTable WHERE [id_Act_Month] = @idMonth)) > @id_Selected_Month )
SET @Month_ShortName = (SELECT Month_Name FROM #Tmp_OrderdedTable WHERE [id_Act_Month] = @idMonth) + ' 0' + CAST((@ActYear - 1)AS varchar(2))
ELSE
SET @Month_ShortName = (SELECT Month_Name FROM #Tmp_OrderdedTable WHERE [id_Act_Month] = @idMonth) + ' ' + @ActYear

SET @Output = 'UPDATE #Tmp_ActMonthTable SET ' + @ColumnName + ' = ' + '''' +@Month_ShortName + ''' WHERE ActMonth_1 = ''' + @First_Month_ShortName + ''''
EXEC(@OutPut)

SET @idMonth = @idMonth - 1
END


/* Act Month - 12 */
SET @Month_ShortName = LEFT(@ActMonth,3) + ' 0' + CAST((@ActYear - 1)AS varchar(2))
UPDATE #Tmp_ActMonthTable SET ActMonth_12= @Month_ShortName WHERE ActMonth_1 = @First_Month_ShortName

/* Result */
SELECT * FROM #Tmp_ActMonthTable


/* Free space */
DROP TABLE #Tmp_ActMonthTable
DROP TABLE #Tmp_OrderdedTable
DROP TABLE #Tmp_Table

END

2. Configuring the dataset

  • In the Data tab of your report, create a new dataset
  • In the Dataset window, change the dataset name, the datasource if needed, the command type to 'StoredProcedure' and write the name of your stored procedure in the 'Query String' pane
  • Go to the Parameters tab of the same window and copy the following parameters name and value :
    Name Value
    @ActMonth =TRIM(Parameters!Period.Label)
    @ActYear =RIGHT(Parameters!Year.Label,2)

Period is a list of month and Year a list of year from 2005 until 2009 and are both parameters of the report. The Label function is used to return the parameter caption.

  • Click on the OK button, pass to the 'Generic Query Designer' and click on the exclamation point to execute your stored procedure. In the new window enter March as @ActMonth value and 08 as @ActYear value and then click the OK button.

Voila, you have linked the Report parameters to your stored procedure.

Now you can use the fields of your dataset in the Layout tab and see the results in the Preview tab


Have fun!

Monday, April 28, 2008

SSRS 2005 : How to format a textbox using a conditional formatting?

  • In the layout tab, select the textbox to modify
  • click on the arrow of the FontSize property to deploy the combobox
  • click on Expression to open the Edit Expression window
  • In this window, you can write a iif clause for instance as

IIF(Fields!KPI.Value ="The world","10pt","12pt")


  • You can do the same sort of formatting with the font, the color, the value, etc.