We spend hours and hours staring at our email One of the most popular email client is MS Outlook. One thing I like to do is to add my favorite feeds to my client. It makes posts readily available and easy to share. Plus, it is a good distration to enhance your skills or just a good distraction.
Category Archives: t-sql - Page 3
SQL Skills – Use Outlook as Your RSS Viewer
Disk Space – DBCC UPDATEUSAGE
One important tool in managing disk space is to make sure the system accurately reports space used by tables. DBCC UPDATEUSAGE will update the values returned by sp_spaceused.
The output of sp_spaceused “displays the number of rows, disk space reserved, and disk space used by a table, indexed view, or Service Broker queue in the current database, or displays the disk space reserved and used by the whole database.” One thing to note when reviewing the output is you may see the the reserved size is disportionately larger than the data and index space used. The reserved space should always be greater, but in one case I did see > 20 GB of difference. The space reported by system can get out of whack (technical term) from repeated use of TRUNCATE statements and frequent DDL statements.
Excellent article on DBCC UPDATEUSAGE by Armando Prato.
Google search for DBCC UPDATEUSAGE.
Query Plans and active query information
Quick script to see active processes and to grab the query plan. It leverages some DMVs to get the query text and plan. Only can be used on SQL 2005 and above. It is a great and light wight way to see what is going on with the server and performance tuning.
Disk Space – Get table sizes
Below is a script I use to determine table size. Basically, it walks through the user databases and for each table. The information is converted in GB. It’s a pretty simple use of MS sp_spaceused with sp_msforeachtable. More info on sp_spaceused.
Full script, below. Download script here.
T-SQL Split up an Array
I work with data from a system that stores values as arrays or extents. In 2008, I created a CLR function to expose the C# split command. I tried several iteration with loops in T-SQL, etc. overtime, I have noticed the CLR as being a bit of a memory hog.
I found this possible solution : Split a string in TSQL (without looping).
Need to change it a bit ..
1. Reduce the numbers table to MAX Extents position ( 80). Still populated it with 1-120.. just in case.
2. Needed it to return as an INLINE Scalar Value and not a Table Value Function.
Here is What I Came up with from the original Script:
/*
Adapted from Split a string in TSQL (without looping)
http://blog.magenic.com/blogs/whitneyw/archive/2008/10/08/Split-a-string-in-TSQL-_2800_without-looping_2900_.aspx
(Link no longer valid)
JCD Changes 5/2010
1. Only have need for 80 position Arrays..
So, I limited my Numbers Table to 120
2. Added a @pos INT to evaluate only one position
3. Need it to act as a SCALAR Function
and not a a Table value Funtion
*/
— ORIGINAL COMMENT
— Create and populate an auxiliary
— table of numbers
IF OBJECT_ID(‘dbo.Numbers’) IS NOT NULL
BEGIN
DROP TABLE dbo.Numbers;
END
GO
CREATE TABLE dbo.Numbers
(
number INT NOT NULL
CONSTRAINT PK_Numbers
PRIMARY KEY CLUSTERED
);
SET NOCOUNT ON;
DECLARE
@max AS INT,
@rc AS INT;
SET @max = 120; — you may need to set this much higher
SET @rc = 1;
BEGIN TRAN;
INSERT INTO dbo.Numbers(number) VALUES(1);
WHILE @rc * 2 <= @max BEGIN INSERT INTO dbo.Numbers(number) SELECT number + @rc FROM dbo.Numbers; SET @rc = @rc * 2; END INSERT INTO dbo.Numbers(number) SELECT number + @rc FROM dbo.Numbers WHERE number + @rc <= @max; COMMIT TRAN; GO /* --ORIGINAL COMMENT Next, we create the inline function that will do the heavy lifting for us. I've added an input for separator as hard coding the comma will almost always guarantee some other delimiter will be needed. Turned it into a scalar function */ IF OBJECT_ID('dbo.SplitString') IS NOT NULL DROP FUNCTION dbo.SplitString; GO CREATE FUNCTION dbo.SplitString(@arr AS VARCHAR(MAX), @sep AS CHAR(1),@pos AS INT ) RETURNS VARCHAR(60) AS BEGIN BEGIN -- SET Value DECLARE @result VARCHAR(60) -- Changed to put return one value to a variable SELECT @result = RTRIM(LTRIM(SUBSTRING(@arr, number, CHARINDEX(@sep, @arr + @sep, number) - number))) FROM dbo.Numbers WHERE number <= LEN(@arr) + 1 AND SUBSTRING(@sep + @arr, number, 1) = @sep AND ((number - 1) - LEN(REPLACE(LEFT(@arr, number - 1), @sep, '')) + 1) = @pos; END RETURN RTRIM(@Result) END GO /* --ORIGINAL COMMENT Finally, we call our function with a string of values. If we needed to then utilize this for filtering we could use the CROSS APPLY operator introduced in SQL 2005. */ DECLARE @arr varchar(MAX), @sep char(1), @pos INT; SELECT @arr = '1, 2, Dan, 4, Blah, %, 7, "We the people...", 9,"A string walks into a RBAR and says...", B, C' ,@sep = ',' -- Set the delimiter ,@pos = 6 -- Set the position to return -- Return Single Value SELECT dbo.SplitString(@arr, @sep, @pos) as SplitValue; SELECT dbo.SplitString(@arr, @sep, 8) as SplitValue; SELECT dbo.SplitString(@arr, @sep, 10) as SplitValue; GO
SQLServerCentral – Report Server Diagnostic Reports
FROM http://www.sqlservercentral.com/articles/SSRS/69257/
Michael Davis has published this article and RDL files
on SQLServerCentral.com. Just happened to read the email, today.
SSRS is a powerful tool, but the ReportServer tool lacks some administrative reporting. Micahel Dais has done a great job filling in that gap!
Super Easy to deploy and gives the end user a better glimpse
into what’s happening on their SSRS installation. While it is
written for a SQL 2005 , some posters announced they had no
problems working against SQL 2008 installations.
Thanks Michael!
T-SQL Using Parameter Table like a Cursor
The Google gods seem to like a very old page of mine about using Cursors in T-SQL. So, from time to time I get commnets via email. Thought I would share an alternative to using cursors.
Except and Intersect operators in SQL Server 2005/2008 (from decipherinfosys)
Good Explanantion and usage examples of the Except and Intersect Operators in SQL 2005 ( 2008).
From decipherinfosys
In SQL Server 2005, MSFT introduced two new operators in their T-SQL language: Except and Intersect. We had briefly touched upon these while discussing the MERGE command in Oracle – you can read more on that post here. EXCEPT returns the distinct data value from the left query (query on left side of the operand) which does not exist in the right query (query on the right side of the operand). INTERSECT returns data value which is common in both queries. In Oracle, the equivalent of the EXCEPT operator is MINUS and INTERSECT is same as INTERSECT. In SQL 2005, using EXCEPT and INTERSECT, one can also simulate the MERGE command in Oracle (see the blog post that is mentioned above).
Read Full Post Here
SSRS – Format() and FormateDateTime()
Found this reference … JUST IN TIME!
Thanks Thavash This is by far my most visited page.
http://thavash.spaces.live.com/blog/cns!CF6232111374DFD2!155.entry
Working with Dates in Reporting Services
Command | Result |
FormatDateTime(Parameters!Date.Value,1) | Tuesday, April 10, 2007 |
FormatDateTime(Parameters!Date.Value,2) | 4/10/2007 |
FormatDateTime(Parameters!Date.Value,3) | 12:00:00 AM |
FormatDateTime(Parameters!Date.Value,4) | 00:00 |
…but the better way to do it would be to use …
2) The Format command and specify the exact format you require. For example…
Command | Result |
Format(Parameters!Date.Value,”dd-MM-yyyy”) | 10-04-2007 |
Format(Parameters!Date.Value,”dd/MM/yyyy”) | 10/04/2007 |
Format(Parameters!Date.Value,”MMM-dd-yyyy”) | Apr-10-2007 |
Format(Parameters!Date.Value,”MMM-dd-yy”) | Apr-10-07 |
So 3 M’s give you “Apr” ….anyway this is quite useful if you’re looking for Day/Month/Year , since the system will default to MM/DD/YYYY.
Using this you should be able to display the date format you want , or send through a particular format to a Stored Proc.
EDITED 22/08/2007 : If the Format Command doesn’t work , try converting the value to a date , eg.
Format(Cdate(Parameters!Date.Value),”dd-MM-yyyy”) |
Jeff’s SQL Server Blog – Proper Case UDF
Using My Favorite Tech Service –http://www.Google.Com.
I Found this Posting for a Proper case T-Sql User Defined Function on Jeff’s SQL Server Blog — Go to the post as it has an port to MySQL and a couple of other ‘ADVANCED’ examples in the comments.
From the original Blog :
I posted this one a long time ago and needed to use it today, so I thought I’d post it up here as well in case anyone finds it useful. This simply attempts to capitalize the first letter of each word for the string passed in. Use it to help clean up some pre-existing data, but don’t use it as a way of presenting your data that is stored improperly since it isn’t exact.�
Read more »