创建内联表值函数sql-server 2008

发布于 2024-11-01 10:37:09 字数 2205 浏览 5 评论 0原文

我想使用以下语句创建内联表值函数

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2024-11-08 10:37:09

不,您不能在内联表值函数中声明变量。

虽然您可以将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文