Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Stored procedure with Dynamic columns?

Author  Topic 

webressurs
Starting Member

5 Posts

Posted - 2007-06-28 : 05:04:29
I am making a CMS solution and need help to get out an article list. The reason why this is a bit tricky is because articles does not have the same fields stored in one static article-table. In tblArticle all standard information about each article is stored. In tblArticleAttribute all different kind on information for an article is stored, defines by the templateDefinition table.

Expample: Template 1 has Title, ingress, body). In tblArticleAttribute all content is stored, for example Title Text, Ingress Text and Body text for ArticleId1 and TemplateDefinitionId 1.


SELECT dbo.tblArticle.Id, dbo.tblArticle.PublishStart, dbo.tblArticle.Name AS ArticleName, dbo.tblTemplate.Id AS TemplateId,
dbo.tblTemplate.Name AS TemplateName, dbo.tblTemplateDefinition.Name AS AttributeName, dbo.tblArticleAttribute.[Content] AS AttributeValue
FROM dbo.tblArticle INNER JOIN
dbo.tblTemplate ON dbo.tblArticle.TemplateId = dbo.tblTemplate.Id INNER JOIN
dbo.tblTemplateDefinition ON dbo.tblTemplate.Id = dbo.tblTemplateDefinition.TemplateId INNER JOIN
dbo.tblArticleAttribute ON dbo.tblTemplateDefinition.Id = dbo.tblArticleAttribute.TemplateDefinitionId AND
dbo.tblArticle.Id = dbo.tblArticleAttribute.ArticleId
WHERE (dbo.tblArticle.ParentId = 3) AND (dbo.tblTemplate.Id = 1)


I have made a View that returns 2 articles like this:

ID Publish start ArticleName TemplateID TemplateName AttributeName AttributeValue
4 22.05.2007 Article 1 1 Article Title Welcome
4 22.05.2007 Article 1 1 Article Ingress Hello
4 22.05.2007 Article 1 1 Article Body Cool
4 22.05.2007 Article 1 1 Article Info Yeah..
5 26.06.2007 Article 2 1 Article Title ByeBye
5 26.06.2007 Article 2 1 Article Ingress Ughhh
5 26.06.2007 Article 2 1 Article Body Nice
5 26.06.2007 Article 2 1 Article Info Arrrg

As you see it return 4 rows per article. This is because the Article template has 4 templateDefinitions. Another template can have more or less templatedefinitions. Is it possible to make a query based on this View that return 1 record for each article? Then each AttributeName shall be a column name, and the attributeValue shall be the value for this Column.

This is really hard to make, ugh!!!

www.WebRessurs.no
   

- Advertisement -