使用与全局临时表的合并

时间:2012-02-09 16:28:06

标签: sql sql-server-2008 stored-procedures merge

我遇到了存储过程的问题。我正在尝试将表var中的数据合并到全局临时表中。一旦我在合并后放入一个表名,我就会收到错误。这是我第一次尝试使用它。如果我注释掉合并,一切正常。

有谁知道问题出在哪里?

USE [MONDAT]
GO
/****** Object:  StoredProcedure [dbo].[Pickrate]    Script Date: 02/08/2012 16:12:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Pickrate]
-- Add the parameters for the stored procedure here
@ReportDate Date --= getdate
--<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS 
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

-- Create memory tables filled with the data we require to fille the temp table
DECLARE @StartDate DATE
DECLARE @EndDate DATE
DECLARE @TrueReportDate DATE
DECLARE @EmployeeRecords TABLE(EmployeeNumber CHAR(6),  EmployeeName CHAR(30), OriginalHireDate DATE, Deptcode CHAR(6), DeptDesc CHAR(30), TeamNo CHAR(2), PayDate DATE, Hours NUMERIC)
DECLARE @PickSummaries TABLE(EmployeeNumber CHAR(6), PayDate DATE, Lbs NUMERIC, PiecePay NUMERIC, Grade03Lbs NUMERIC, Grade02Lbs NUMERIC, Grade01Lbs NUMERIC)

--drop temp table if it exists
IF EXISTS (
    SELECT *
    FROM tempdb..sysobjects
    WHERE name LIKE '##HoursByDay%')
    BEGIN
        DROP TABLE ##HoursByDay
    END

--create temp table
Create table ##HoursByDay (ID INT IDENTITY(1,1),
                           EmployeeNumber CHAR(6),
                           EmployeeName CHAR(30),
                           OriginalHireDate DATE,
                           DeptCode CHAR(6),
                           DeptDesc CHAR(30),
                           TeamNo CHAR(2),
                           PayDate DATE,
                           DailyPayHours NUMERIC,
                           DailyLbs NUMERIC,
                           DailyPiecePay NUMERIC,
                           DailyLbsPerHour NUMERIC,
                           DailyGrade01Lbs NUMERIC,
                           DailyGrade02Lbs NUMERIC,
                           DailyGrade03Lbs NUMERIC,
                           DailyGrade01Percent NUMERIC,
                           DailyGrade02Percent NUMERIC,
                           DailyGrade03Percent NUMERIC,
                           WeeklyPayHours NUMERIC,
                           WeeklyLbs NUMERIC,
                           WeeklyPiecePay NUMERIC,
                           WeeklyLbsPerHour NUMERIC,
                           WeeklyGrade01Lbs NUMERIC,
                           WeeklyGrade02Lbs NUMERIC,
                           WeeklyGrade03Lbs NUMERIC,
                           WeeklyGrade01Percent NUMERIC,
                           WeeklyGrade02Percent NUMERIC,
                           WeeklyGrade03Percent NUMERIC,
                           WeeklyRank NUMERIC)

-- determine Period Start, Period End and True Report dates
IF @ReportDate = CAST(GetDate() AS DATE)
BEGIN
    SET @TrueReportDate = DATEADD(d, -1, GetDate())
END
ELSE
BEGIN
    SET @TrueReportDate = @ReportDate
END
SET @StartDate = DATEADD(day, - 1 - (DATEPART(dw, @TrueReportDate) + @@DATEFIRST - 1) % 7, @TrueReportDate)                            
SET @EndDate = DATEADD(day, 6 -(DATEPART(dw, @TrueReportDate) + @@DATEFIRST) % 7, @TrueReportDate)


-- fill the memory tables with data
-- Gather data from the time and attendance system
INSERT INTO @EmployeeRecords (EmployeeNumber, EmployeeName, OriginalHireDate, Deptcode, DeptDesc, TeamNo, PayDate, Hours)
SELECT     EZTrack.dbo.hEmployee.EmployeeNumber, MAX(RTRIM(EZTrack.dbo.hEmployee.FirstName) + ' ' + RTRIM(EZTrack.dbo.hEmployee.LastName)) AS Name, MAX(EZTrack.dbo.hEmployee.OriginalHireDate) 
                  AS OriginalHireDate, MAX(EZTrack.dbo.sOrganization.OrgCode) AS Deptcode, MAX(EZTrack.dbo.sOrganization.OrgDesc) AS DeptDesc, MAX(EZTrack.dbo.sOrganization.OrgCode) AS TeamNo, 
                  EZTrack.dbo.tPunch.Chargedate AS PayDate, SUM(CAST(DATEDIFF(mi, EZTrack.dbo.tPunch.ID, EZTrack.dbo.tPunch.OD) AS numeric) / 60) AS Hours
FROM       EZTrack.dbo.tPunch INNER JOIN
                  EZTrack.dbo.hEmployee ON EZTrack.dbo.tPunch.EmployeeID = EZTrack.dbo.hEmployee.EmployeeID INNER JOIN
                  EZTrack.dbo.hEmployeeOrgs AS hEmployeeOrgs_1 ON EZTrack.dbo.hEmployee.EmployeeID = hEmployeeOrgs_1.EmployeeID INNER JOIN
                  EZTrack.dbo.sOrganization AS sOrganization_1 ON hEmployeeOrgs_1.Orglevel2ID = sOrganization_1.OrganizationID LEFT OUTER JOIN
                  EZTrack.dbo.sOrganization INNER JOIN
                  EZTrack.dbo.hEmployeeOrgs ON EZTrack.dbo.sOrganization.OrganizationID = EZTrack.dbo.hEmployeeOrgs.Orglevel6ID ON 
                  EZTrack.dbo.hEmployee.EmployeeID = EZTrack.dbo.hEmployeeOrgs.EmployeeID
WHERE     (EZTrack.dbo.hEmployee.Status = 1 and EZTrack.dbo.tPunch.Chargedate >= @StartDate and EZTrack.dbo.tPunch.Chargedate <= @EndDate)
GROUP BY EZTrack.dbo.hEmployee.EmployeeNumber, EZTrack.dbo.tPunch.Chargedate

-- Gather picking data from the harvesting database
INSERT INTO @PickSummaries (EmployeeNumber, PayDate, Lbs, PiecePay, Grade03Lbs, Grade02Lbs, Grade01Lbs)
SELECT      EmployeeID, BackDate AS PayDate, SUM(BoxCapacity) AS Lbs, SUM(Payrate) AS PiecePay, SUM(CASE WHEN Grade = '03' THEN BoxCapacity ELSE 0 END) 
                  AS Grade03Lbs, SUM(CASE WHEN Grade = '02' THEN BoxCapacity ELSE 0 END) AS Grade02Lbs, SUM(CASE WHEN Grade = '01' THEN BoxCapacity ELSE 0 END) 
                  AS Grade01Lbs
FROM        STmush_30_Main.dbo._vTransData
WHERE       BackDate >= @StartDate and BackDate <= @EndDate
GROUP BY EmployeeID, BackDate

----Place the harvester data into the global temp table
INSERT INTO ##HoursByDay (EmployeeNumber,  EmployeeName, OriginalHireDate, DeptCode, DeptDesc, TeamNo, PayDate)
SELECT DISTINCT EmployeeNumber,  EmployeeName, OriginalHireDate, DeptCode, DeptDesc, TeamNo, @TrueReportDate
FROM @EmployeeRecords

---- Merge the values from the hours query to the temp table
--MERGE ##HoursByDay AS [target]
--USING 
--  (SELECT C.EmployeeNumber, SUM(CAST(DATEDIFF(mi, EZTrack.dbo.tPunch.ID, EZTrack.dbo.tPunch.OD) AS numeric) / 60) AS DailyPayHours
--   FROM EZTrack.dbo.tPunch B INNER JOIN EZTrack.dbo.hEmployee C ON EZTrack.dbo.tPunch.EmployeeID = EZTrack.dbo.hEmployee.EmployeeID
--   WHERE B.Chargedate = @TrueReportDate) as [source]
--ON
--  ([target].EmployeeNumber = [source].EmployeeNumber)
--WHEN MATCHED
--  THEN
--      UPDATE SET
--      [target].DailyPayHours = [source].DailyPayHours


RETURN 0                      

END

当我执行SP时,我收到错误“消息102,级别15,状态1,过程选择,第109行 “## HoursByDay”附近的语法不正确。“

2 个答案:

答案 0 :(得分:1)

虽然the documentation似乎没有明确提及#temp或##全局临时表,但我从未见过使用它们的MERGE示例。也许是因为MERGE ##HoursByDay应该是MERGE INTO ##HoursByDay?也许是因为之前的陈述没有用分号正确终止?不是我现在可以测试的地方,但我会尝试那些。

无论如何,你为什么不使用永久表?除了不必在最后删除它之外,它实现了与##全局临时表相同的并发和隔离。我不明白全球临时表在这里买了什么。

答案 1 :(得分:1)

检查数据库的兼容级别。

我可以通过将其设置为2000或2005来重现此错误。在2008年,一旦删除对基表的所有引用,它就可以正常工作(尽管MERGE语句需要以半冒号终止。)< / p>