Quantcast
Channel: Thoughts, codes, occasional rants, many others. » SQL Server
Viewing all articles
Browse latest Browse all 11

What’s up with the types?

$
0
0

This rant deserved a post!

Early this morning, I was jet set to write a script that would duplicate a (package) and give it a new name. So basically, idea was to get the identity value of the old package, and insert it as a new with same attributes but different package names. Based on the insert, it would do the same for all corresponding child tables. To help understand better, something like this:

INSERT INTO dbo.PackageTable(PackageDesc, attribute1, attribute2)
SELECT
@newPackageDesc AS PackageDesc
, attribute1
, attribute2
FROM dbo.PackageTable
WHERE PackageID = @oldPackageID

I got a bunch of error messages such as:

There was an error! Error converting data type varchar to numeric.

After dwelling around for almost 45 minutes, (when I was planning to go back to take Database 101 after all these years) guess what I discover? One of the columns is using user defined type such as:

[PackageDesc] [dbo].[Desc_Short] NOT NULL

and the Desc_Short was defined as:

CREATE TYPE [dbo].[Desc_Short] FROM [varchar](30) NOT NULL
GO

Firstly, I see no benefit of doing that, and I wanted to find out some use cases where you could benefit from defining a UDF. Came across this blog post:

http://blogs.msdn.com/b/bartd/archive/2010/08/25/t-sql-udts-what-are-they-good-for.aspx

I will quote a couple of paragraphs from this post just to make this blog post a bit more convincing:

“The attempt to make the app developer’s life easier can backfire and actually made it a little harder.”

Now, I was not going to do this, but I wanted to check how much effort is really needed to get away from UDTs. Turns out:

1) There is no easy way to drop types without first modifying all the dependencies.
2) Even if you change all the object definitions (for columns), you still have to find all the parameters/variables that is being used. (SQL Search will come handy here).
3) What happens when your type has to be modified? Lets say, Desc_Short in the above example has to accomodate varchar(40) instead of varchar(30)? There is no alter types, so you will have to repeat all the steps above (I am assuming that if you have to do this, you will not go back to types again).

Bottom line: We should not add more dependencies than there already is. So, please, do not use user defined types unless you can really justify it.


Viewing all articles
Browse latest Browse all 11

Trending Articles