While I was writing a stored procedure that did some string manipulation and was debugging some unexpected behavior, I came across a fascinating, infurating, hidden-in-plain-sight "feature" of the LEN function: it returns the length of a string expression excluding trailing blanks. So, for example:

declare @string varchar(10)
set @string = '123456789 '

declare @len int
set @len = len( @string )

print @len
print right( @string, @len - 5 )

returns 9 and '789 ', not the expected 10 and '6789 '. Also, with this code:

declare @string varchar(10)
set @string = '123456789 ...








The T-SQL PRINT statement is the way to output plain text from SQL scripts and stored procedures. Unfortunately, it has a built-in limitation of printing 8000 non-Unicode or 4000 Unicode characters, and the rest is truncated. I was writing some SQL that generated lengthy SQL scripts that exceeded this limitation, and I wanted to create my own version of PRINT that would split my string at the nearest CRLF, print the part preceding the CRLF, then repeat on the rest of the string to the right of the CRLF. The CRLF itself would be omitted, since each PRINT statement appends ...

I've been writing a lot of dynamic SQL that generates other SQL lately. While SQL isn't the best string manipulation language, it's got great native access to database schema information. However, my latest project was resulting in some very long scripts that coudn't be printed with PRINT, because it truncates the output after 8000 bytes, which is 8000 chars or 4000 nchars. One solution to this would have been to execute dynamic SQL to create a stored procedure containing the text, but in this case, I wanted to append multiple scripts together, so I chose to use PRINT as a simple ...

Using using

Using is one of those dual use keywords in C#. Everybody uses using at the top of your code file so that you don’t have to type all of those long namespaces. The other use is for taking control over the closing and disposing of objects that support the IDisposable interface. Sometimes a little using goes a long way. One IDisposable class that is a good candidate for using is SqlConnection. Without using code to access a database, you would need to use a try-finally block to make sure you free up the resource.

 

SqlConnection conn = new SqlConnection...

...and with it a long missed friend returns!

When I first switched from 2000 to 2005, I was dismayed to find that the scripting options were drastically reduced in functionality. I am glad to announce that in SP2, the old 2000 options are all back!

The service pack contains a lot of new functionality, read all about it here. The service pack can be downloaded here.

How do you find if a table is referenced in a stored proc?  That can be quite the Easter egg hunt without some sort of tool.  The following SQL uses the syscomments table (contains the stored proc text) and joins to the sysobjects table to determine which syscomment records belong to stored procs.  CHARINDEX returns the starting position of an expression in a character string, so the fact that it returns greater than zero signals a hit.

DECLARE @SEARCHSTRING VARCHAR(255)

SELECT @SEARCHSTRING = 'MyTable'

SELECT DISTINCT sysobjects.name
FROM sysobjects,syscomments
WHERE sysobjects.id = syscomments.id
-- ...






This morning I discovered I could enter code to VS but couldn't backspace over it.  This would require me to code perfectly the first time, and for me that's a non-starter.  It turns out that none of the command buttons work.  The short story on the fix for this oddity is to either disable Auto-Hide or open one of the hidden windows, then click the X to close.  More info at the msdn blog site: Tabs, Backspace, Delete and other commands not working in the editor.

If you use Windows Vista and are like me, you have already turned off those annoying prompts generated by User Account Control (UAC). But can you really just turn it off?

Well, it turns out that you can't. I went to install Adobe Acrobat Reader 8.0, which is Vista compatible. After downloading the file I set about installing it, only to be informed that "The Temp folder is on a drive that is full or is inaccessible."

Some googling turned up this Tech Note on Adobes site. According to this note "On Vista, with User Account Control disabled, there ...

The full text of the error reads: "Commit failed. (details follow): 'pre-commit' hook failed with error output: all was unexpected at this time." I've run into this error when trying to commit files in Subversion (with TortoiseSVN) at least twice. At first, I thought it was caused by some obscure state error. I tried doing a cleanup on the directory, and then on the parent directory, with no success. I tried deleting the entire project and downloading it afresh from the repository. Nothing worked, until my associate Noel asked me what comment I'd used. It was then I realized that ...