Last month Glen Berry started a fantastic series called "DMV a day" and posted one DMV everyday.
This post has a compilation of all 30 DMV's.
Thursday, May 6, 2010
Monday, January 18, 2010
Transfer Tables between Schemas
Sometimes we accidentally create table in our own user schema instead of dbo (or others). The following code translates between different schema.
Suppose there is a table called Employee which has been created in ragarwal schema instead of dbo. The following code will transfer it to dbo from ragarwal schema.
ALTER SCHEMA dbo
TRANSFER ragarwal.Employee
More on Alter Schema at the following MSDN Link.
Suppose there is a table called Employee which has been created in ragarwal schema instead of dbo. The following code will transfer it to dbo from ragarwal schema.
ALTER SCHEMA dbo
TRANSFER ragarwal.Employee
More on Alter Schema at the following MSDN Link.
Sunday, January 17, 2010
Record Resultset from Stored Procedure in a Table
There may be times when a Store Procedure outputs a Rowset and you may want to capture the results in a database table.
(The below example is executed on the AdventureWorks database)
Here is how you can do that. Suppose you have a Stored Procedure named LoadEmployee and it accepts the three parameters MaritalStatus, Gender and SalariedFlag and it outputs the resultset from the HumarResources.Employee table. Now, if you are interested in finding all salaried single ladies from the table and store it in EmployeeMaster table, this is how you do..
INSERT INTO dbo.EmployeeMasterAnd the output will be stored in a table.
EXEC dbo.LoadEmployee @MaritalStatus = 'S', @Gender = 'F', @SalariedFlag = 1
Monday, December 21, 2009
ANCESTOR MDX Function
I recently started looking up on MDX Functions and one of the first MDX Function I came across was ANCESTOR. Although, MSDN does provide some very good resource on MDX functions, but it was not so simple for me. Either I am totally new to MDX or I am too lame.
MSDN tries to explain the ANCESTOR MDX function using the Product Hierarchy on the Adventure Works Database but I think Date Hierarchy can be more cool.
So, here we go...
As the name suggests, the Ancestor MDX expression helps you find the Ancestor of any MDX Expression. You can specify the either the no. of levels you want to go back or also an expression specifying a level.
The syntax for Ancestor is as follows:
Ancestor(Member_Expression, Level)
or
Ancestor(Member_Expression, Distance)
Thus, for example, if the Date Hierarchy is as follows:
Year -> Semester -> Quarter -> Month -> Date
The MDX expression results to 04th October 2003
and you want to know the total Internet Sales Amount for the Semester this date belongs to... you will use the following syntax
SELECT {
ANCESTOR(
[Date].[Calendar].[Date].[October 4, 2003], --Member Expression
[Date].[Calendar].[Calendar Semester]) --Level Expression
} ON COLUMNS,
[Measures].[Internet Sales Amount] ON ROWS
FROM [Adventure Works]
Please note as the Member Expression can return sets, this is used with { }.
If you want to know the Internet Sales Amount for the Month, Quarter and Year based on the given date you will use the following syntax
SELECT {
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], [Date].[Calendar].[Month]),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], [Date].[Calendar].[Calendar Quarter]),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], [Date].[Calendar].[Calendar Year])
} ON COLUMNS,
[Measures].[Internet Sales Amount] ON ROWS
FROM [Adventure Works]
The syntax produces the following output

You can use the following syntax to use the Distance (Numeric Expression) to get the same output.
SELECT {
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], 1),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], 2),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], 4)
} ON COLUMNS,
[Measures].[Internet Sales Amount] ON ROWS
FROM [Adventure Works]
Remeber from Date, Month is 1 Level up, Quarter is 2 Levels up and Year is 4 Levels up..
Please note the Ancestor Function always returns a single value..
Interestingly, there is also an MDX Function ANCESTORS but I could not find the difference between two. Even the Ancestors return a single value..
The MSDN article on ANCESTOR can be access here...
MSDN tries to explain the ANCESTOR MDX function using the Product Hierarchy on the Adventure Works Database but I think Date Hierarchy can be more cool.
So, here we go...
As the name suggests, the Ancestor MDX expression helps you find the Ancestor of any MDX Expression. You can specify the either the no. of levels you want to go back or also an expression specifying a level.
The syntax for Ancestor is as follows:
Ancestor(Member_Expression, Level)
or
Ancestor(Member_Expression, Distance)
Thus, for example, if the Date Hierarchy is as follows:
Year -> Semester -> Quarter -> Month -> Date
The MDX expression results to 04th October 2003
and you want to know the total Internet Sales Amount for the Semester this date belongs to... you will use the following syntax
SELECT {
ANCESTOR(
[Date].[Calendar].[Date].[October 4, 2003], --Member Expression
[Date].[Calendar].[Calendar Semester]) --Level Expression
} ON COLUMNS,
[Measures].[Internet Sales Amount] ON ROWS
FROM [Adventure Works]
Please note as the Member Expression can return sets, this is used with { }.
If you want to know the Internet Sales Amount for the Month, Quarter and Year based on the given date you will use the following syntax
SELECT {
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], [Date].[Calendar].[Month]),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], [Date].[Calendar].[Calendar Quarter]),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], [Date].[Calendar].[Calendar Year])
} ON COLUMNS,
[Measures].[Internet Sales Amount] ON ROWS
FROM [Adventure Works]
The syntax produces the following output

You can use the following syntax to use the Distance (Numeric Expression) to get the same output.
SELECT {
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], 1),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], 2),
ANCESTOR([Date].[Calendar].[Date].[October 4, 2003], 4)
} ON COLUMNS,
[Measures].[Internet Sales Amount] ON ROWS
FROM [Adventure Works]
Remeber from Date, Month is 1 Level up, Quarter is 2 Levels up and Year is 4 Levels up..
Please note the Ancestor Function always returns a single value..
Interestingly, there is also an MDX Function ANCESTORS but I could not find the difference between two. Even the Ancestors return a single value..
The MSDN article on ANCESTOR can be access here...
Monday, November 23, 2009
Sharepoint in Plain English
The very simple video by CommonCraft explains the concept of Microsoft Sharepoint Server in "plain english".
SharePoint in Plain English
SharePoint in Plain English
Thursday, November 5, 2009
Special Characters in a Table Name
The last day we accidentally changed a table name from 'dbo.TableName' to 'db.TableName'.
Afterwards we were not able to access the table at all, although we could see the table on querying sys.objects or INFORMATION_SCHEMA.TABLES.
The first impression was that the table schema/ownership got changed from dbo to db. However, the table was still existing in the same dbo schema but the name of the table became 'db.TableName' from 'TableName'.
Thus to rename the table I used the following command.
EXEC sp_Rename 'dbo.[db.TableName]', 'TableName'
Please note the use of [] brackets. It is used to embed special characters in the SQL Syntax.
Also, in the second parameter we do not specify the schema/username.
Afterwards we were not able to access the table at all, although we could see the table on querying sys.objects or INFORMATION_SCHEMA.TABLES.
The first impression was that the table schema/ownership got changed from dbo to db. However, the table was still existing in the same dbo schema but the name of the table became 'db.TableName' from 'TableName'.
Thus to rename the table I used the following command.
EXEC sp_Rename 'dbo.[db.TableName]', 'TableName'
Please note the use of [] brackets. It is used to embed special characters in the SQL Syntax.
Also, in the second parameter we do not specify the schema/username.
Unable to remove Partition Filegroup
Today while removing a Filegroup we came across the famous error message.
The filegroup FileGroupNameXXX cannot be removed because it is not empty.
Normally, on getting this error message the first thing I do is try to shrink the Filegroup
DBCC SHRINKFILE (FileGroupNameXXX, 0)
This will clear all unused space from it.
However SQL Server informed me that the File does not exist. On further checking I noticed that the Filegroup does not have an associated File or Partition Scheme associated with it. Then why is it not empty and what is associated with it?
At this point, SQL Server DMV (Dynamic Management Views) came to my rescue and the following command showed there is a table along with an index still associated with the Filegroup.
SELECT ds.name, i.name, o.name
FROM sys.data_spaces ds
inner join sys.indexes i on i.data_space_id = ds.data_space_id
inner join sys.objects o on i.object_id = o.object_id
where ds.name = FileGroupNameXXX
This gave me the name of the Objects associated. So, I first dropped the table and ran the following command to remove the FileGroup.
ALTER DATABASE TestDB REMOVE FILEGROUP FileGroupNameXXX
Hope this helps...
The filegroup FileGroupNameXXX cannot be removed because it is not empty.
Normally, on getting this error message the first thing I do is try to shrink the Filegroup
DBCC SHRINKFILE (FileGroupNameXXX, 0)
This will clear all unused space from it.
However SQL Server informed me that the File does not exist. On further checking I noticed that the Filegroup does not have an associated File or Partition Scheme associated with it. Then why is it not empty and what is associated with it?
At this point, SQL Server DMV (Dynamic Management Views) came to my rescue and the following command showed there is a table along with an index still associated with the Filegroup.
SELECT ds.name, i.name, o.name
FROM sys.data_spaces ds
inner join sys.indexes i on i.data_space_id = ds.data_space_id
inner join sys.objects o on i.object_id = o.object_id
where ds.name = FileGroupNameXXX
This gave me the name of the Objects associated. So, I first dropped the table and ran the following command to remove the FileGroup.
ALTER DATABASE TestDB REMOVE FILEGROUP FileGroupNameXXX
Hope this helps...
Subscribe to:
Posts (Atom)