I have a question on regarding the following post, I would add to it but it won't let me since I am new.
Edited to add the specific type of tables and better information
I have two tables below and would like the accomplish in getting tblnames of studentname into tblCombineNames into Student names.
Please advise, thanks!
TblNames
ID(PK) StudentType(FK) StudentNo(FK) GradeNo(FK) StudentName
---------- ---------- ---------- ---------- -------------
1 1 1 1 Mary
2 1 1 1 John
3 1 1 1 Sam
4 2 2 2 Alaina
5 2 2 2 Edward
6 2 2 2 Joe
I would like the output to be below
TblCombineNames
ID(PK) StudentType(PK) StudentNo(PK) GradeNo(PK) StudentNames
---------- ---------- ---------- ---------- -------------
1 1 1 1 Mary, John, Sam
2 2 2 2 Alaina, Edward, Joe
I would have a scalar-valued Function named something like
---dbo.fn_Concatenate_Names
ALTER FUNCTION [dbo].[fn_Concatenate_Names]
(
@StudentType VARCHAR(250),
@StudentNo VARCHAR(250),
@GradeNo VARCHAR(250)
)
RETURNS Varchar(250)
BEGIN
Declare @rtn Varchar(250)
BEGIN
Select @rtn=(
Select StudentNames + ', ' as 'data()'
from tblStudentnames
where studentType = @StudentType and StudentNo = @StudentNo and GradeNo = @GradeNo
for XML path('')
)
Set @rtn = LEFT(@rtn, Len(@rtn) - 1)
END
RETURN (@rtn)
END
I would do on update like to call the function
update tblCombineNames
set studentnames = fn_concatenate_names(StudentType,StudentNo,GradeNo)
It seems like it would work but it takes 2 hours to run on tblStudentNames of 250730 records. I don't think it should take that long.