创建内联表值函数sql-server 2008
我想使用以下语句创建内联表值函数
USE [abc]
GO
/****** Object: UserDefinedFunction [dbo].[fnspEmpPeriodSkus] Script Date: 04/14/2011 16:26:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fnspEmpPeriodSkus]
(
-- Add the parameters for the function here
@PositionID int
)
RETURNS TABLE
AS
RETURN
(
declare @PeriodTypeID int
select @PeriodTypeID = cofig.PeriodTypeID from sysMarketSaleConfigurations config inner join setupJobs j on j.JobID = config.JobID
inner join setupDesignations d on d.JobID = j.JobID where d.DesignationID = @PositionID
SELECT EmployeeID,EmployeeName, ZoneID,Zone,RegionID,Region,TerritoryID,Territory, StartDate, EndDate, empperiods.PeriodID, empperiods.DesignationID, EmpSkus.SKUID, EmpSkus.SKUCode, EmpSkus.Title, EmpSkus.GroupName,
EmpSkus.ProductGroupID, EmpSkus.PositionID,EmpSkus.PositionTitle
FROM (SELECT StartDate, EndDate, PeriodID, DesignationID
FROM (SELECT sysPeriods.PeriodID, sysPeriods.StartDate, sysPeriods.EndDate, setupDesignations.DesignationID
FROM setupJobs INNER JOIN
setupDesignations ON setupJobs.JobID = setupDesignations.JobID CROSS JOIN
sysPeriods
WHERE Year(sysPeriods.StartDate) = Year(Current_timestamp) and (sysPeriods.PeriodTypeID = @PeriodTypeID)) AS drvtbl) AS empperiods LEFT OUTER JOIN
dbo.vwEmpSkus AS EmpSkus ON
empperiods.DesignationID = EmpSkus.PositionID
)
,但它给了我以下错误:关键字声明附近的语法不正确。
问题:是否可以在内联表值函数中执行我想要执行的操作(声明一个变量并使用函数参数从查询中获取其结果),然后将此值传递给将计算的主查询结果)?如果是的话怎么办?如果没有,什么是选择多语句表值函数或其他东西。我很欣赏建议和其他想法
i want to create an inline table valued function using following statement
USE [abc]
GO
/****** Object: UserDefinedFunction [dbo].[fnspEmpPeriodSkus] Script Date: 04/14/2011 16:26:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fnspEmpPeriodSkus]
(
-- Add the parameters for the function here
@PositionID int
)
RETURNS TABLE
AS
RETURN
(
declare @PeriodTypeID int
select @PeriodTypeID = cofig.PeriodTypeID from sysMarketSaleConfigurations config inner join setupJobs j on j.JobID = config.JobID
inner join setupDesignations d on d.JobID = j.JobID where d.DesignationID = @PositionID
SELECT EmployeeID,EmployeeName, ZoneID,Zone,RegionID,Region,TerritoryID,Territory, StartDate, EndDate, empperiods.PeriodID, empperiods.DesignationID, EmpSkus.SKUID, EmpSkus.SKUCode, EmpSkus.Title, EmpSkus.GroupName,
EmpSkus.ProductGroupID, EmpSkus.PositionID,EmpSkus.PositionTitle
FROM (SELECT StartDate, EndDate, PeriodID, DesignationID
FROM (SELECT sysPeriods.PeriodID, sysPeriods.StartDate, sysPeriods.EndDate, setupDesignations.DesignationID
FROM setupJobs INNER JOIN
setupDesignations ON setupJobs.JobID = setupDesignations.JobID CROSS JOIN
sysPeriods
WHERE Year(sysPeriods.StartDate) = Year(Current_timestamp) and (sysPeriods.PeriodTypeID = @PeriodTypeID)) AS drvtbl) AS empperiods LEFT OUTER JOIN
dbo.vwEmpSkus AS EmpSkus ON
empperiods.DesignationID = EmpSkus.PositionID
)
but it gives me following error: incorrect syntax near keyword declare.
Question: is it possible to do in inline table valued function what i want to do ( declare a variable and get its result from query using parameter of function) then passing this value to main query that will calculate the result)? if yes how? if no what are choices multi statement table valued function or something else. i appreciate suggestions and other ideas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您不能在内联表值函数中声明变量。
虽然您可以将 2 个查询合并为 1 个 - 看起来您应该能够将初始选择合并到当前使用该变量的子查询中。
No, you can't declare variables within an inline table valued function.
Though you could just combine the 2 queries into 1 - looks like you should just be able to combine that initial select into the subquery where you currently use the variable.