CREATE FUNCTION Inffkfrom(@tbID INT,
@colID SMALLINT)
returns VARCHAR(2000)
AS
BEGIN
DECLARE @r VARCHAR(2000),
@a VARCHAR(200)
SELECT @r = '',
@a = ''
DECLARE cs CURSOR FOR
SELECT FKfrom=CONVERT(VARCHAR(200), Object_name(rkeyid)+'.'+r.[name])
FROM sysforeignkeys c
JOIN syscolumns f
ON c.fkeyid = f.[id]
AND c.fkey = f.colID
JOIN syscolumns r
ON c.rkeyid = r.[id]
AND c.rkey = r.colID
WHERE fkeyID = @tbID
AND fkey = @colID
ORDER BY
keyNo
OPEN cs
FETCH NEXT FROM cs INTO @a
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @r = @r + CASE
WHEN Len(@r) > 0 THEN ', '
ELSE ''
END + @a
FETCH NEXT FROM cs INTO @a
END
CLOSE cs
DEALLOCATE cs
RETURN( @r )
END
GO