Differences between IE8 Compatibility View and IE7

The following link is from the IE blog and lists differences between IE 7 and IE 8 compatibility mode rendering.

Differences between IE8 Compatibility View and IE7

Advertisements

CSS Reset Stylesheets

Here are two reset stylesheets that I’ve found recently:

JavaScript eval function with JSON

This post contains a simply example of how to use the eval function in JavaScript to make a string containing JSON into an actual JavaScript object.

The example JSON was from here and below is the code.

Please note:

The parenthesis are required because of an oddity in the JavaScript syntax. I’m unsure of the reasons at the moment. It is briefly mentioned on this page.

It is probably better to use this -> http://www.json.org/json_parse.js … than to use eval though as it is a parser. Eval seems to be considered less secure though it is probably faster.

<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">

var jsonString = '{ "glossary": {"title": "example glossary", "GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}';
var title = eval('(' + jsonString + ')').glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para;
alert(title);

</script>
</body>
</html>

JQuery Visual Studio documentation only on Debug

I’ve included my scripts using a dynamic method that means Visual Studio can’t determine what scripts are available to a given page.

This removed the code completion for scripts, and I found the code completion helpful especially as I am learning JQuery and also due to the fact that you can never remember everything 🙂 I also wanted to prevent the script loading when

To get around this issue I’ve included the scripts like this

<% #if DEBUG %>
<!-- Load VS JQuery documentation if in debug mode! -->
<script src="../../Scripts/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
<% #endif %>

Which ensures that the Script include is only included in DEBUG builds… this means that when I would go to production that script would not be included in the page…

This doesn’t seem (at the moment) to cause issues with the real registration of JQuery… It’s not the most perfect, but it’s a safeguard  against me forgetting 🙂

Update: It’s actually not being found at runtime… So maybe that is why it is not causing issues 🙂

OUTPUT Parameters in Stored Procedures and RETURNing a Status

I needed to learn how to use Output Parameters in Stored Procedures within Sql Server 2005. I wanted also to do this only in Sql for starters. I also wanted to return a value to the calling code.

You can return a value from stored procedures that you can use to designate success or failure or some other status.

I’ve never used the Output parameter before, though it is apparently faster to return a value as an output parameter as a result set doesn’t have to be created (I’m not sure where i read that. But it basically said that it is a little quicker…)

Here is a small example of doing how to do this. Note the following:

  • You need to put OUTPUT after the parameter name in the stored procedure
  • To return a status value you use RETURN
  • When executing the stored procedure you need to specify OUTPUT after passing in the parameter values (for some reason ?!)
  • You need to assign the result of the stored proc to a variable to get the RETURNed value

Here is the Sql code example:

CREATE PROCEDURE [dbo].[FormatMyNameManyTimes] (
    @FirstName  nvarchar(100) = '',
    @LastName   nvarchar(100) = '',
    @DefaultResult nvarchar(200) OUTPUT
)
AS
BEGIN
    SELECT @FirstName + ' ' + @LastName
    UNION ALL
    SELECT @LastName + ', ' + @FirstName
    UNION ALL
    SELECT @FirstName + ' ' + @LastName
    UNION ALL
    SELECT @LastName + ', ' + @FirstName
    UNION ALL
    SELECT @FirstName + ' ' + @LastName
    UNION ALL
    SELECT @LastName + ', ' + @FirstName
    UNION ALL
    SELECT @FirstName + ' ' + @LastName;

    SELECT @DefaultResult = @FirstName + ' ' + @LastName;

    RETURN 404;
END

DECLARE	@return_value nvarchar(100);
DECLARE @Default nvarchar(200);

EXEC	@return_value = [dbo].[FormatMyNameManyTimes]
            @DefaultResult = @Default OUTPUT,
            @FirstName = N'Silly',
	    @LastName = N'Sausage';

SELECT  'Return Value' = @return_value;
SELECT  '@DefaultResult' = @Default;

DROP PROCEDURE [dbo].[FormatMyNameManyTimes];

Collations on Sql Server 2005+

This post is about collations in Sql Server 2005+.

I will give some simple information on:

  • What Collations are Available
  • What is the Server Collation
  • What is a Databases Collation
  • Altering Database Collation
  • Comparing Columns with different collations

What Collations are Available?

fn_helpcollations() function

SELECT * FROM fn_helpcollations()

Use this to view all available collations (name and description)

What is the Server Collation?

There are a few ways to do this!

sp_helpsort procedure

sp_helpsort

This will return the description of the collation that is default for the server.

SERVERPROPERTY function

SELECT SERVERPROPERTY('collation')

This will return just the name of the default server collation

What is a Databases Collation

SELECT DATABASEPROPERTYEX('MyTemporaryDatabase', 'Collation')
    AS DatabaseCollation;

This will return the collation of the specified database (“MyTemporaryDatabase”).

Altering Database Collation

ALTER DATABASE MyTemporaryDatabase
    COLLATE Latin1_General_CS_AI

Comparing Columns with Different Collations

To compare columns in a table with different collations you want to convert them to the same collation.

You can either specify the specific collation as follows:

SELECT  Data
FROM    MyDataTable
        JOIN MyOtherDataTable
WHERE   MyDataTable.Data COLLATE Latin1_General_CS_AI
        = MyOtherDataTable.Data COLLATE Latin1_General_CS_AI

Or you can specify that you want to convert the column(s) to the current databases default collation as follows:

SELECT  Data
FROM    MyDataTable
        JOIN MyOtherDataTable
WHERE   MyDataTable.Data COLLATE DATABASE_DEFAULT
        = MyOtherDataTable.Data COLLATE DATABASE_DEFAULT

Returning the NewSequentialID() after Insert using the Output Clause

Update 2009-12-22 – Added information and link on getting the ID from .NET DbCommand object.

The Problem

The Problem is that we have a table that uses the NewSequentialID() function on the Default constraint. We want to be able to run a stored procedure to insert a new row and return the newly inserted GUID generated by NewSequentialID() much like we would when we have an IDENTITY column and use @@IDENTITY or SCOPE_IDENTITY().

Solution(s)

I recently read an article that suggests the creation of a trigger that get’s the ID that NewSequentialID() generated from the Inserted table (I’ve not tested this) (See idea 2 here)

There seems to be a better way gleaned from this article… I never knew you could do this!

See Also: Sql Server Books Online details on the Output Clause

First Lets Create our Table (Run this on a test database!!!)

CREATE TABLE MyEntity(
    EntityID uniqueidentifier NOT NULL PRIMARY KEY DEFAULT NewSequentialID(),
    EntityName nvarchar(100) NOT NULL
);
GO


And now a sproc to do the insert. Notice the Output clause!!!!

CREATE PROCEDURE MyEntity_Insert
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO MyEntity (EntityName) OUTPUT inserted.EntityID VALUES('William');
END
GO


Now lets call it and get the returned value.. While we are at it we will SELECT all the rows ( all one of them 🙂 ) and compare the EntityID values.

DECLARE	@return_value int
EXEC	@return_value = [dbo].[MyEntity_Insert]
SELECT	'Return Value' = @return_value

SELECT * FROM MyEntity


Just to clean up!

DROP PROCEDURE MyEntity_Insert;
DROP TABLE MyEntity;

.Net – Getting the newly Generated Guid

Update 2009-12-22

Bloke at work directed me to this article which shows how to get this value from .NET (at the time of the original writing of this post, I was only interested in getting the Sql working… seeing if it was possible from the database side to do what I wanted… We were planning at the time.)

To get the OUTPUT value in the above case you should just have to go.

var id = (Guid)command.ExecuteScalar();