Friday, June 3, 2011

SSIS : Export Data to a text file using a package

Open up SQL SERVER Business Intelligence Development Studio
Click on File-->New-->Project-->Integrated Services Project
sometimes "Integrated Services Project" is not visible , make sure that in the Project Types - Business Intelligence Projects is selected. Here is the screen shot.

Fig 1


Once the project name is given (seen in Fig 1) , Drag and drop Data Flow Task from the toolbox on the  Control Flow.( seen in Fig 2)
In the case of toolbox is not visible, go to View-->toolbox

Fig 2


Right click on the Connection Manager as shown below in Fig 3 and select New OLEDB Connection" This will lead you to a screen as shown in Fig 4.

Fig 3


This is where you will fill up all the necessary database connection as shown here in Fig 4 and click the Test Connection to make sure that you are able to connect to the database server as selected .

Fig 4



Now whene you hit OK in the Databse Connection manager you will see OLE DB Source object with green and red arrow in the Control Flow as seen in the Fig 5.

Fig 5



Now double click on the OLE DB Source Object on the Control Flow and the OLE DB Source Editor whill open up as shown below in Fig 6

Highlight the Conenction Manager on the left as you will be filling up the information to exract data from the database now.
In the OLE DB connection Manager dropdown select the Conenction Manager that was created in Step 4.
Select SQL Command in the daat Access mode. We use this mode in this example as we are using a query to get the data from the table.
In the sql Command text box write the query that you are goign to use to extract the data from the database and export out to the flat file.
Fig 6



Now, highlight the columns in the left that will show all the colums that your query will pull and the columns to be written out in the flat file. You can check one of the columns off if you don't want the column to be exported to the file. Leave the error output section as it is . WE are not doing anything with this section in this example.
Fig 7




Now from the tool box, navigate to the Data Flow Destination section and drag and drop the Flat File Destination Item in the Data Flow and expand the green arrow to connect to the Flat File Destination item as shown below picture.
Fig 8



Right click again in the Conenction Mager section and select the New Flat file Connection and thsi will pull up a Flat File Destination editor as show below.
Fig 9



Fig 10











Creating Composite nonclustered index

CREATE NONCLUSTERED INDEX IX_InEligible_ptno_Uploaded
ON [271_InEligible] (ptno, uploaded)



--Table Name [271_InEligible]
indexed columns ptno, uploaded

Wednesday, June 1, 2011

Error:Row handle referred to a deleted row or a row marked for deletion

We got the error "Row handle referred to a deleted row or a row marked for deletion" from a sql job ( that calls a stored procedure) after we moved to sql 2008 R2. When we were running this job in sql 2000 it was running fine.
I had been looking for a solution to fix this and most of the internet articles points to the "mising primary key" issue.
The stored procedure moves the data from TABLE1 to TABLE2. TABLE2 did not have any primary key as this was just used as an intermediate table. I added the primary key to the TABLE2, but the job still continued to fail now and then with the above error.
Upon further look at the code I saw the following code in the stored proc that the job was calling

BEGIN TRANSACITON TRAN1

delete w from DATA2LINK.statusupdates.dbo.workqueue w
INNER JOIN DataUpdate_DTS d2
ON w.ptno = d2.ptno
AND status_Id = 6



SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO Error_handler


--remove any deleted(declined) Data update request from workqueue
DELETE w FROM DATA2LINK.statusupdates.dbo.workqueue w
INNER JOIN DATA2LINK.statusupdates.dbo.DataUpdate d
ON w.ptno = d.ptno
AND w.status_Id = 6
AND d.deleted = 1


The culprit seems to be the second delete query because we are referring already marked for deletion row(in the first query) . I removed the second delete statement .

The job has not failed since then, though it has been only few days since the code change. I will revisit again, but for now I consider this as the resolution for the above error.

Thursday, May 26, 2011

Find the index fragmentation in a database

Whenever a query has a poor performance, the first step I take is to see if the indexes of the table in the query are fragmented badly.

use DB1
GO

SELECT ps.database_id, ps.OBJECT_ID, ps.index_id, si.name, ps.avg_fragmentation_in_percent,
(SELECT distinct so.name FROM sys.objects so INNER JOIN sys.indexes ON so.object_id = si.object_id) ParentTable
FROM sys.dm_db_index_physical_stats (7, NULL, NULL, NULL, NULL) AS ps
INNER JOIN sys.indexes si ON ps.OBJECT_ID = si.OBJECT_ID
AND ps.index_id = si.index_id
WHERE ps.database_id = DB_ID() AND si.name is not null AND
ps.avg_fragmentation_in_percent > 30
ORDER BY ps.avg_fragmentation_in_percent desc
GO


***Here "7" is the database Id of the curent database against which the query is run, in my e.g. DB1

you can find the database id of the current database by the following script
Use DB1
GO
SELECT DB_ID()
GO
*****************************




In the above results, I will be rebuilding the indexes of these tables.

Create non-clustered index online

USE [db1]
GO
CREATE NONCLUSTERED INDEX []
ON [dbo].[Patient_Table] ([HOSPITAL],[SSN],[Code])

GO


(online option is available ony on Entereprise edition)

Monday, May 23, 2011

Top 10 SQL Server 2008 Features for the Database Administrator (DBA)

We migrated to the new version Sql server 2008 R2 early this year but I am afraid that we have not been able to utilize all of the "awesome" features that sql 2008 comes with, though we have used SSIS, DBmail etc but that is in development aspect. Here is a list (yes of course the top 10). More blog posts to follow on the research and hands on experience on each of the follwing features.

1.Activity Monitor
2.[Sql Server Audit]
3.BackUp Compression - ( I love this feature - considering how many databases we backup daily and monthly)
4.Central Managemant Servers
5.Data Collector and Management Data Warehouse
6.Data Compression
7.Policy-Based Management ( as the team grows this will be helpful - to implement standards on that naming conventions.)
8.Predictable Performance and Concurrency
9.Resource Governor
10.Transparent Data Encryption (TDE)

Friday, May 20, 2011

ADD DROP Primary key on a SQL table

alter table DataUpdate_DTS
Add CONSTRAINT PK_ptno PRIMARY KEY (ptno)
GO


alter table DataUpdate_DTS
drop CONSTRAINT PK_DataUpdate_DTS
GO