Archive

Posts Tagged ‘SQL Server’

T-SQL: Using common table expressions (CTE) to generate dates

January 11th, 2010 Kasper de Jonge No comments

This is something I have used many times, and always had to look up on internet. I’m designing a new datawarehouse and have to populate my date dimension. In SQL server 2008 we have common table expressions (CTE),  We can use it to run a sequence from to a date. This blog post will explain how you can generate dates: http://smehrozalam.wordpress.com/2009/06/09/t-sql-using-common-table-expressions-cte-to-generate-sequences/

You can set you regional date / time by using the Set language dutch (where dutch is your regional setting).

Categories: SQL Server Tags: ,

SQL Server 2008 Diagnostic Information Queries

December 4th, 2009 Kasper de Jonge No comments

I was searching for some query to get information from my DWH without me having actually access, i found a set of pretty heavily commented queries that are very useful for detecting and diagnosing many common performance issues with SQL Server 2008 created by Glenn Berry

Check them here:

http://glennberrysqlperformance.spaces.live.com/blog/cns!45041418ECCAA960!2015.entry

Categories: BI Technical, SQL Server Tags:

Microsoft Virtualization: Best Choices for SQL Server

September 2nd, 2009 Kasper de Jonge No comments

While Virtualization of hardware is getting hotter by the day, Ms teched created a blog post about virtualization:

Should I virtualize SQL Server? What benefits will I see?  What do I need to consider when making this decision?  These are some of the questions we are frequently asked as Virtualization continues to be a hot topic.  The short answer is yes – Microsoft Virtualization (Windows Server 2008 R2 Hyper-V + Microsoft System Center) delivers significant benefits including reduced costs through server consolidation and power and space savings, improved server utilization, greater agility for responding to dynamic business needs, rapid server provisioning, reduced management complexity and reduced downtime during failover with Hyper-V Live Migration.

Read the rest at: http://blogs.technet.com/dataplatforminsider/archive/2009/09/02/microsoft-virtualization-best-choices-for-sql-server.aspx

Categories: SQL Server Tags:

How to trace the execution of a stored procedure?

August 27th, 2009 Kasper de Jonge No comments

I was at a client and they had some performance issues calling stored procedures from code. They were unable to find the sql statementfor responsible the performance degrade.

You can use the SQL profiler to show you the TSql statement within your stored procedure and their trace properties, to do this go to trace properties, check Show all events and you will get the possibility to check 2 events that will look inside the SP’s and show you alle the Tsql statements.

trace

Categories: SQL Server Tags: ,

Remove duplicates from table using SQL Rank() function

August 27th, 2009 Kasper de Jonge No comments

Having to de-duplicate rows from source tables is a very common task in the data integration world, we were trying to do all different of Tsql to filter out the duplicate values. We needed to pick the row based upon a certain criteria. On the internet i found the TSQL function Rank()

It works like this:

No matter where the source data is stored, as a part of the ETL workflow, we will stage source data in a SQL Server 2005 table.

This is the staging table:

create table SourceWithDuplicates
(SurrogateKey int identity(1,1),
Code varchar(12),
Description varchar(20),
OtherAttribute1 int,
OtherAttribute2 Varchar(10),
TieBreakerColumn Varchar(12))

As a general rule I always add a surrogate key column to my staging tables (that makes some DBAs happy); an in this case it would actually help in the de-duplication process.

Some sample data:

Insert into SourceWithDuplicates values(‘Abc’, ‘Row Description 1′, 1, ‘Other1′,’1′)
Insert into SourceWithDuplicates values(‘Abc’, ‘Row Description 1′, 1, ‘Other1′, ‘1′)
Insert into SourceWithDuplicates values(‘Abc’, ‘Row Description 1′, 1, ‘Other2′,’2′)\
Insert into SourceWithDuplicates values(‘Def’, ‘Row Description 2′, 1, ‘Other4′,’a')
Insert into SourceWithDuplicates values(‘Def’, ‘Row Description 2′, 1, ‘Other5′,’a')
Insert into SourceWithDuplicates values(‘Ghi’, ‘Row Description 3′, 1, ‘Other5′,’a')
Insert into SourceWithDuplicates values(‘Jkl’, ‘Row Description 4′, 1, ‘Other5′,’a')

The requirements:

Code and description values need to be unique in the target table; in case they are duplicates, choose the row with the highest value in the tie breaker column. If the value in the tiebreaker column is also duplicated; then choose any of the rows.

So the data looks like:

SurrogateKey Code Description OtherAttribute1 OtherAttribute2 TieBreaker ———— ———— ——————– ————— ————— ———-
1 Abc Row Description 1 1 Other1 1
2 Abc Row Description 1 1 Other1 1
3 Abc Row Description 1 1 Other2 2
4 Def Row Description 2 1 Other4 a
5 Def Row Description 2 1 Other5 a
6 Ghi Row Description 3 1 Other5 a
7 Jkl Row Description 4 1 Other5 a

Following the requirements, only 4 rows kept should be chosen; those with surrogate keys:2 3, 4 or 5, 6 and 7
This is the query using the Rank() function that resolve our issue:

Select *,
rank() over (Partition by Code, description order by TieBreakerColumn desc, SurrogateKey) MyRank
from SourceWithDuplicates

and this is its output:

Surrogatekey Code Description MyRank
———— ———— ——————– ——————–
3 Abc Row Description 1 1
1 Abc Row Description 1 2
2 Abc Row Description 1 3
4 Def Row Description 2 1
5 Def Row Description 2 2
6 Ghi Row Description 3 1
7 Jkl Row Description 4 1

Notice that if we filter the result from the previous query using WHERE MyRank=1 we will get the rows to be kept.

Very nice ! I  found this at:

http://rafael-salas.blogspot.com/2007/04/remove-duplicates-using-t-sql-rank.html

Categories: SQL Server Tags: ,

Schedule SQL Query output to file, the simple way

August 19th, 2009 Kasper de Jonge 2 comments

Today two colleagues wanted to schedule the output of a SQL command to a text file so they could check some values on a daily base. After some clicking in SQL Server i found a very simple solution,

I created a SQL server Job Step which executed the SQL command

crop

Then went to the Advanced tab and selected an output file to where the data can be written (even appended):

step

Very simple way to log query output to a file at a regular interval.

Categories: SQL Server Tags:

SQL Find Last Day of Any Month

July 10th, 2009 Kasper de Jonge No comments

I needed to determine tha last day of previous month in a view, i found this on internet and works great:

Following script demonstrates the script to find last day of previous, current and next month.
----Last Day of Previous Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
LastDay_PreviousMonth
----Last Day of Current Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
LastDay_CurrentMonth
----Last Day of Next Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))
LastDay_NextMonth

found this at http://blog.sqlauthority.com/2007/08/18/sql-server-find-last-day-of-any-month-current-previous-next/

Categories: SQL Server Tags:

How to Clear SQL Server Cache When Performing Benchmark Tests

May 28th, 2009 Kasper de Jonge No comments

I was testing the perfomance of a query from different perspectives and wanted to make sure SQL Server didn’t cache Query results, to achieve this:

run DBCC DROPCLEANBUFFERS, which clears all data from the cache. Then run DBCC FREEPROCCACHE, which clears the stored procedure cache.
 

found at:

http://www.devx.com/tips/Tip/14401

Categories: SQL Server Tags:

Microsoft announced CEP, real time BI on the horizon ?

May 14th, 2009 Kasper de Jonge No comments

Microsoft announced earlier this week that a CEP/stream processing product will be included in SQL 2008 R2Complex Event Processing, or CEP, is primarily an event processing concept that deals with the task of processing multiple events with the goal of identifying the meaningful events within the event cloud. CEP employs techniques such as detection of complex patterns of many events, event correlation and abstraction, event hierarchies, and relationships between events such as causality, membership, and timing, and event-driven processes.

Microsoft called out four reasons to me why CEP might be needed in addition to ordinary database processing. Two are the standard reasons for data reduction:

1. Without CEP, you can’t bang the data into the database fast enough.
2. You don’t want to keep most of the data past a short time window anyway.
The other two are also fairly standard reasons for using CEP:
3. Standard SQL isn’t all that great for time series anyway.
4. CEP use cases often call for incremental processing and/or parameterization of queries, something CEP engines are commonly better designed for than are DBMS.

However, Microsoft seems to be taking a somewhat different approach to time-based SQL extensions than some other vendors. To quote email Microsoft sent today:

Microsoft Research (MSR) introduced the temporal extensions to relational algebra based upon a notion of application time that is independent of system time. It matters when the event originated instead of when they arrived at the processing system. Further it treats each event as being associated with an interval of time as opposed to a point in time. This helps in modeling certain real life phenomenon naturally. [StreamBase et al.] also reason about multiple streams. Both the approaches are extensions to relational algebra. The MSR approach took the algebra as the starting point while StreamBase took an existing language over the algebra – SQL as the starting point. The MSR approach consequently avoids having to rework other elements of the SQL surface. The primary language extensions through which this algebra will be exposed initially is LINQ.

What are the implications of this? Can we use the CEP algorithm to monitor real time data from the cloud and extract only the necessary data to our datawarehouse ? or am i going to far with this ?

Found at: http://www.dbms2.com/2009/05/13/microsoft-announced-cep-this-week-too/

Categories: Cloud, SQL Server Tags: , , ,

New SQL release: SQL Server 2008 R2 + CTP

May 12th, 2009 Kasper de Jonge No comments

Microsoft announced a new SQL Server version, SQL Server 2008 R2 with realy great upgrades for BI. And you can register for the CTP.

Some very interesting new features are:

  • Self Service Analysis with “Project Gemini”

    Build Robust Analytical Applications

    • Combines native Excel 2010 functionality with an in-memory, column oriented processing engine to allow users to interactively explore and perform calculations on millions of rows of data at lightening speeds

    • Streamlines the process of integrating data from multiple sources – including corporate databases, spreadsheets and external sources

    • Access PivotTables, slicers, and other familiar analysis features in Excel to create reports and perform advanced analysis

  • More Powerful Ad-hoc Reporting with Enhanced Data Models

    Report with Ease

    • Decrease time and costs developing reports

    • Enable timely access to information to help make better decisions by empowering end users to easily design queries, reports and charts through a highly intuitive, drag and drop interface

    • Powerful and intuitive authoring and ad hoc reporting capabilities with enhanced data models

  • “Grab & Go” Reporting

    Collaborate with Confidence

    • Central, secure location for IT administrators and users to publish content objects that can be broadly reused and easily customized to meet the users’ needs

    • Ensure consistency by creating and maintaining departmental content that can be accessed by the rest of the organization for building comprehensive business reports

    • Accelerate report creation by allowing end users to reuse existing components (queries, tables, charts, maps, gauges, logos) as building blocks for creating new reports

It even includes the new MDM with Master Data Services (MDS).

Read more and register for the CTP here :  http://www.microsoft.com/sqlserver/2008/en/us/R2.aspx

Categories: SQL Server Tags: ,