Amass

MSSQL’s Text and Varchar incompatabilities

November 5th, 2007 Luke

I’ve been working on getting some of my test project out of the door, and I’m at the point where I’m writing the database queries.

As it turns out, you are not allowed, under MSSQL, to use a column of the type text in a WHERE clause. It needs to be a varchar column.

Why? I have no idea.

MSSQL’s Alternative to “DROP TABLE IF EXISTS …”

November 4th, 2007 Luke

I’m working on teaching myself to use ASP.Net and MS SQL right now. It’s…interesting, and there are a lot of things that I sorely miss about MySQL. You really don’t know how good you have it until you don’t.

One thing that I love about MySQL is the DROP TABLE IF EXISTS syntax. It goes something like this:

DROP TABLE IF EXISTS users;

And it does exactly what it says. Quick, easy, and great - and if users doesn’t exist, no nasty errors! I use it in my table-generating code all the time as a just in case measure. Here’s how to do the same thing in MS SQL:

IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'users') DROP TABLE users;

I had to Google for about a half an hour to get that one figured out - it’s not in my book, and it’s a little hard to find out there.

Why? Did Microsoft figure that you’d write code to create your tables that worked perfectly the first time?

MSSQL’s auto_increment equivalent

November 1st, 2007 Luke

I’m getting more and more into using ASP.Net with databases, however…there are still a few things left to learn.

One of those things is that MS SQL does things differently. In MySQL, it’s easy to do something like this:

CREATE TABLE users ( id int not null auto_increment );

And then be done with it. However, MS SQL does not have an auto_increment attribute. It uses the Identity type instead:

CREATE TABLE users ( id int identity(1,1) );

With the first value passed being what it starts counting from, and the second value being what it goes up by.

There are a lot of gotchas involved in making the switch. I get the impression that I’m going to slowly encounter all of them.