私は、別のテーブルのデータをテーブルに取り込むストアドプロシージャを持っています。 更新トリガーを使用してSP 3を実行すると、レコードがテーブルに挿入されます。ストアドプロシージャは、トリガーとマニュアルを実行すると異なる結果を返します。
私がSPを手動で実行すると、私はテーブルに6レコードを取得します。
'SET ANSI_NULLS ON',
'SET QUOTED_IDENTIFIER ON',
'SET NOCOUNT ON'
は、SPとトリガーで同じです。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[Residency_Date_Summary_Populate]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @StartDate datetime;
SET @StartDate = DateAdd(year, DateDiff(year, 0, GetDate()), 0);
WITH Tally (N) AS
(SELECT TOP (12) ROW_NUMBER() OVER (ORDER BY (SELECT 0))
FROM master.sys.objects), CTE1 AS
(SELECT c.First_Name,
c.Last_Name,
c.ID,
r.Building,
r.Move_In_Date,
r.Move_Out_Date,
StartOfMonth = DateAdd(month, t1.N-1, @StartDate),
EndOfMonth = DateAdd(day, -1, DateAdd(month, t1.N, @StartDate)),
MonthNbr = t1.N
FROM CONTACTS c
JOIN Residency_Dates r
ON c.ID = r.ID_U
JOIN Tally t1
ON t1.N between month(r.move_in_date) and month(coalesce(r.move_out_date, getdate ()))), CTE2 AS
(SELECT First_Name,
Last_Name,
Building,
MonthNbr,
ID
,StayForMonth = CASE WHEN Move_In_Date > StartOfMonth AND Move_out_Date <= EndOfMonth
THEN DateDiff(day, Move_In_Date, Move_Out_Date)
WHEN Move_In_Date > StartOfMonth
THEN DateDiff(day, Move_In_Date, EndOfMonth)
WHEN Move_out_Date > EndOfMonth
THEN DateDiff(day, StartOfMonth, EndOfMonth)
WHEN Move_out_Date IS NULL AND month(StartOfMonth) = month(GetDate())
THEN DateDiff(day, StartOfMonth, GetDate())
ELSE DateDiff(day, StartOfMonth, COALESCE(Move_Out_Date, EndOfMonth))
END
FROM CTE1)
INSERT into Residency_Date_Summary
SELECT First_Name,
Last_Name,
Building,
January = MAX(CASE WHEN MonthNbr = 1 THEN StayForMonth ELSE 0 END),
February = MAX(CASE WHEN MonthNbr = 2 THEN StayForMonth ELSE 0 END),
March = MAX(CASE WHEN MonthNbr = 3 THEN StayForMonth ELSE 0 END),
April = MAX(CASE WHEN MonthNbr = 4 THEN StayForMonth ELSE 0 END),
May = MAX(CASE WHEN MonthNbr = 5 THEN StayForMonth ELSE 0 END),
June = MAX(CASE WHEN MonthNbr = 6 THEN StayForMonth ELSE 0 END),
July = MAX(CASE WHEN MonthNbr = 7 THEN StayForMonth ELSE 0 END),
August = MAX(CASE WHEN MonthNbr = 8 THEN StayForMonth ELSE 0 END),
September = MAX(CASE WHEN MonthNbr = 9 THEN StayForMonth ELSE 0 END),
October = MAX(CASE WHEN MonthNbr = 10 THEN StayForMonth ELSE 0 END),
November = MAX(CASE WHEN MonthNbr = 11 THEN StayForMonth ELSE 0 END),
December = MAX(CASE WHEN MonthNbr = 12 THEN StayForMonth ELSE 0 END)
FROM CTE2
GROUP BY First_Name, Last_Name, Building
ORDER BY Last_Name, First_Name, Building;
End
で
を交換するのですか?それは 'sys.objects'に対するパーミッションを持っていますか? –