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)
 Need Help Creating a View

Author  Topic 

jtrirogoff
Starting Member

4 Posts

Posted - 2007-03-28 : 18:37:43
Hey Guys,

I need some help creating a particular view. I need this view to concatenate each "Column_Name" with its corresponding "Column_Value" for every row and display it as a "Details" field. I realize that I can Hard code in each "Column_Name", but this is bad design... I need it to be flexible. Ex.

SELECT 'Table_1' AS TableName, [Column_Name1: Column_Value1, ...] AS Details
FROM dbo.Table_1

I'm thinking something with WHILE LOOPS but I can't wrap my head around this one. Any Help towards this matter would be greatly appreaciated, Thanks!

- Jonny

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-03-28 : 18:40:30
Talk about bad design! Your view should not be doing this. Return just the data to your application, then have your application do the formatting.

What is your real business requirement?

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

jtrirogoff
Starting Member

4 Posts

Posted - 2007-03-28 : 20:57:33
I didn't say that it was going to be pretty, but unfortunately given certain circumstances I only have a View to work with. I'll give more detail on my situation:

This View is ultimately going to be a summary of several Log Tables in our database. Each Log Table has different Column Names, that's why I must concatenate all the Column Names and corresponding Column Values under a single field "Details", because I'm going to be performing a UNION on all our Log Tables. What this View does (through our application) is show the User the Details of all their different Logs under one view and from here the User can print it.

Here is a simplified version of what I'm trying to do (Note: The tables that I'm using have many more columns than this example):

Log_1:

ColA | ColB | ColC
---- + ---- + ----
1 | 2 | 3
...

Log_2:

ColD | ColE | ColF
---- + ---- + ----
4 | 5 | 6
...

Log_3:

ColG | ColH | ColI
---- + ---- + ----
7 | 8 | 9
...

Resulting View:

LogDetails_View

LogName | Details
-------- + --------------------------------
Log_1 | ColA: 1, ColB: 2, ColC: 3
... |
Log_2 | ColD: 4, ColE: 5, ColF: 6
... |
Log_3 | ColG: 7, ColH: 8, ColI: 9
... |

Any ideas? It doesn't seem to bad but I just can't wrap my head around it. I've created a function that returns a string of all the
column names concatenated together (ColA, ColB, ColC) for a particular table, but I'm not quite sure how I'm going to use this (if at all useful) in my view. Once again, any help towards this matter would be greatly appreaciated, Thanks!

- Jonny
Go to Top of Page
   

- Advertisement -