Checking TSQL in SQL Server Reporting Services Datasets

Recently I have been checking all the Dependencies on the Reports in SSRS(Sql server Reporting Services) because of a Major deployment on the ETL. The ETL change was to accomodate the transfer of a few columns from old Table TableA to the new table TableB. Some of the Datasets in the Reports are Stored Procedures and so I used sp_depends procedure to get the list of dependencies for TableA.

I also used the following TSQL to get list of objects in any database where the command text includes TableA.


DECLARE @tablename VARCHAR(100)
SET @tablename = ‘%tableA%’

SELECT OBJECT_NAME(id) AS OBJECT ,TEXT,* FROM syscomments WHERE TEXT LIKE @Tablename

So far its good. But I found there are some TSQL written directly in the Reports Datasets(which I think is bad SSRS Development) and I cannot search them using the above two procedures.
Upon researching in the ReportServer database the TSQL that is stored in the datasets are embedded in the CommandText tag of contentcolumn in the Catalog table.

The following is the script that illustrates it.
DECLARE @tablename VARCHAR(100)

SET @tablename = ‘%TableA%’

SELECT PATH
, NAME
, CAST(CAST(content AS VARBINARY(MAX)) AS XML) AS ReportXML
FROM ReportServer.dbo.Catalog
WHERE CAST(content AS VARBINARY(MAX)) LIKE @tablename

You can even update the reports using the ReportXML column which I will discuss about it the future blogs.

DMV Report for Index Fragmentation and Statistics Update

Recently the ETL load times of my Datawarehouse are increasing and so I decided to check the Indexes status on the tables . I found some fragmentation on the indexes and also some Statistics that are not being updated regularly . I searched online and found this blog by Ben Nevarez(SQL Blog).
I decided to check the fragmentation levels of the Index after the ETL load is done daily on a regular basis. So I digged deep into the DMV’s and got this script below

select
OBJECT_NAME(ps.object_id) as TableName
,si.Name
,ps.Avg_Fragmentation_in_percent
,STATS_DATE(ss.object_id,ss.stats_id) as LastUpdatedStatistics
from
sys.dm_db_index_physical_stats(DB_ID(DB_NAME()),null,null,null,null) ps
join
sysindexes si
on
ps.object_id = si.id
and
ps.index_id = si.indid
left outer join
sys.stats ss
on
ss.object_id = ps.object_id
and
ss.name = si.name
where
ps.avg_fragmentation_in_percent > 10
order by
ps.avg_fragmentation_in_percent desc

I published the above query in my Reporting services suite of Operational Reports and check it daily morning. The report shows all the Indexes with more than 10% percent fragmentation along with Last updated Statistics time . If you want to go further.You may want to rebuild all the indexes that are above 20% fragmented in TSQL. If there is a better way of doing it please comment.

@Vivekserou

Procedure to export text files

Recently I have read a lot of Blog posts on how to export files in different formats. But I use a custom procedure which exports text files in Pipe Delimited format using BCP.This procedure can be used in other processes for Output and Error Handling.

Here is the Script for the procedure.You can modify the below procedure using any of the delimiters.

alter procedure Export_To_TextFile
(
	@OutputQuery varchar(5000)
	,@FileLocation varchar(400)
	,@FileName varchar(1000)
	,@ServerName varchar(100)
)

as

Declare @bcpcommand varchar(8000)

set @bcpcommand = 'bcp "' + @OutputQuery + '" queryout ' + @FileLocation + @FileName + ' ' + CHAR(13)
+ '-c -t" | " -T -S ' + @ServerName

Exec master..xp_cmdshell @bcpcommand 

Here is how I call the procedure

ExportToTextFile_Results

Reporting Services Date Format

Many Clients have varied Requirements for Dates. You can do the conversions in Date format using T-SQL from the following link in MSDN.
In reporting services you can convert datetime using the customs visual basic datetime formats using Expressions.
Here are the following links on MSDN for Visual Basic Datetime Formats
Predefined DateTime Formats
Using Format Function

Report Server Names for Instances

If the sql server reporting services is configured on the server called “SQLBOX” then the URL is http://SQLbox/reports.
If there is a named instance like SQLBOX\VIVEK then the reporting server URL is http://SQLBOX/reports_vivek

Problem with SQL Server numeric data : Error converting data type varchar to numeric.

Today I stuck with an interesting problem of converting number data in Varchar fields to numeric datatypes. I have created the following sample data

Create table SQL  script

Create table SQL script

The data in the table contains Decimal and scientific values in varchar format as shown in the figure below.

Table_NumericData

Now when I tried to convert the above values to numeric as below it returns only 3 rows but with the following message

Wrong SQL

Wrong SQL

Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to numeric.

The isnumeric does return 1 for all the 4 values in the table. I modified the query so that it initially converts to
float and then converts to numeric and it works.

Correct SQL

Correct SQL

numeric_ResultTable

Twitter Accounts of BI Experts

Here are the list of Industry BI Experts and their Twitter User accounts

http://spreadsheets.google.com/ccc?key=ppfitTGNfaCuf1l9u4Au4JQ

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value

The Error Occurs when the input is date and the given day values are not in the range of the expected days for a given month(1-31)

For eg.

You cannot specify the date parameter as Feb 31 2009 since february only has 28 days in a non-Leap year and it throws the above error.

Extracting Account Name and Domain from Email using T-SQL

Storing the list of email subscribers is quite often seen in the web based companies. Once the emails are stored they are segmented based on the customer interests and products they have purchased on the website.
In this article I will show the simple step of extracting the Account Name and Domain Name using T-sql.

DECLARE @email NVARCHAR(MAX)
DECLARE @EmailAccount NVARCHAR(100)
DECLARE @EmailDomain NVARCHAR(100)

SET @email = ‘george@yahoo.com’

SET @EmailAccount = Lower(LTrim(Substring(@Email, 1, Charindex(‘@’,@Email, 1) – 1)))
SET @EmailDomain = Lower(RTrim(Substring(@Email, Charindex(‘@’,@Email, 1) + 1, 1000)))

PRINT @EmailAccount
PRINT @EmailDomain

Adding Expressions in SSIS – Connection Strings

Expressions are powerful in Integration Services. With Expressions you can dynamically set the servername (based on the environment) or the name of the excel file(if it is a fixed format). Let me give you an example.

I have a simple Dataflow task(as shown below) that gets the data from a table and exports it in a Tab delimited text file.

The Data Flow Task

The Data Flow Task

I have set up a following variable called ServerName where I can change the value dynamically on the Connection String.

Variable Server Name

Variable Server Name


To configure the ConnectionString to use the Variable name dynamically at RunTime we need to configure Dimensions. This is how its done.Right Click on the Connection String and click properties(or click F4 by highlighting Databaes connection)

Database Connection Properties

Database Connection Properties

In the Properties section click on the Expression for configuring nd also setting the Servername settings as below.

Expressions_Connection_Properties

Expressions_Connection_Properties

Select ServerName

Select ServerName

Configure Value as ServerName

Configure Value as ServerName

Click Ok twice and the expressions are now configured. The best way to check it is to use the Bidshelper. (www.codeplex.com/BidsHelper) In the below diagram as you notice the Pink colour on the edge of the Connection String Icon which represents that the connection is configured via Expression.

Expressions_BidsHelper

The above example should give you a good example on how to configure Connection Strings using Expressions. Just keep commenting .