Here's what I did:

Create a class library project containing a public and static function. I wrote the following to convert someone's name into having an upper case first character and lower case characters after that. CLR stored procedures are good for string handling, and often people enter their names in a database in an inconsistent format.
public class NameProcessor
{
public static String ProcessName
(String name)
{
String rv;
rv = name.Substring(1,1).
ToUpper() + name.Substring(2).ToLower();
return rv;
}
}
I know there is a mistake in this, but we'll debug it later.



create assembly processstring from
‘c:\Documents and Settings\Richard\My
Documents\...\classlibrary1.dll’

select * from sys.assemblies

Create function dbo.pn(@name nvarchar(100))
returns nvarchar(100)
external name processstring.NameProcessor.ProcessName
Note this has to use an nvarchar type

select dbo.pn('abc')
Oops - this returns 'Bc', not what we wanted

Add the PDB file to your assembly:
alter assembly processstring
add file from 'c:\Documents and Settings\Richard
My Documents\...\classlibrary1.pdb'

- Select "Attach to Process" from the tools menu
- Select "Show Processes from all users" in the bottom left.
- Select "sqlservr.exe", and select "Attach to Managed Code"
- Add a breakpoint in your function

Go back into Management Studio
select dbo.pn('abc')
Your breakpoint is hit. Correct the code.
As this is .net, you can add a unit test for
this with nUnit if you're so inclined.

Drop your PDB File from your assembly
alter assembly processstring
drop file 'c:\Documents and Settings\Richard
My Documents\...\classlibrary1.pdb'
Update your DLL
alter assembly processstring from
'c:Documents and Settings\Richard
My Documents\...\classlibrary1.dll'

select dbo.pn('abc')
Excellent - this now works correctly.
No comments:
Post a Comment