April 2009
Mon Tue Wed Thu Fri Sat Sun
« Mar   May »
 12345
6789101112
13141516171819
20212223242526
27282930  

Day April 2, 2009

Fun with PATINDEX() and DB_NAME()

I’m going to guess everyone can guess that DB_NAME() will give you the name of the current database when called with no parameters. If not, now you know.

PATINDEX() is a little bit more fun, so before we get into things, here’s how PATINDEX() works:

DECLARE @my_var AS NVARCHAR(30);
SET @my_var = 'fun with patterns';

SELECT PATINDEX('%pattern%', @my_var);
-- returns 10

PATINDEX() returns the position of the pattern in the search expression. It’s 1 indexed for those of you who come from a 0-based string world.

Moving forward!

So far, I haven’t done anything cunning. But what if you have a crazy situation like this:

You have a publisher database. There are two subscriptions set up with bidirectional transactional replication. They are named Subscriber1 and Subscriber2. They are stored on separated physical servers, but they have identical schemas. Funnily enough, and this is where it gets tricky, there’s another database in the picture. One each server there’s a Web database (WebOne and WebTwo). It does reporting and uses tables in the subscriber. It also holds all the stored procedures. Suddenly things get a lot more entertaining because you now have to fully reference table names.

(Is this confusing enough without pictures?)

What to do?!?

Well, for 99% of of your stored procedures, you can use CTRL+H and ignore this blog post. You probably can ignore this blog post for the other 1%. Unless you’re insane, like me, and generate ad hoc SQL from a CLR assembly. If that’s the case, keep reading.

So, I have a CLR assembly which contains a function. That function emits SQL code which is executed via sp_executesql. Rather than maintain two versions of this assembly, one for each server, I decided to use REPLACE, PATINDEX, and DB_NAME to make my life more fun.

DECLARE @condition AS NVARCHAR(10);
SET @condition = '%Two';

IF (SELECT PATINDEX(@condition, DB_NAME())) > 0
BEGIN
  SELECT @magic = REPLACE(@magic, 'Subscriber1', 'Subscriber2');
END

That’s it. It’s pretty simple, really. This is the part where you remark on how smart I am and send me money via PayPal.

PASS Summit Call For Speakers – 8 Days Left

I finally put together my abstracts and submitted them to PASS for the 2009 Summit. If you want to speak, but haven’t submitted abstracts yet, you better hop to it.

I figured that I would go ahead and list my abstracts here so you can copy them, fix the typos, and submit them as your own.

A Dynamic World Demands Dynamic SQL

Dynamic SQL is a misunderstood and much maligned part of a DBA’s tool kit – it can be used to solve difficult business problems, respond to diverse data needs, and alleviate performance problems. Many DBAs reject dynamic SQL outright as a potential source of SQL injections, being poorly performing, or just for being a hacky solution in general. Not so!

Jeremiah Peschka has been making extensive use of dynamic SQL throughout his career to solve a variety of problems. He’ll set about dispelling these misconceptions and demonstrate how dynamic SQL can become a part of every DBA’s tool kit.

Session Goals

  • Learn how to work effectively with diverse inputs.
  • Learn how to avoid common pitfalls of dynamic SQL.
  • Develop an understanding of real world uses for dynamic SQL.

Digits, Dates, and Databases

Calendar and number tables migh not help you get a date, but they can help you accomplish difficult tasks in the database while sticking true to your set-based roots. Nobody wants to admit that they’ve used a while loop in their SQL code, but there are ways to avoid looping constructs and use a set-based solution using a numbers table for loop control. Date manipulation can be cumbersome to peform, can create unwieldy and unreadable code, and can even create performance bottlenecks. Using a calendar table can alleviate these performance bottlenecks and make many calculations easy.

Session Goals

  • Learn what number and calendar tables are.
  • Learn how to effectively use a numbers table in set-based SQL.
  • Learn how to work with a calendar table to remove the need for complex date manipulation.

What are you waiting for? Submit your sessions today!

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 401 items have been purified.