Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:53:30+00:00 2026-05-29T11:53:30+00:00

I am having an issue with a stored procedure. I am trying to merge

  • 0

I am having an issue with a stored procedure. I am trying to merge data from a table var into a global temp table. I get an error as soon as I put a table name in after merge. This is my first time trying to use it. If I comment out the merge, everything works.

Does anyone recognize where the issue is?

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

When I execute the SP I get an error “Msg 102, Level 15, State 1, Procedure Pickrate, Line 109
Incorrect syntax near ‘##HoursByDay’.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:53:31+00:00Added an answer on May 29, 2026 at 11:53 am

    Check the compatibility level of your database.

    I can reproduce this error by setting it to 2000 or 2005. On 2008 once I remove all references to your base tables it works fine (though the MERGE statement needs to be terminated with a semi colon.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue trying to call a web service from stored procedure. sp_OACreate
I'm having an issue trying to set variable in SQL in a stored procedure.
I'm having a never-ending problem with trying to call a stored procedure from a
I'm having issues when trying to call a MySQL (5.0.77) stored procedure with parameters,
I'm having an issue selecting dates properly from Postgres - they are being stored
I am having difficulty executing a MS SQL Server stored procedure from Java/jsp. I
I am having issues calling an Oracle FUNCTION (not a Stored Procedure) from Java
I'm trying to run a stored procedure and having real issues debugging. I've tried
I am having a problem with an update stored procedure. The error is: UPDATE
Morning People, I'm having a little issue with COALESCE causing a stored procedure which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.