Archive for the ‘SQL Server 2008’ Category

Post

10th SEP – PASS Meeting- What’s new in SQL Server 2008 for BI

In SQL Server 2008 on September 26, 2009 by ashwaniroy

SSAS 2008 has improved Dimension and Aggregation Designer, new Attribute Relationship Designer, Optimize performance with block computations mode and dynamic management views for enhanced resource monitoring.SSRS 2008 reports has the unique data format of Tablix which allows writing reports with combined advantage of Table and Matrix formats. It can integrate with Microsoft Office SharePoint Server 2007 for central delivery and management of business insight. It also enables users to quickly gain insight into complex sets of data by displaying data graphically with enhanced visualization capabilities. Performance has improved drastically for situations where you are generating large reports.

SSIS Pipeline is optimized to enable more parallel loading of data. You can write script components in C# now. It comes with improved scalability with thread pooling and enhanced lookup transformations. It also performs more functional and scalable data transfers with the improved SQL Server Import and Export Wizard.

Other engine features that you need to be aware of are, improvements to partitioning, change data capture to enable easy extraction of changed data from a production system, backup and table compression which enables better performance and less storage space utilisation. It also comes with optimized Star-Join, improved lock escalation handling, Merge statements and other advanced T SQL enhancements.

 

The slides and demos are available here for download.

Post

MDX – SOLVE order “Watch your calculations”

In SQL Server 2008 on August 27, 2009 by ashwaniroy

Have a look at this query

WITH

MEMBER [Measures].[REFUNDS] AS [Measures].[Internet Sales Amount] – [Measures].[Internet Sales Amount] * .4

MEMBER [Measures].[PROFIT] AS [Measures].[Internet Sales Amount] – [Measures].[REFUNDS]

MEMBER [Product].[Product].[Top10] AS

sum(

TOPCOUNT([Product].[Product].[Product].MEMBERS

, 10

, [Measures].[Internet Sales Amount]

)

,

([Measures].CurrentMember)

)

MEMBER [Product].[Product].[Top10PercOfTotal] AS [Product].[Product].[Top10]/[Product].[Product].[All Products]

SELECT

{

[Measures].[Internet Sales Amount],

[Measures].[REFUNDS],

[Measures].[PROFIT]

} ON COLUMNS,

{

[Product].[Product].[Top10]

,[Product].[Product].[Top10PercOfTotal]

,[Product].[Product].[All Products]

}

ON ROWS

FROM [Adventure Works]

This yeilds same result as yours ie. not a flat cell by cell division

 

Internet Sales Amount

REFUNDS

PROFIT

Top10

$10,355,525.92

$6,213,315.55

$4,142,210.37

Top10PercOfTotal

0.35272454

0.211634724

0.141089816

All Products

$29,358,677.22

$17,615,206.33

$11,743,470.89

 

I don’t expect it to produce this result . If the computations was going as planned the output should be 0.35272454  (35 %) for each.

I have altered the SOLVE ORDER (this is the property which determines the order of cell computation)

Here is modified query

WITH
MEMBER [Measures].[REFUNDS] AS [Measures].[Internet Sales Amount] - [Measures].[Internet Sales Amount] * .4,SOLVE_ORDER = 1
MEMBER [Measures].[PROFIT] AS   [Measures].[Internet Sales Amount] - [Measures].[REFUNDS],SOLVE_ORDER = 2
 
MEMBER [Product].[Product].[Top10] AS
 sum(
  TOPCOUNT([Product].[Product].[Product].MEMBERS
  , 10
  , [Measures].[Internet Sales Amount]
  )
  ,
  ([Measures].CurrentMember)
  )
,SOLVE_ORDER = 3
 
MEMBER [Product].[Product].[Top10PercOfTotal] AS 
[Product].[Product].[Top10]/[Product].[Product].[All Products]
, FORMAT_STRING = "Percent"
 , SOLVE_ORDER = 4
 
 
 
SELECT 
 {
  [Measures].[Internet Sales Amount],
  [Measures].[REFUNDS],
  [Measures].[PROFIT]
  } ON COLUMNS,
 {
[Product].[Product].[Top10]
,[Product].[Product].[Top10PercOfTotal]
,[Product].[Product].[All Products]
 
  } 
  ON ROWS
FROM [Adventure Works]

·

 

Internet Sales Amount

REFUNDS

PROFIT

Top10

$10,355,525.92

6213315.552

4142210.368

Top10PercOfTotal

35.27%

35.27%

35.27%

All Products

$29,358,677.22

$17,615,206.33

$11,743,470.89

 

There you go. SOLVE_ORDER determines the series of how cell computation  will be formed. One with smallest SOLVE_ORDER will be evaluated first.

Hope this explains SOLVE_ORDER and how it works.

Post

Thursday, 10th September – SQL Server User Group Meeting

In SQL Server 2008 on August 25, 2009 by ashwaniroy

The evening’s agenda includes the following presentations (see Events section for more details):

What’s new in SQL Server 2008 for BI

Presented by Ashwani Roy, MCTS MCITP MCAD MCP

We’re also pleased to let you know that SQL Server MVP, Simon Sabin will be presenting. Details about this presentation will be made available shortly.

 

Location:
Bank Of America 
5 Canada Square
London E14 5AQ
United Kingdom

Register @ http://www.sqlpass.org.uk/

Post

Favorite SQL Server Interview Questions

In Interview Questions,Microsoft SQL Server,SQL Server 2008 on August 18, 2009 by sqlapostle

Every interviewer has some favorite questions that he likes to ask in almost each and every interview. Here are some of mine, and a bit of advice for those who think these are easy, I would hold off on the answers till next week for you to try and take a guess at the answers (Without executing the commands on SQL Server, of course)

1. The Setup :

Employee

  • EmployeeId int
  • EmployeeName nvarchar(50)
  • Sex ( constraint M / F) char(1)
  • DepartmentId int

 

The requirement is to produce the following output in an optimized way

DepartmentId                          CountFemale                      CountMale                  EmployeeCount

 

2. The Setup :

USE PlayGround
GO
BEGIN TRAN
    Create Table Employee
    (
        EmployeeId int
        ,EmployeeName nvarchar(100)
    )
    exec xp_cmdshell ‘bcp Playground.dbo.Employee in c:\Employee.txt -E -c’
    TRUNCATE TABLE Employee
ROLLBACK TRAN
GO
SELECT * FROM Employee
GO

What is the expected result of the above script ?

3. A variation on the above :

USE PlayGround
GO
    Create Table Employee
    (
        EmployeeId int
        ,EmployeeName nvarchar(100)
    )
    Insert Into Employee Select 1, ‘TestEmployee’
GO

BEGIN TRAN
    TRUNCATE TABLE Employee
ROLLBACK TRAN
GO
SELECT * FROM Employee

4. Is it possible to insert into two tables having same structure using a single insert statement ?

Post

OLAP PivotTable Extensions

In SQL Server 2008 on July 27, 2009 by ashwaniroy

Excel 2007 provides many APIs which are quite powerful from analytics point of view.Not all of them are exposed via the UI though ( for some reason … ). SQL Server MVP Greg Galloway has developed this very cool tool which is EXCEL 2007 add-in and lets you do very cool stuff.

You know that calculated members are evaluated on the fly.But the only way you can have this calculated member available from browsing is by doing this physically in the cube. There is a way that you can do it in the excel itself .These are called Private Calculated Members

Simple ratios or differences and stuff that are very specific to a (or small group) of cube users can be put in here , rather than cluttering the cube.

Limitation: If you run "OLAP Tools… Convert to Formulas" on a PivotTable with private calculated members, the private calculated members will show N/A. There is no known workaround at this point other than having your OLAP administrator define these calculated members in the cube itself.

There is much more to this too and the download and more information is available here http://www.codeplex.com/OlapPivotTableExtend.

Post

SQL Server 2008 – Merge , Grouping Sets , table Valued Parameters and some other stuff !!

In SQL Server 2008 on July 23, 2009 by ashwaniroy

Some time back I took a small session for a group of SQL Server users about some cool stuff in SQL Server 2008 from the T SQL point of view. Some of the students wanted to have access to the scripts , so here they are

MERGE STATEMENT and OUTPUT CLAUSE

drop table Source
drop table Destination

go

CREATE TABLE Source (id int,val varchar(56));

CREATE TABLE Destination (id int,val varchar(56));

GO

INSERT Source VALUES (1,’A');
INSERT Source VALUES (2,’B');– This will be inserted

INSERT Destination VALUES (1,’q');–this will be updated to A
INSERT Destination VALUES (6,’w');
INSERT Destination VALUES (8,’r');

GO

MERGE Destination D — target table

        USING Source S — source table

        ON S.ID = D.ID

        WHEN MATCHED  THEN

                UPDATE set D.val = S.val

          WHEN NOT MATCHED THEN

                — insert a row if the stock is newly acquired

                INSERT VALUES (S.ID, S.VAL)

        — output details of INSERT/UPDATE/DELETE operations

        — made on the target table

        OUTPUT $action, inserted.*, deleted.*; —this is not required in Merge statement I have kept it here just to demostrate that from SQL 2005 onwards
                                                       — OUTOUT clause can be used along with INSERTED , DELELED to get the what has been done as a result to DML action
                                                       — Might help in auditing the DML actions
/*output of the OUTPUT CLAUSE IS

$action    id    val        id        val— I can see what is Inserted and what is Deleted
INSERT    2    B        NULL    NULL
UPDATE    1    A        1        q

*/                                                      

SELECT * FROM Destination;–See the result

GO

 

GROUPING SETS

GROUPING SETS are a new feature of SQL Server 2008. Using them will allow multiple groupings to be returned in one record set.

We will grouping on City and StateProvice in the same query

use AdventureWorksLT
go
—old way —————-
SELECT NULL, City, COUNT(StateProvince) AS Nbr
FROM SalesLT.Address
GROUP BY  City
UNION ALL
SELECT StateProvince, NULL, COUNT(StateProvince) AS Nbr
FROM SalesLT.Address
GROUP BY StateProvince

–useing GROUPING SETS in SQL 2008
SELECT StateProvince, City, COUNT(StateProvince) AS Nbr
FROM SalesLT.Address
GROUP BY
GROUPING SETS
(
  (City),
(StateProvince)
–()
  );

TABLE VALUED PARAMTERS

In this example I will pass a table valued parameter in the stored procedure

–USING Table valued input Parameters
CREATE TYPE myTableType AS TABLE (id INT, name NVARCHAR(100),qty INT);

ALTER PROCEDURE myProc (@tvp myTableType READONLY) AS

declare @table TABLE (id int)

    UPDATE Inventory SET
    qty += s.qty
    FROM Inventory AS i INNER JOIN     @tvp AS tvp
    ON i.id = tvp.id
GO

 

NEW DATE TIME DATA TYPES

– IN SQL 2008 INSTANCE
declare @datetime datetime,@DATETIME2 DATETIME2
select @datetime = ’1500-03-04 15:43:26.857′–this will give out of range exception asyou cannot have anything before 1900
select @datetime2 = ’1500-03-04 15:43:26.857′– there you can do it
SELECT @DATETIME2

DECLARE @DATE DATE , @TIME TIME
SELECT @DATE =’1900-01-01′,@TIME = ’15:43:26.857′
SELECT @DATE ,@TIME

DECLARE @DATETIMEOFFSET datetimeoffset
SELECT @DATETIMEOFFSET = ’2005-09-08 12:20:19.345 -08:00′    — stored datetime with OFFSET Lets say you want datetime datetime captured in LONDON server transfrom to US and lets assume the offset is 8 hrs
SELECT @DATETIMEOFFSET

————————————————————————————————————————–

Thats all for now!! I though it will be easier to get the scripts from here rather than download from my sky drive. It is not a very extensive list , it just covers the 1 hours session which I took for beginner sql users.

Post

A quick Look at CDC –(Change data Capture)

In SQL Server 2008 on July 23, 2009 by ashwaniroy

 

What is CDC :-

Change data capture (CDC) is a set of software design pattern used to determine (and track) the data that has changed so that action can be taken using the changed data. Also, Change data capture (CDC) is an approach to data integration that is based on the identification, capture and delivery of the changes made to enterprise data sources.

It is a very common requirement for a data warehouse load package is to determine what has changed in the source systems and load this data into the warehouse.

All I wanted to give was a script that you can use along with adventure works Database to see what are basic stuff in CDC and how it works. off course you can use it in your Data warehouse SSIS packages.

—————————————————————

USE AdventureWorks
GO
–Enable Change Data Capture on the Database–
EXEC sp_cdc_enable_db
GO

–Enable Change Tracking on a table–
EXEC sp_cdc_enable_table
    @source_schema = ‘HumanResources’ ,@source_name = ‘Employee’,@supports_net_changes = 1,@role_name = null
go
–Simulate a Data Change —
UPDATE HumanResources.Employee
SET   ManagerID = 10 WHERE Title = ‘Production Technician – WC60′
go

select * from HumanResources.Employee where Title = ‘Production Technician – WC60′

–Get the Data —
select sys.fn_cdc_get_min_lsn(‘HumanResources_Employee’),sys.fn_cdc_get_max_lsn() 
go

select *  from cdc.fn_cdc_get_all_changes_HumanResources_Employee
(0×00000036000017400078,0x0000003600001EC1006C,’all’)

declare @minLSN varbinary(max),@maxLsn varbinary(max)

select @minLSN = sys.fn_cdc_get_min_lsn(‘HumanResources_Employee’)
,@maxlsn=sys.fn_cdc_get_max_lsn() 

select *  from cdc.fn_cdc_get_all_changes_HumanResources_Employee
(@minLSN ,@maxLsn ,’all’)

–Disable CDC as I dont want this running on my Laptop–
EXEC sp_cdc_disable_db
GO
/*
    WHEN 1 THEN ‘delete’
    WHEN 2 THEN ‘insert’
    WHEN 4 THEN ‘update’

*/

–exec sp_configure

—————————————————————————————————–

Post

Dynamic Management Views for SSAS

In SQL Server 2008 on July 15, 2009 by ashwaniroy

With SQL Server 2005 onwards we are provided with a set of DMVs. These DMVs allow us to monitor facts like which index is being used and which are not , where are IO bottlenecks , what is cache hit ratio and other stuff. But what about analysis service.

What if I want to know which of my hierarchies are being used , which are the aggregations that are not being used etc etc.

Well from SQL 2008 onwards you can have this capability.

AS 2008 has

–>  4 DBSCHEMA (Database Schema DMVs)

1. $SYSTEM.DBSCHEMA_CATALOGS

2. $SYSTEM.DBSCHEMA_COLUMNS

3. $SYSTEM.DBSCHEMA_PROVIDER_TYPES

4. $SYSTEM.DBSCHEMA_TABLES

–> 10 DMSCHEMA DMVs

1. $SYSTEM.DMSCHEMA_MINING_COLUMNS

2. $SYSTEM.DMSCHEMA_MINING_FUNCTIONS

3. $SYSTEM.DMSCHEMA_MINING_MODEL_CONTENT

4. $SYSTEM.DMSCHEMA_MINING_MODEL_CONTENT_PMML

5. $SYSTEM.DMSCHEMA_MINING_MODEL_XML

6. $SYSTEM.DMSCHEMA_MINING_MODELS

7. $SYSTEM.DMSCHEMA_MINING_SERVICE_PARAMETERS

8. $SYSTEM.DMSCHEMA_MINING_SERVICES

9. $SYSTEM.DMSCHEMA_MINING_STRUCTURE_COLUMNS

10. $SYSTEM.DMSCHEMA_MINING_STRUCTURES

These DMVs describe data mining models in the Analysis Services database.

–> There are 13 DMVs which describe the Meta Data of Analysis service data base (cube,partitions,hierarchies etc)

1. $SYSTEM.MDSCHEMA_CUBES

2. $SYSTEM.MDSCHEMA_DIMENSIONS

3. $SYSTEM.MDSCHEMA_FUNCTIONS

4. $SYSTEM.MDSCHEMA_HIERARCHIES

5. $SYSTEM.MDSCHEMA_INPUT_DATASOURCES

6. $SYSTEM.MDSCHEMA_KPIS

7. $SYSTEM.MDSCHEMA_LEVELS

8. $SYSTEM.MDSCHEMA_MEASUREGROUP_DIMENSIONS

9. $SYSTEM.MDSCHEMA_MEASUREGROUPS

10. $SYSTEM.MDSCHEMA_MEASURES

11. $SYSTEM.MDSCHEMA_MEMBERS

12. $SYSTEM.MDSCHEMA_PROPERTIES

13. $SYSTEM.MDSCHEMA_SETS

These DMVs expose a Whole New gold mine for Analysis and monitoring of SSAS. Have a look at some of the DMX queries below which use these DMVs to expose some very useful information.

1. Open SQL Server Management Studio

2. Open a DMX query Window and connect to Adventure Works Cube .(if you dont have adventure works cube you can download it from codeplex.com)

3. paste this query

SELECT *
FROM $SYSTEM.MDSCHEMA_MEASURES
WHERE CUBE_NAME = ‘Adventure Works’
   AND MEASUREGROUP_NAME = ‘Internet Sales’
ORDER BY [MEASUREGROUP_NAME]

You will see that you have information about all the measures in the Measure Group

here is sample output (Only selected few column due to space constraint)

CATALOG_NAME SCHEMA_NAME CUBE_NAME MEASURE_NAME MEASURE_UNIQUE_NAME DEFAULT_FORMAT_STRING
Adventure Works DW Adventure Works Internet Order Quantity [Measures].[Internet Order Quantity] #,#
Adventure Works DW Adventure Works Internet Extended Amount [Measures].[Internet Extended Amount] Currency
Adventure Works DW Adventure Works Internet Tax Amount [Measures].[Internet Tax Amount] Currency
Adventure Works DW Adventure Works Internet Freight Cost [Measures].[Internet Freight Cost] Currency
Adventure Works DW Adventure Works Internet Unit Price [Measures].[Internet Unit Price] Currency
Adventure Works DW Adventure Works Internet Total Product Cost [Measures].[Internet Total Product Cost] Currency
Adventure Works DW Adventure Works Internet Standard Product Cost [Measures].[Internet Standard Product Cost] Currency

Now that we know how useful can this DMV be here are some scripts that can help you play around with

select * from $system.dbschema_tables
select * from $system.dbschema_columns
select * from $system.dbschema_catalogs

select * from $system.mdschema_cubes
select * from $system.mdschema_hierarchies

select * from [Adventure Works].[$Product] –database dimension

select * from $system.discover_commands
select * from $system.discover_connections
select * from $system.discover_memoryusage
select * from $system.discover_object_memory_usage
select * from $system.discover_object_activity where object_reads > 0
select * from $system.discover_partition_stat

SQL Server MVP Darren Gosbell has blogged about it and I suggest you look at his blog http://geekswithblogs.net/darrengosbell/Default.aspx 

(even otherwise it is a very good blog)

Post

Ignore_Dup_key Index Option

In Microsoft SQL Server,SQL Server 2005,SQL Server 2008 on June 17, 2009 by sqlapostle

The Ignore_Dup_Key is an index option that lets you specify the error response to a duplicate key value insert into a column having a unique clustered or unique non clustered index.

If you have Ignore_Dup_Key option set to ON for a unique index, when presented with a duplicate key insert into the column, it will issue a warning message only and rows violating the constraint will fail.

Set it to OFF and you will get an error message along with the transaction being rolled back and aborted.

Lets see some code which explains this , coz 10 lines of code are worth 100 lines of documentation :) .

   1:  Use PlayGround
   2:  -- Use your personal test database name here , mine's called the Playground 
   3:  GO
   4:  -- Lets create the Test Table 
   5:  CREATE Table MyDupKeyTest
   6:  (
   7:      KeyCol int
   8:      ,AnotherCol varchar(20)
   9:  )
  10:  GO
  11:  -- Create a unique index with the Ignore_dup_key set to ON
  12:  CREATE UNIQUE INDEX ixuc_MyDupKeyTest_KeyCol ON MyDupKeyTest(KeyCol) WITH (IGNORE_DUP_KEY = ON)
  13:  GO
  14:  -- Insert our test row
  15:  INSERT INTO MyDupKeyTest SELECT '1','SqlApostle'
  16:      17:  --Now lets try inserting another row with the duplicate values
  18:  BEGIN TRY
  19:      INSERT INTO MyDupKeyTest SELECT '1','SqlApostle'
  20:  END TRY
  21:  BEGIN CATCH
  22:      PRINT 'IN CATCH BLOCK'
  23:  END CATCH
  24:  -- Didn't reach the Catch block , there is only an warning issued
  25:      26:  GO
  27:  -- Now lets turn the option to OFF 
  28:  ALTER INDEX ixuc_MyDupKeyTest_KeyCol ON MyDupKeyTest REBUILD WITH (IGNORE_DUP_KEY = OFF )
  29:  GO
  30:  BEGIN TRY
  31:      INSERT INTO MyDupKeyTest SELECT '1','SqlApostle'
  32:  END TRY
  33:  BEGIN CATCH
  34:      PRINT 'IN CATCH BLOCK'
  35:  END CATCH
  36:  -- Into the catch block , error has occured
  37:  GO
 
Further on this in later posts

Post

Download Available (BI EVENING 10 JUNE Reading (UK):- Attribute Relationships, Aggregations and using MDX Studio to its best)

In MDX,SQL Server 2008 on June 13, 2009 by ashwaniroy

I was a speaker this BI evening at Microsoft Reading on Attribute Relationships, Aggregations and using MDX Studio to its best.

Setting proper relationships for the attributes of a dimension is essential from a query performance point of view. It is one of the most important things while dimensional modelling.
Aggregations are pre calculated summaries of cube data that help enable Microsoft SQL Server 2005 Analysis Services (SSAS) to provide rapid query responses. Aggregations are like fact table data grouped and aggregated to a higher level in the Hierarchy. Building the correct aggregations can greatly enhance query performance.
MDX Studio can be used to analyze complex MDX expressions, monitor performance characteristics of MDX queries and get insight into how MDX interacts with other UDM features such as attribute relationships. MDX Studio provides unique visualization representation of MDX expression and allows the user to interact with different stages of MDX execution. It is built by Mosha Pasumansky, who is inventor of MDX and one of the Architects of Microsoft Analysis Services.

The Slides and Demo is available for download here.

Follow

Get every new post delivered to your Inbox.