SQL Server Business Intelligence: August 2008

Friday, August 29, 2008

Step-By-Step: How to move SQL Server data efficiently with the Bulk Copy Program

Step-By-Step: How to move SQL Server data efficiently with the Bulk Copy Program


Takeaway: Learn to use the Bulk Copy Program (BCP), a command-line utility, to import and export large amounts of data in and out of SQL Server databases


The Bulk Copy Program (BCP) is a command-line utility that ships with Microsoft SQL Server. With BCP, you can import and export large amounts of data in and out of SQL Server databases quickly and easily. Any DBA who has utilized this functionality will agree that BCP is an essential tool.

BCP 101
You access the BCP utility from the command prompt. Here's the simple syntax:
bcp {dbtable | query} {in | out | queryout | format} datafile
[-n native type] [-c character type] [-S server name] [-U username]
[-P password] [-T trusted connection]

The command example I'm going to use starts with bcp followed by a fully qualified table name (database name, table or object owner, table or object name). For example, if you want to export the authors table, as part of the dbo group from the pubs database, you supply the full table name pubs.dbo.authors. Next, you use an in or out argument to specify whether you want BCP to copy data into or out of a database. You then specify the location of the datafile on your database server.

At this point, our BCP statement looks like this:
bcp pubs.dbo.authors out c:\temp\authors.bcp

Note
When using BCP, don't forget that the switches are case-sensitive. If you don't apply the right case, the BCP statement won't work correctly or will fail.

Next, we'll add some of the basic command-line options. The –n switch specifies native SQL Server format. The –c switch is used when formatting the file using Char as a datatype. If you use this switch, you can easily open your file with Excel. The –S switch enables you to add the server/instance name. The –U switch allows you to add the name of the login used to connect to SQL Server. The –P switch lets you add the password of the –U switch, and the –T switch is for establishing a trusted connection to your SQL Server. Figure A shows our completed BCP statement.

Figure A
Sample BCP out command


If we were importing data to the authors table, our BCP command would look like this:
bcp pubs.dbo.authors in c:\temp\authors.bcp -c -Sstevenw -Usa –P

That gives you an idea of the basics of the BCP utility, but there are plenty more switches at your disposal. In all, BCP supports 27 switches, which are shown in Listing A.
Listing A
bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]                  [-e errfile]
  [-F firstrow]             [-L lastrow]                     [-b batchsize]
  [-n native type]          [-c character type]              [-w wide character type]
  [-N keep non-text native] [-V file format version]          [-q quoted identifier]
  [-C code page specifier]  [-t field terminator]            [-r row terminator]
  [-i inputfile]            [-o outfile]                     [-a packetsize]
  [-S server name]          [-U username]                    [-P password]
  [-T trusted connection]   [-v version]                     [-R regional enable]
 [-k keep null values]     [-E keep identity values]         [-h "load hints"]


With this many switches available, I recommend that you play around with them to see which ones you might want to use. For example, the –e switch is handy because it will create an error file you can look at if your BCP command returns errors. Here is an example:
bcp pubs.dbo.authors out c:\temp\steventest.txt -n -Sstevenw -Usa -P
-eC:\temp\error.txt

For more information on how to use the various switches, see SQL Server Books Online.

BCP and database objects
Let's create a new database called pubs2 and use BCP to move all the data from the pubs database into it. First, we need to create the database. Then, we'll generate the SQL script, run it on pubs2, and export the data from pubs into pubs2.

Start by creating the database in SQL Server Enterprise Manager and name it pubs2. Next, select pubs2, right-click on it, and choose Generate SQL Scripts from the All Tasks menu. Now you are ready to set your scripting options. Click Show All and then select the Script All Objects check box, as shown in Figure B.

Figure B
The Generate SQL Scripts dialog box


Go to the Options tab and select all the check boxes under Table Scripting Options, as we've done in Figure C. You can preview your script from the General tab or click OK to create and save the script.

Figure C
Table Scripting Options


Note
By performing the above, we are making a complete replica of the Pubs database. We could also have scripted out only one table or multiple tables. You can make the choice, depending on the task at hand.

Now, open Query Analyzer and run the newly created script (Figure D).
http://img.com.com/i/tr/cms/contentPics/r00220020620wrr01_09.gif
Run the script in SQL Query Analyzer.
After the script runs, the database objects and indexes will have been successfully created (Figure E).

http://img.com.com/i/tr/cms/contentPics/r00220020620wrr01_10.gif
The database objects have been created in SQL Server Enterprise Manager

To export the authors table out of pubs and import it into pubs2, we'll use the BCP statements we learned earlier:
bcp pubs.dbo.authors out c:\temp\pubauthors.bcp –n –Sstevenw –Usa –P
bcp pubs2.dbo.authors in c:\temp\pubauthors.bcp –n –Sstevenw –Usa –P

Then, open the SQL Query Analyzer and run the following statements on the pubs2 and pubs databases:
Use pubs2
Select * from authors
Select count(*) from authors

Use pubs
Select * from authors
Select count(*) from authors

This allows you to see the full process of moving data come full circle.

Summary
Most DBAs today use DTS, Database Restore, and/or attaching and detaching databases to copy data from one server to another. All these tools will get the job done. But if you want to copy large amounts of SQL Server data quickly, BCP is still one of the best tools available. Once you get past the initial learning curve, you'll find that BCP offers a highly efficient way to copy data from one database to another.

Thursday, August 21, 2008

SSIS 101: Object Variables, ResultSets, and Foreach Loop Containers

SSIS 101: Object Variables, ResultSets, and Foreach Loop Containers

By Andy Leonard, 2008/08/21

Introduction

SQL Server 2005 Integration Services (SSIS) is a flexible data-related development platform. In all computer languages flexibility is proportional to complexity and SSIS is no exception.

In this article I will discuss three parts of SSIS that I often use together:

· The Execute SQL Task Resultset property

· Variables of the Object data type

· Foreach Loop Containers

The Execute SQL Task Resultset Property

Execute SQL Tasks provide lots of cool functionality in SSIS. A lot of the power of this task is misunderstood or underused in SSIS packages – even by experienced SSIS developers. The task is able to execute any SQL statement using a host of data access providers including:

· Excel

· OLEDB

· ODBC

· ADO

· ADO.Net

· SqlMobile

ADO types
Figure 1: Execute SQL Task ConnectionType Property Options

I recently learned from Carol Rucker how to improve the performance of SQL Server stored procedure calls. To do this use the ADO.Net ConnectionType – connected to an ADO.Net Connection Manager. Set the IsQueryStoredProcedure property to True and enter a stored procedure name in the SQLStatement property as shown in Figure 2, and configure the Parameters for the stored procedure using parameter names as show in Figure 3.

Configuration
Figure 2: Configuring a Stored Procedure Call

Configuring 2
Figure 3: Configuring Stored Procedure Parameters

Executing a SQL Server stored procedure using this method is about twice as fast as executing an Exec T-SQL statement as demonstrated in the Execution Results shown in Figure 4. Thanks Carol!

Execution Results
Figure 4: Execution Results of ADO.Net Stored Procedure vs. OLE DB Exec T-SQL Statement

Like everything else related to database development, this is pretty simple and cool – once you know about it! J

When you execute a SQL statement using an Execute SQL Task, whether it’s a stored procedure or not, you sometimes get the results of a Select statement. SSIS can store these results in a variable of the Object data type.

Let’s walk-through an example: Create a new SSIS project and rename the Package.dtsx package object to “Resultsets_Objects_ForEach.dtsx”. Drag an Execute SQL Task onto the Control Flow. Double-click the task to open the editor. Leave the ConnectionType property set to OLE DB and click the Connection property dropdown to select <New Connection…>.

On the Configure OLE DB Connection Manager form, create a connection to a local database. In this example, I am using the SQL Server 2005 AdventureWorks database available from www.codeplex.com. After the Connection Manager is configured, click OK to return to the Execute SQL Task editor.

Add the following T-SQL statement to the SQLStatement property:

 Select
   ContactID
  ,Title
  ,FirstName
  ,MiddleName
  ,LastName
  ,Suffix
  ,EmailAddress
  ,Phone
 From Person.Contact

Listing 1: T-SQL Select Statement

When I’m developing SSIS projects I use SQL Server Management Studio (SSMS) to develop T-SQL statements. After they’re working like I want, I copy them to the clipboard and paste them into the SQLStatement property of the Execute SQL Task. One “gotcha” in SSIS is the property value textbox will allow you to paste only the first line of a multi-line statement. Since you can only see the first line in this textbox, many developers believe they have pasted the entire multi-line statement into the property when this is not the case. To be safe, always click the ellipsis (the button with three dots) on the right side of the SQLStatement property value textbox to open the Enter SQL Query form as shown in Figure 5:

Query Form
Figure 5: Using the Enter SQL Query Form

Click the OK button to close the Enter SQL Query form and store the T-SQL query in the Execute SQL Task’s SQLStatement property.

To return the results of this query into an SSIS object variable, configure the Resultset property of the Execute SQL Task – setting it to “Full result set” as shown in Figure 6:

Sttings
Figure 6: Execute SQL Task Resultset Property Options

The default Resultset property option is None, indicating no results from the T-SQL stored in the SQLStatement property will be captured. The other options are:

· Single Row – indicating the results will contain one or more columns but only one row.

· Full result set – indicating the results will contain one or more columns and one or more rows.

· XML – indicating the results will be in XML format.

The Full result set option returns an ADO.Net dataset object, and SSIS variables of the Object data type can hold a dataset. To configure the variable, select the Result Set page in the Execute SQL Task and click the Add button. Replace the text “NewResultName” in the Result Name column with the ordinal 0. In the Variable Name column, select <New variable…> from the dropdown.

When the Add Variable form displays, click the Container dropdown to configure the scope of the variable. Set the variable scope to the Package as shown in Figure 7:

Package Scope
Figure 7: Selecting Package Scope in the Add Variable Container Dropdown

One suggested improvement for SSIS is to put this dialog in the Variables window, allowing developers to select or edit Variable scope without having to delete and re-create SSIS variables.

Click the OK button to accept the Package scope for this variable, returning to the Add Variable form. Change the variable name to People and set the Value type (data type) property to Object as shown in Figure 8:

Properties
Figure 8: Configuring the People Variable

Click the OK button to complete the People variable creation and configuration as shown in Figure 9:

Variable config
Figure 9: Configured People Variable for the Result Set

We’re done configuring the Execute SQL Task – click the OK button to close the editor. Our Execute SQL Task will execute a T-SQL statement against the AdventureWorks database, return a result set, and push the result set into an Object variable named People.

We can test the execution of this statement by right-clicking the task on the Control Flow and selecting Execute Task. Green boxes are a good thing:

Success
Figure 10: Successful Execution of Execute SQL Task

Shredding User::People

The People variable is in the User namespace. You can see this in Figure 8. The namespace can also be viewed by clicking the Choose Variable Columns button on the Variable viewer toolbar:

Results
Figure 11: The Choose Variable Columns Button on the Variables Viewer

Walking through a multi-value object is referred to as shredding. In this case we will use a Foreach Loop Container to shred the People variable (although phrasing that differently sounds macabre…). Drag a Foreach Loop Container from the Toolbox onto the Control Flow. Click on the Execute SQL Task to connect a Success Precedence Constraint from the Execute SQL Task to the Foreach Loop Container as shown in Figure 12:

Flow
Figure 12: Success Precedence Constraint

Double-click the Foreach Loop Container to open the editor and view the Collection page. The Foreach File Enumerator is selected by default. The available enumerators are:

· Foreach File – iterates files in a specified directory. You can retrieve all the *.CSV files in a folder.

· Foreach Item – iterates items in a collection. You can loop through the rows and columns in an Excel spreadsheet.

· Foreach ADO – iterates the rows in an ADO recordset or an ADO.Net dataset.

· Foreach ADO.Net Schema Rowset – iterates database schema objects.

· Foreach From Variable – iterates a collection in a variable. You can shred a string array contained in an object variable.

· Foreach NodeList – iterates XML.

· Foreach SMO – iterates SQL Management Objects (SMO) objects. Use this to loop through database objects, but can also be used to iterate SQL Server instances on the network.

Listing
Figure 13: Enumerator List on the Foreach Loop Container Collection Page

Select the Foreach ADO Enumerator. In the ADO object source variable dropdown, select User::People. The Enumeration Mode defaults to “Rows in the first table”. Since we only have one table that will work well for our example. This option works with ADO recordsets and ADO.Net datasets, but the other options – “Rows in all tables” and “All tables” – work only with ADO.Net datasets (because ADO recordsets contain only a single table).

On the Variable Mappings page, click the Variable dropdown (in the first row) and click <New variable…>. Configure the variable as shown in Figure 14:

Properties
Figure 14: Creating the LastName String Variable

Click the OK button to complete the variable creation process.

Set the Index for User::LastName to 4. “Why 4?” I hear you asking. It is because the LastName column in our SQL query in Listing 1 is the fifth listed in the Select clause. Since the ordinal of the fifth item in this zero-based list is 4, we use 4 to identify this column:

Mapping
Figure 15: Configuring the LastName Variable Mapping

Click the OK button on the Foreach Loop Container editor.

Drag a Script Component into the Foreach Loop Container and double-click it to open the editor. Click the Script page and add LastName (variable names are case-sensitive) to the ReadOnlyVariables property of the Script Task as shown in Figure 16:

Variables
Figure 16: Adding the LastName Variable to the Script Task ReadOnlyVariables Property

Click the Design Script button to open the Visual Studio for Applications (VSA) editor. Replace the default code in Public Sub Main() with the following code:

       PublicSub Main()
             '
             ' Add your code here
         '
         Dim sLastName As String = Dts.Variables("LastName").Value.ToString
         Dim sMsg As String
         sMsg = "LastName: " & sLastName
         MsgBox(sMsg)
       Dts.TaskResult = Dts.Results.Success
      End Sub


Listing 2

This script reads the value of the User::LastName variable into a script String variable named sLastName. Next, a String variable named sMsg is created. sMsg is next populated with the value “LastName: “ and the value of the sLastName variable. This is then displayed in a message box (MsgBox).

Close the VSA editor and click the OK button on the Script Task editor.

Time to Test

Execute the SSIS package in the debugger by clicking the green VCR-style Play button on the toolbar or by pressing the F5 key.

Display the variable
Figure 17: Displaying the First LastName Value

Figure 17 illustrates the package is operating as expected.

Conclusion

In this example, we created an Object variable named People and used an Execute SQL Task to push the results of a T-SQL query into this variable. We used a Foreach Loop Container to shred the dataset contained in the People variable, pushing the contents of one field (the LastName column) into a String variable named LastName. Finally, we displayed the value of the LastName variable with each iteration of the Foreach Loop Container.

 

Resources:

SSIS101_Resultsets_Objects_ForEach_Solution.zip

Wednesday, August 6, 2008

Microsoft BI Platform

Monday, August 4, 2008

Managing SSIS Service

Help JumpStart.TV to bring us more knowledge and Videos on SSIS.














If you liked that, join Jumpstart TV Inc., who has All Rights Reserved

SQL Server 2008 SSRS New Data Visualizations


The new Gauge data region is most often used to provide a high-level summary of your data by highlighting key performance indicator (KPI) values. The gauge uses a pointer to show a single value. You can add a range to highlight a subset of values on your scale and control the size and positioning of the scale on the gauge to create different visual effects.

The new Chart data region supports a richer set of data visualization features. The new chart types include bar/column cylinder, pyramid, funnel, polar, radar, stock, candlestick, range column, range bar, smooth area, smooth line, stepped line, and box plot chart types. There is also built-in support for Pareto and Histogram charts.

There are many more enhancements that have been made to the charting functionality, so check out the BOL link below and read all about them along with other new features.

Check out a screencast about the new Data Visualizations in SQL Server 2008 here.

Read more about this in the SQL Server 2008 BOL 'What's New in Report Authoring'.

Video: Did you know? SQL Server 2008 SSRS New Data Visualizations

Sunday, August 3, 2008

SQL Server 2005 Business Intelligence Virtual PC


Original Post
With thanks to
Dan's Blog

Here is the list of all the features present in it.
Virtual PC

Instructions
Business Intelligence VPC Release 6.part01.exe
Business Intelligence VPC Release 6.part02.rar
Business Intelligence VPC Release 6.part03.rar
Business Intelligence VPC Release 6.part04.rar
Business Intelligence VPC Release 6.part05.rar
Business Intelligence VPC Release 6.part06.rar
Business Intelligence VPC Release 6.part07.rar
so if you are
looking for the password it is the typical Microsoft demo one
and the same one that was used on the last VPC,
pass@word1

Gauge Your Data Warehousing Maturity

Gauge Your Data Warehousing Maturity

By Wayne W. Eckerson

Many of us have managed data warehousing projects for years. Some have delivered highly strategic systems that are treasured by users and valued by top executives. Others have struggled to sustain interest and funding in their data warehouses even though users are crying out for better, more accurate information.

What separates successful from struggling solutions? How does your data warehousing initiative compare to others in the industry? What will it take to get your data warehouse to the next level?

Many data warehousing managers ask these questions today. Unfortunately, there are no quick or easy answers. But to provide some guidance, TDWI has developed a data warehousing maturity model that you can use to benchmark your progress. The model provides a quick way for you to gauge where your data warehousing initiative is now and where it needs to go next.

Read out more
http://www.tdwi.org/publications/display.aspx?ID=7199