April 2007 - Posts

Sql Server Data Type Precedence : Redux

Previously jokiz bloged about this one, I'd like to add further.

The same issue cropped up again in the forums although not the exactly the same as jokiz's experience, this one could really give you hours of head scratching. Previously I was amazed that dynamic ORDER was possible with T-SQL without resorting to dynamic T-Sql using CASE statements:

SELECT p.ProductId -- int
       ,p.ProductName -- varchar
       ,p.EncodedDate -- datetime
       ,p.IsActive    -- bit
FROM dbo.Product p
ORDER BY CASE @sortBy
         WHEN 'Id' THEN p.ProductId
         WHEN 'Date' THEN p.EncodedDate
         ELSE p.ProductName END

The following will work as long as the @sortBy variable is either "Id" or "Date", once it is neither the engine will raise an  "Error converting varchar to datetime." The reason is that the CASE statement has to return an undetermined datatype, it does an implicit conversion according to data type precedence; since DateTime is of higher precedence than varchar, the engine will try to cast the varchar to DateTime hence the error.

A hack would be to cast all the columns to be returned as varchar so it won't give that error. However, you'd have to pad integers in order for it be sorted correctly (ie: converting integers(1,2,10) to varchar will give you (1,10,2), so it should be padded first 0001,0002,0010). This can get really ugly and slow. With this kind of scenario and many criterias, the best way would be to resort to dynamic T-Sql.

On a side note, Sql Server also will implicitly cast statement(s) with operators:

SELECT
columns
FROM dbo.table001 t
WHERE t.FileNo = 12345 -- Where FileNo is of Varchar with 0 pads(ie. 0012345)

Assuming there is no FileNo like 'X012345', the query will be parsed and executed without errors. Of course it woud be prudent to explicitly CAST it, just in case we'll have heterogenous data in the future.

Hope this helps!

Using ConnectionString section in NHibernate

Finally NHibernate (1.2+) is able to take advantage of .NET2.0's connectionStrings section, which previously I'd have to either declare it twice (in either config file or the dedicated hibernate.cfg.xml) All you need to do is, instead of using hibernate.connection.connection_string, use the "newer" hibernate.connection.connection_string_name property.

<connectionStrings>

  <add name="myConnectionSTring" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True"

   providerName="System.Data.SqlClient" />

 </connectionStrings>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
            <!--<mapping assembly="WebApplication1" />-->
            <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
            <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
            <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
            <property name="connection.connection_string_name">myConnectionString</property>
            <!-- HBM Mapping Files -->
        </session-factory>
    </hibernate-configuration>


UPDATE(Thanks to Ayende):Castle's ActiveRecord(RC1/trunk) which uses NHibernate, gives you two option of taking advantage of the connectionStrings sections either by(older one)

<add key="hibernate.connection.connection_string" value="ConnectionString = ${myConnectionString}" />


or the "newer one" which takes advantage of the NH1.2+ support for the connectionStrings sections:

<add key="hibernate.connection.connection_string_name" value="myConnectionString" />

It now makes it more manageable, no need to have separate connectionString sections, more importantly though, is that you can take advantage of connectionString/protectedSections of .NET 2.0.






Posted by bonskijr | 1 comment(s)
Filed under: ,

Expression Studio is now part of MSDN Subscriptions

Via Scott Guthrie

Microsoft finally had a changed of heart and included the Expression Studio as part of MSDN subsription. I don't have to worry about that trial expiring anymore, coz I think Expression Web/Blend is a blast for creating WPF/E applications. Thank you Microsoft!

Official statement from Somasegar