SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Create a view for Merged records


Create a view for Merged records

Author
Message
skb 44459
skb 44459
SSC Veteran
SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)

Group: General Forum Members
Points: 297 Visits: 297
Table
Rowointer Notes
------- ---------
Row0001 TestNotes1
Row0002 TestNotes2
Row0003 TestNotes3

Would like to create a view based on above table which which will merge Notes field based on RowPointer.
it should put a return character between each notes.

View record will look like this
ViewRP ViewNotes
-------- ------------
Row0001 TestNotes1
TestNotes2
TestNotes3
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26245 Visits: 17552
A rather strange request and a bit limited I suspect but this should do it. Notice how I posted readily consumable ddl and data? This is something you should do in the future.


if OBJECT_ID('tempdb..#Something') is not null
drop table #Something

create table #Something
(
RowPointer char(7),
Notes char(10)
)

insert #Something
select 'Row0001', 'TestNotes1' union all
select 'Row0002', 'TestNotes2' union all
select 'Row0003', 'TestNotes3'

select top 1 RowPointer, STUFF((select Notes + char(10)
from #Something
order by RowPointer
for XML PATH('')), 1, 0, '')
from #Something



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
skb 44459
skb 44459
SSC Veteran
SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)

Group: General Forum Members
Points: 297 Visits: 297
Thanks your code works . But my notes field is type ntext when i run the code i get

The data types ntext and char are incompatible in the add operator.

if OBJECT_ID('tempdb..#Something') is not null
drop table #Something

create table #Something
(
RowPointer char(7),
Notes NTEXT
)

insert #Something
select 'Row0001', 'TestNotes1' union all
select 'Row0002', 'TestNotes2' union all
select 'Row0003', 'TestNotes3'

select top 1 RowPointer, STUFF((select Notes + char(10)
from #Something
order by RowPointer
for XML PATH('')), 1, 0, '')
from #Something


The data types ntext and char are incompatible in the add operator.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26245 Visits: 17552
Don't use the TEXT datatype. It is deprecated and the performance is horrible. It is also incredibly painful to work with. The replacement is (n)varchar(max). It would be FAR better to change your table. You should be able to change it in place with no adverse affects on anything. If you are unable you will have to cast your column in your query.


select top 1 RowPointer, STUFF((select cast(Notes as nvarchar(max)) + char(10)
from #Something
order by RowPointer
for XML PATH('')), 1, 0, '')
from #Something



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26245 Visits: 17552
Also, I should be ashamed of myself for not putting an ORDER BY clause on my query. It is imperative to use an order by when the order of the rows matters. For this small sample it will never be an issue, but in your actual table it does make a difference.


select top 1 RowPointer, STUFF((select cast(Notes as nvarchar(max)) + char(10)
from #Something
order by RowPointer
for XML PATH('')), 1, 0, '')
from #Something
order by RowPointer



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search