Category: Blog

  • The IF Statement in a T-SQL Query

    I’ve seen quite a few posts from people asking how to do something like this:

    SELECT 
      a.ID
    , IF a.MyChar = 'A' THEN 'Success'
      ELSE 'Fail'
    FROM MyTable a

    Of course, that doesn’t work in T-SQL, and you’ll get something like this:

    Msg 156, Level 15, State 1, Line 3

    Incorrect syntax near the keyword ‘IF’.

    Msg 156, Level 15, State 1, Line 3

    Incorrect syntax near the keyword ‘THEN’.

    There’s not IIF, no IF( x, then y, else z) construct. There is an IF … ELSE statement, but it’s use in code flows as a control statement such as

    DECLARE @i CHAR(1)
    SELECT @i = mychar FROM MyTable
    
    IF @i = 'A'
      SELECT 'Success'
    ELSE 
      SELECT 'Fail'
      

    Instead we have a CASE statement, which is designed to give you multiple choices. In the example above, I’d write:

    SELECT 
      a.ID
    , CASE WHEN a.MyChar = 'A' THEN 'Success'
      ELSE 'Fail'
      END
    FROM MyTable a

    I can even add multiple “WHEN” clauses if I want:

    SELECT 
      a.ID
    , CASE 
        WHEN a.MyChar = 'A' THEN 'Success'
        WHEN a.MyChar = 'B' THEN 'Close'
        WHEN a.MyChar = 'C' THEN 'Far'
        ELSE 'Fail'
      END
    FROM MyTable a

    Let your developers know that when they are looking for an inline IF type of logical statement, T-SQL gives them CASE instead.

  • Recovering a Database

    So you’re restored a database, restored a few logs, all with NORECOVERY as expected and realize there are no more logs. You see this in Management Studio and wonder what do to:

    restoringdb

    I’ve run a bunch of code, restoring lots of files, but I’m done.

    RESTORE DATABASE db4 FROM DISK = 'db4_base.bak' WITH norecovery
    RESTORE LOG db4 FROM DISK = 'db4_log1.trn' WITH norecovery
    RESTORE LOG db4 FROM DISK = 'db4_log2.trn' WITH norecovery
    RESTORE LOG db4 FROM DISK = 'db4_log3.trn' WITH norecovery
    RESTORE LOG db4 FROM DISK = 'db4_log4.trn' WITH norecovery
    ....
    RESTORE LOG db4 FROM DISK = 'db4_log42.trn' WITH norecovery
    

    You don’t need another log to bring things online. This simple command will fix things:

    RESTORE DATABASE db4 WITH recovery
    

    That will return:

    RESTORE DATABASE successfully processed 0 pages in 2.629 seconds (0.000 MB/sec).

    and your database will be ready to go:

    retoreddb

  • Wild West Day

    One of the things I’m missing today is Wild West Day at Red Gate. It’s not just because I don’t live anywhere near the office (they’re in Cambridge, UK, I’m in Denver, CO), but also because I have a vacation scheduled. Camping up near Fort Collins with family is on the agenda, and today should be a fun day of horseback riding for the girls and some biking for the boys.

    redgatewildwestday

    That’s what I expect for our trip, but this was the reminder picture that came from Red Gate, for the company outing next week. They are having a day out event for employees, which sounds fun. I worked in a few companies that had outside events for employees, and those were also the days that really helped people bond together and enjoy their jobs a little more.

    I miss those, and wish I had the chance to do one once in awhile. I might as my wife’s company has about 10 or 12 telecommuters in the Denver area and is looking to funding a day out somewhere.

  • Bloopers

    An early peak at the bloopers going out on Monday. Feel free to enjoy today. I’ve included the editorial and then the raw bloopers from the last month, which has a few more mistakes in there.

    The official Independence Day 2011 Videos

     

    The raw ones are below, and include the ones above plus a few more that didn’t make the cut.

     

    Raw Bloopers