我想使用以下语句
创建内联表值函数 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
)
但它给了我以下错误:关键字声明附近的语法不正确。
问题:是否可以在内联表值函数中执行我想要做的事情(声明变量并使用函数参数从查询中获取结果)然后将此值传递给将要计算的主查询结果)?如果有,怎么样?如果没有什么是多语句表值选择函数或其他什么。我很欣赏建议和其他想法
答案 0 :(得分:1)
不,您不能在内联表值函数中声明变量。
虽然您可以将2个查询合并为1 - 看起来您应该只能将该初始选择组合到当前使用该变量的子查询中。