Friday, September 17, 2010

If At First You Don't Succeed, Make Sure You Added the DOCTYPE

I just thought of something that can be ridiculously aggravating.

Picture this: You're working along, making good progress structuring your HTML with beautiful DIV, UL, P elements, among other SEO-friendly markup. The CSS is nicely organized and as cross-browser safe as you can possibly make it. You're developing in Firefox or Chrome and everything looks fantastic, but you figure it's a good idea to check out IE since, while good developers know the tools on other browsers are vastly superior, this is largely still the public's browser of choice. And in order for any site to be successful you have to develop to the lowest common denominator, right? But wait, what's this!? IE just barfed all over your perfect markup, exclaiming "You don't know what you're doing, kid. Nice try!" Well, rather than throwing your computer out the window or starting from scratch, you might double-check that you didn't forget to include the DOCTYPE element at the top of your page. Forgetting to specify the document type can be the cause of some of the most annoying rendering bugs, so it pays to check.

Mike

Tuesday, September 14, 2010

Yet Another Syntax Highlighter For the Web and Blogs

Today I was looking at the ol' tech blog thinking that while my current code syntax rendering is okay, it's not great. There must be something better. So I poked around the web for a bit looking, at a few other prominent technical blogs, and I came across an excellent JavaScript-based syntax highlighter appropriately named, well, SyntaxHighlighter. I know, sometimes fancy marketing names just don't make the to-do list.

The library is configurable on a number of fronts. Probably the most important feature is the ability to add what the creator calls brushes. These are essentially code files specifically tuned to the particular language's syntax you're looking to highlight. For instance, there are SQL, JavaScript, Java, and C# brushes, among others. Another nice add is the ability to tune the overall formatting via a CSS theme. This allows you to tweak the colors to various display types including but not limited to Eclipse and Emacs. Pretty nice indeed. Lastly, you don't even need to host the requisite files. The creator, Alex Gorbatchev, provides free hosting - all he asks is for a donation to help with the Amazon S3 costs.

Configuration

Configuration is also pretty much a breeze. If you're modifying an existing blog template on Blogger, as was the case for this site, you'll probably want to do a backup of your existing template. Login to your account and navigate to the Design tab. From here you'll be looking for the sub-menu option entitle Edit HTML. Backup your template via the supplied download link. That being done, search for "" in the template document in the textarea below. Just above the closing "" element is where you're going to insert the following code.

<!-- Syntax Highlighter code -->

<!-- SH::Setup CSS -->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css' rel='stylesheet' type='text/css'/>

<!-- SH::Setup JS -->
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/>

<!-- SH::Configure Brushes -->
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>

<!-- SH::Run syntax highlighter -->
<script type='text/javascript'>
     SyntaxHighlighter.config.bloggerMode = true;
     SyntaxHighlighter.all();
</script>

There are four main sections here:
  1. CSS
  2. JavaScript core files
  3. Syntax brushes
  4. Config and run highlighter

If you're running this in Blogger, you should include the line config.bloggerMode=true, as per the documentation. You'll notice I've included a number of brushes. There is also an autoloader feature that sounds like it should offload some downloading on pages that don't require all the brush files, but I haven't tried it out yet. The last thing to do is use the highlighter in a blog post. It really doesn't get much more complicated than the following:

<pre class="brush:js">
function testMeOut()
{
    alert("You should really try this out.");
}
</pre>

In this example I've decided to highlight a simple JavaScript function by specifying a brush type of "js" in my outer "<pre>" class definition.  One important thing to note is that there are actually two different ways to go about highlighting with this library. I've used the "<pre>" tag method, as this is the recommended method for blogs, but it requires you to HTML escape your code. It's a minor inconvenience, but still important to note because the syntax highlighting will not work if you have less-than "<" symbols in the code you are trying to highlight.

That should do it. If you're not seeing the correct results, make sure you've included all necessary files, referenced your brush types correctly, and escaped your HTML.

Links

Thursday, September 9, 2010

Dropping Multiple Stored Procedures

Occasionally when you're working with a database, e.g. your local dev environment, you need to do a little bit of cleanup. Let's say you run a stored procedure creation script from with SQL Server Management Studio and forget to specify the target database. So instead of having a bunch of procedures in the intended database you now have them in another database or, perhaps worse, in your master database.

SSMS doesn't allow you to select multiple procs to run commands on at once, so you have to select them one-by-one and issue the drop. I don't know about you, but there's no way I want to do that for ten, let alone fifty or more stored procedures. A better way to approach this is to instead write a batch processing script that loops over a list of stored procedure names and executes the DROP command on each of them.

The trick here is to get a list of user-defined stored procedures that fall under the desired catalog (database) and loop over that list with a cursor. The reason for this is that unlike a WHERE clause you can't pass a list to the DROP PROCEDURE command. To accomplish a batch DROP, you have to store the resulting value from the current row retrieved from the cursor and execute the command on each value inside a loop. The complete code is shown below.

* Please be advised this code is provided as is with no warranties of any kind. Don't run this on a production environment if you're not absolutely certain as to how it will affect your database system.

--
-- DROP USER-DEFINED STORED PROCEDURES
--

USE MASTER; -- Change to desired database
GO
SET NOCOUNT ON;

PRINT '---- STARTING ----';

DECLARE @UserStoredProcedureName    VARCHAR(200),
        @Command                    VARCHAR(200);

PRINT '---- Creating Cursor... ----';

DECLARE SPCursor CURSOR SCROLL STATIC READ_ONLY FOR
SELECT SPECIFIC_NAME 
  FROM INFORMATION_SCHEMA.ROUTINES
 WHERE SPECIFIC_CATALOG = 'master' -- Change to desired database
   AND SPECIFIC_NAME IN 
   -- This inner select is likely redundant, but I'm not clear on all the 
   -- differences between sys.objects and INFORMATION_SCHEMA
 (
    SELECT name FROM sys.objects
     WHERE type='P'
       AND is_ms_shipped = 0 
       -- A value of 0 represents user-defined procs
       -- Please note, some diagram utility stored procs don't follow this rule
 );

PRINT '---- ...Finished Creating Cursor ----'

PRINT '---- Opening Cursor... ----';

OPEN SPCursor;

PRINT '---- ...Finished Opening Cursor ----';

PRINT '---- Retrieving First Rec... ----';

FETCH NEXT FROM SPCursor
INTO @UserStoredProcedureName;

PRINT '---- ...Finished Retrieving First Rec ----';

PRINT '---- Starting Command Loop... ----';

WHILE(@@FETCH_STATUS = 0)
BEGIN

    -- Include a schema name as a prefix, if necessary
    SET @Command = 'DROP PROCEDURE dbo.' + @UserStoredProcedureName;
    
    PRINT '----     executing:: ' + @Command;
    
    --SELECT @Command
    
    --***Only uncomment the line below if you're sure the correct procs will be affected***
    --EXEC(@Command)
    
    FETCH NEXT FROM SPCursor
    INTO @UserStoredProcedureName;
    
END;

PRINT '---- ...Finished Command Loop ----';

PRINT '---- Closing Cursor... ----';

CLOSE SPCursor;

PRINT '---- ...Done Closing Cursor ----';

PRINT '---- Deallocating Cursor... ----';

DEALLOCATE SPCursor;

PRINT '---- ...Done Closing Cursor ----';

PRINT '---- FINISHED ----';

SET NOCOUNT OFF;