SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sanjay5219
Posting Yak Master

228 Posts

Posted - 03/27/2013 :  11:26:17  Show Profile  Reply with Quote
Hi All,
I have two table, I have to update Column Marks In Table1 from Table2 based on marks and unioue column in Roll

Please suggest

Table1
Roll Name Marks
1 A 34,65,89
2 B 80,76
3 C

Table 2
Roll Subject Marks
1 Hindi 34
1 English 65
1 French 89
2 French 80
2 Maths 76

James K
Flowing Fount of Yak Knowledge

3651 Posts

Posted - 03/27/2013 :  11:40:14  Show Profile  Reply with Quote
select
	t1.roll,
	t1.Name,
	stuff(( 
		select ',' as [text()],t2.marks as [text()] 
		from Table2 t2 
		where t1.roll = t2.roll 
		for XML path('')),1,1,''
		) as marks
from
	Table1 t1;


If you want to update Table1, then this (assuming marks is a character type column in Table1)
update t1 set marks = 
	stuff(( 
		select ',' as [text()],t2.marks as [text()] 
		from Table2 t2 
		where t1.roll = t2.roll 
		for XML path('')),1,1,''
		) 
from
	Table1 t1;

Edited by - James K on 03/27/2013 11:42:10
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000