對於關於以下文章,我有一個問題,我會添加到它,但它不會讓我,因爲我是新的。將函數值合併爲一個字符串的sql函數
編輯添加的特定類型的表,更好的信息
我有下面兩個表,並希望在獲得studentname的tblnames到tblCombineNames到學生名字的完成。
請指教,謝謝!
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
我想輸出要低於
TblCombineNames
ID(PK) StudentType(PK) StudentNo(PK) GradeNo(PK) StudentNames
---------- ---------- ---------- ---------- -------------
1 1 1 1 Mary, John, Sam
2 2 2 2 Alaina, Edward, Joe
我想有一個標量值函數名字類似
---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
我會做更新想調用的函數
update tblCombineNames
set studentnames = fn_concatenate_names(StudentType,StudentNo,GradeNo)
看起來這會工作,但它需要2個小時到的250730條記錄tblStudentNames運行。我不認爲這會持續很長時間。
? Postgres的?甲骨文? –
對不起,我是SQL 2008 – eripey
[這裏的帖子越來越接近了,我需要弄清楚如何在函數中使用它。點擊查看我在說的帖子。](http://stackoverflow.com/questions/11890590/if-countvalues-1-combine-all-values-into-a-single-cell) – eripey