Microsoftの一部のバージョンOLE SQL Server用DBプロバイダー(ほとんどの場合Windows XP上)はWITH
ステートメントをサポートしていないことを理解しました。そこで、SQLを移動することにしましたステートメントをテーブル値関数に追加し、アプリケーションから呼び出します。今、私は立ち往生しています。INSERT INTO
ステートメントとWITH
?ここに私がこれまでに持ってきたコードがありますが、SQL Serverはそれを好きではありません... :-(
CREATE FUNCTION GetDistributionTable
(
@IntID int,
@TestID int,
@DateFrom datetime,
@DateTo datetime
)
RETURNS
@Table_Var TABLE
(
[Count] int,
Result float
)
AS
BEGIN
INSERT INTO @Table_Var ([Count], Result) WITH T(Result)
AS (SELECT ROUND(Result - AVG(Result) OVER(), 1)
FROM RawResults WHERE IntID = @IntID AND DBTestID = @TestID AND Time >= @DateFrom AND Time <= @DateTo)
SELECT COUNT(*) AS [Count],
Result
FROM T
GROUP BY Result
RETURN
END
GO
CTE(WITH
ステートメント)を省略し、代わりにサブクエリを使用するインラインテーブル値関数を作成できます。
CREATE FUNCTION GetDistributionTable
(
@IntID int,
@TestID int,
@DateFrom datetime,
@DateTo datetime
)
RETURNS TABLE
AS
RETURN
(
SELECT COUNT(*) AS [Count],
Result
FROM (
SELECT ROUND(Result - AVG(Result) OVER(), 1) Result
FROM RawResults
WHERE IntID = @IntID
AND DBTestID = @TestID
AND Time >= @DateFrom
AND Time <= @DateTo
) t
GROUP BY
Result
)
GO
テーブル値関数のCTEの構文は次のとおりです。
CREATE FUNCTION GetDistributionTable
(
@IntID int,
@TestID int,
@DateFrom datetime,
@DateTo datetime
)
RETURNS TABLE
AS
RETURN
(
WITH cte AS
(
SELECT ROUND(Result - AVG(Result) OVER(), 1) Result
FROM RawResults
WHERE IntID = @IntID
AND DBTestID = @TestID
AND Time >= @DateFrom
AND Time <= @DateTo
)
SELECT COUNT(*) AS [Count],
Result
FROM cte
GROUP BY
Result
)
GO
あなたの例は、マルチステートメントTVFがより良い実行プランを選択する際にクエリオプティマイザを防ぐことができるため、インラインTVFを使用してみてください(パフォーマンスの違いは説明されていますここ)
このような..
CREATE FUNCTION GetDistributionTable
(
@IntID int,
@TestID int,
@DateFrom datetime,
@DateTo datetime
)
RETURNS
@Table_Var TABLE
(
[Count] int,
Result float
)
AS
BEGIN
WITH T
AS (
select Ticket_Id,COUNT(1) Result from
Customer_Survey
group by MemberID,SiteId,Ticket_Id
)
INSERT INTO @Table_Var ([Count], Result)
SELECT COUNT(*) AS [Count],
Result
FROM T
GROUP BY Result
RETURN
END
GO
CTE with if else in UDF
USE [SchoolDB]
GO
/****** Object: UserDefinedFunction [dbo].[GetDistributionTable] Script Date: 24-08-2019 05:17:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter FUNCTION [dbo].[GetDistributionTable]
(
@DepartmentName varchar(50) = 'Production'
)
RETURNS
@Table_Var TABLE
( NUM int IDENTITY(1,1),
[ParentEmployeeKey] int,
Result float
)
AS
BEGIN
Declare @Table_Vars table
(
[ParentEmployeeKey] int,
Result float
);
insert into @Table_Vars([ParentEmployeeKey])
select COUNT(1) Result from
[SchoolDB].[dbo].[DimEmployee] where DepartmentName = @DepartmentName
if(@@rowcount >0)
begin
WITH T
AS (
select [ParentEmployeeKey],COUNT(1) Result from
[SchoolDB].[dbo].[DimEmployee] where DepartmentName = @DepartmentName
group by [ParentEmployeeKey]
)
INSERT INTO @Table_Var ([ParentEmployeeKey], Result)
SELECT COUNT(*) AS [Count],
Result
FROM T
GROUP BY Result
end
else
WITH T
AS (
select [ParentEmployeeKey],COUNT(1) Result from
[SchoolDB].[dbo].[DimEmployee] where DepartmentName = @DepartmentName
group by [ParentEmployeeKey]
)
INSERT INTO @Table_Var ([ParentEmployeeKey], Result)
SELECT COUNT(*) AS [Count],
Result
FROM T
GROUP BY Result
RETURN
END
GO