in

CodeSmith Community

Your Code. Your Way. Faster!

Database Field Collation settings (SchemaExplorer.ColumnSchemaCollection) & ExtendedProperties

Last post 05-05-2008 9:04 PM by blake05. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 04-15-2008 8:41 PM

    • Clayton
    • Top 500 Contributor
    • Joined on 01-27-2004
    • Brisbane, Australia
    • Posts 15
    • Points 230

    Database Field Collation settings (SchemaExplorer.ColumnSchemaCollection) & ExtendedProperties

    G'day,

    I've got a SQL Server 2005 database with mixed collation settings (2000 upgrade + some developer environmental differences caused the problem).

    I can identify the collation settings via the following SQL Server Statement.

    SELECT TABLE_CATALOG, TABLE_SCHEMA , TABLE_NAME,

    COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,

    IS_NULLABLE, COLUMN_DEFAULT, COLLATION_NAME

    FROM INFORMATION_SCHEMA.COLUMNS AS cols

    WHERE (TABLE_CATALOG = N' yourdbnamegoeshere') AND (NUMERIC_PRECISION IS NULL)

    AND NOT(COLLATION_NAME IS NULL)

    ORDER BY TABLE_NAME, COLUMN_NAME

     

    I created a simple little Codesmith template to find out if the extended properties contained the Collation_Name values.

    <%@ CodeTemplate Language="VB" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
    <%@ Property Name="SourceSchema" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the code should be based on." %>
    <%@ Assembly Name="System.Data" %>
    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Assembly Name="CodeSmith.BaseTemplates" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="SchemaExplorer" %>
    <%  

     Dim strDefaultValue as string = ""
     Dim objSortedCols As SchemaExplorer.ColumnSchemaCollection

        ' Sort the column in the table alphabetically, so we have consistent changes through all types of Databases.
     objSortedCols = New SchemaExplorer.ColumnSchemaCollection(SourceSchema.Columns)
        objSortedCols.Sort(New SchemaExplorer.PropertyComparer("Name"))
     
    %>

    <% For intLoop as integer = 0 to objSortedCols.Count -1 %>
    [<%= SourceSchema.Name %>].[<% = objSortedCols.Item(intLoop).Name %>]
    <%
     For intEPLoop as integer = 0 to objSortedCols.Item(intLoop).ExtendedProperties.count -1
    %>   
     <% = objSortedCols.Item(intLoop).ExtendedProperties(intEPLoop).Name %> = <% = objSortedCols.Item(intLoop).ExtendedProperties(intEPLoop).Value %>
     <% Next intEPLoop %> 
    <% Next intLoop %>

    However in Codesmith 3.x there are no Collation extended properties for collation.  I search for a matching property, but could not find one.  How can I use codesmith to build an ALTER table script to make my field collation settings consistent?

    Jamie Clayton
    Application Developer
    Jenasys Design
    • Post Points: 35
  • 05-05-2008 9:04 PM In reply to

    • blake05
    • Top 25 Contributor
    • Joined on 04-03-2008
    • Wisconsin
    • Posts 166
    • Points 2,790

    Re: Database Field Collation settings (SchemaExplorer.ColumnSchemaCollection) & ExtendedProperties

    Answer

    Hello,

    The following attached file should work. I have only tested it on an old database that I had lying around, so if you find a bug please post the fix.

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC
    Support Specialist
    Filed under:
    • Post Points: 5
Page 1 of 1 (2 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems