多表关联与表值函数
习惯性用表值函数来替代视图。更多的表值自定义函数,可参考:
http://msdn.microsoft.com/zh-cn/library/ms191165.aspx
下面写一个简单的例子,如多表关联,就可以用表值函数来实现,可以解决在数据显示时,需要显示引用表的另外一些字段。
代码
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[udf_Project]
(
)
RETURNS TABLE
AS
RETURN
(
SELECT [ProjectId],s.[SeriesId],[SeriesName],[ProjectName],p.[Description],[IsActive]
FROM [Project] p
LEFT JOIN [Series] s ON (p.[SeriesId] = s.[SeriesId])
)
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[udf_Project]
(
)
RETURNS TABLE
AS
RETURN
(
SELECT [ProjectId],s.[SeriesId],[SeriesName],[ProjectName],p.[Description],[IsActive]
FROM [Project] p
LEFT JOIN [Series] s ON (p.[SeriesId] = s.[SeriesId])
)