Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Is it somehow possible to convert rows into a column in a single query?tblObject:ID Name1 Object 12 Object 23 Object 3tblValue:ObjectID Name1 Value 11 Value 21 Value 32 Value 13 Value 1I need an output like:ID Name Values1 Object 1 Value1, Value 2, Value 32 Object 2 Value13 Object 3 Value1I could do some cursor-tricks but is it possible to do this in a single query?
spirit1
Cybernetic Yak Master
11752 Posts
Posted - 2004-11-17 : 07:36:25
well u can write a function which return a CSV string and call that...
create function dbo.ToCSV(@ObjectId int)returns varchar(1000)asbegin Declare @CSVList varchar(1000) SELECT @CSVList = COALESCE(@ColumnList + ', ', '') + Name FROM tblValue WHERE ObjectId = @ObjectId return @CSVListend select *, dbo.ToCsv(ObjectId) from tblObject
Go with the flow & have fun! Else fight the flow
Mickey13
Starting Member
4 Posts
Posted - 2004-11-17 : 07:57:27
That is clever. I didn't know the string-assignment would work 'setbased'. Thanks.