Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Rodske 74 posts 104 karma points
    Jun 08, 2010 @ 14:56
    Rodske
    0

    Umbraco tables to flat document type tables

    When needing to conduct data ETL processes and native data logic (stored procs, ssis packages) etc, you can't bet a native object table.
    (Unless you can create a CLR stored proc that uses the umbraco.library)

    Below is a SQL query you may want to use to flatten that cmsPropertyData table out into working object tables. ;-)

    CREATE VIEW [dbo].[umbracoContent] AS
    SELECT
        node.id nodeID, 
        cd.versionId as versionID, 
        cd.published as show, 
        node.parentID, 
        node.nodeUser, 
        node.[level], 
        node.sortOrder,                      
        cc.contentType,         
        isnull(cd.templateId,dct.templateNodeId) as templateNodeId, 
        cd.documentUser,                     
        1 as language,                     
        node.createdate as createdate,               
        cd.updateDate,                     
        cd.releasedate,                     
        cd.expiredate,                     
        node.path,                     
        cd.text as nodeName,          
        lower(cd.text) as urlName,       
        versionUser.userName as versionUserName,           
        nodeUser.userName as nodeUserName,           
        ct.alias,         
        ctNode.text as nodeTypeName        
    FROM umbracoNode node           
        inner join cmsContent cc on cc.nodeId = node.id     
        inner join cmsDocument cd on cd.nodeId = node.id and published = 1     
        inner join cmsContentType ct on ct.nodeId = cc.contentType     
        inner join umbracoNode ctNode on ctNode.id = ct.nodeId     
        left join cmsDocumentType dct on dct.contentTypeNodeId = ct.NodeId and dct.isDefault = 1    
        left join umbracoUser nodeUser on nodeUser.id = node.nodeUser           
        left join umbracoUser versionUser on versionUser.id = cd.documentUser    
        and node.nodeObjectType = 'C66BA18E-EAF3-4CFF-8A22-41B16D66A972'
        and node.parentID <> -10
    GO

    CREATE VIEW [dbo].[umbracoContentData] as    
    SELECT 
        versionId as versionId, 
        cmsPropertyType.Alias as alias, 
        COALESCE(dataNtext, dataNvarchar, convert(nvarchar(1000), dataDate),convert(nvarchar(1000), dataInt)) as content 
    FROM  cmsPropertyData
    INNER JOIN cmsPropertyType on cmsPropertyType.id = cmsPropertyData.propertyTypeId
    GO

    CREATE PROCEDURE dbo.umbracoTabularObject
        @docTypeAlias nvarchar(30) ,
        @parentNodeId int=0,
        @liveOnly bit=0
    AS
        --Stand-alone parameters
        --drop table #columns
        --declare @docTypeAlias nvarchar(30), @parentNodeId int
        --select @docTypeAlias = 'SpecialsEdition', @parentNodeId=0
       
        declare @contentTypeId int, @sql nvarchar(4000)
        set @sql = 'DROP TABLE [dbo].[objectTable_'+@docTypeAlias+']';
        IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[objectTable_'+@docTypeAlias+']') AND type in (N'U'))
            exec sp_executesql @sql;

        select @contentTypeId=contentTypeNodeId
        from cmsContentType ct
            left join cmsDocumentType dct on dct.contentTypeNodeId = ct.nodeId
        where alias=@docTypeAlias

        -- create columns temp table
        SELECT
            ROW_NUMBER() OVER(ORDER BY tabid,sortorder) AS idx
            ,cast((case dbType
                when 'Integer' then 'int'
                when 'Nvarchar' then 'Nvarchar(1000)'
                when 'Ntext' then 'Ntext'
                when 'Date' then 'Datetime'
                end) as varchar(20)) as tableType
            ,cast((case dbType
                when 'Integer' then 'dataInt'
                when 'Nvarchar' then 'dataNvarchar'
                when 'Ntext' then 'dataNtext'
                when 'Date' then 'dataDate'
                end) as varchar(20)) as umbracoColumn
            ,Alias
        INTO #columns
        FROM [cmsDataType] dy
            inner join [cmsPropertyType] pt on dy.nodeid=pt.dataTypeId
        WHERE contentTypeId=@contentTypeId
         
        set @sql = 'SELECT
            ROW_NUMBER() OVER(ORDER BY sortOrder) AS idx
            ,nodeID
            ,versionID
            ,parentID
            ,level
            ,sortOrder
            ,nodeName
            ,path
            ,releaseDate
            ,expireDate
            ,createDate
            ,updateDate
            ,nodeUserName as ''updateBy'''
           
        declare @i int, @max int
        select @i=1, @max = max(idx) from #columns
        while @i<=@max
            begin
            select @sql = @sql + ',cast(null as ' + tableType + ') as ''' + Alias + ''''
                from #columns where idx=@i
            set @i = @i+1
            end
        set @sql = @sql + ' INTO [objectTable_'+@docTypeAlias+']
        FROM umbracoContent
        WHERE alias=@docTypeAlias'
       
        IF @liveOnly=1 SET @sql = @sql + ' AND (getdate() BETWEEN isnull(releasedate,getdate()) AND isnull(expiredate,getdate()))'
        --AND show=1
       
        IF @parentNodeId >0 SET @sql = @sql + ' AND parentID='+@parentNodeId
       
        exec sp_executesql @sql, N'@docTypeAlias nvarchar(30)',@docTypeAlias
       
        -- =============== update each column =================
        select @i=1
        while @i<=@max
            begin
            select @sql = 'UPDATE [objectTable_'+@docTypeAlias+']
                SET ['+ Alias +']=' + umbracoColumn +'
            FROM [objectTable_'+@docTypeAlias+'] ot
                INNER JOIN cmsPropertyData pd ON pd.versionID=ot.versionID
                INNER JOIN cmsPropertyType pt ON pt.id = pd.propertyTypeId
            WHERE pt.Alias='''+Alias+'''' from #columns where idx=@i
            --print @sql
            exec sp_executesql @sql       
            set @i = @i+1
            end
       
        --set @sql =  'SELECT * FROM [objectTable_'+@docTypeAlias+']'
        --exec sp_executesql @sql
    GO

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies