Click here to Skip to main content
15,908,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys i need your help, i am trying to create a stored procedure, but i dont know why MSQl Management Studio is complaining about this query.

Error Msg:Msg 102, Level 15, State 1, Procedure Consolidate_Time_From_THQ, Line 1
Incorrect syntax near 'Consolidate_Time_From_THQ'.

SQL
CREATE Procedure Consolidate_Time_From_THQ



DECLARE @start DATE,  @end DATE
DECLARE @tbl TABLE 
([userid] uniqueidentifier
,[userObjname] nvarchar(255)
,[chargedToid] uniqueidentifier
,[chargedToObjtype] tinyint
,[chargeToObjname] nvarchar(255)
,[chargedToProjectid] uniqueidentifier
,[totalBillableSeconds] int
,[totalNonBillableSeconds] int
,[totalSeconds] int
,[effectiveDate] datetime)
INSERT @tbl


SELECT 
dbo.timesheet.userid, 
dbo.THQ_Objects.objname AS userObjname, 
dbo.timesheetDetail.chargedToid, 
THQ_Objects_1.objtype AS chargedToObjtype, 
THQ_Objects_1.objname AS chargeToObjname, 
CASE THQ_Objects_1.objtype 
WHEN 1 THEN dbo.timesheetDetail.chargedToid 
WHEN 2 THEN
(SELECT projectid
FROM dbo.task
WHERE objid = dbo.timesheetDetail.chargedToid) 
ELSE NULL 
END AS chargedToProjectid, 
SUM(dbo.timesheetDetail.totalBillableSeconds) AS totalBillableSeconds, 
SUM(dbo.timesheetDetail.totalNonBillableSeconds) AS totalNonBillableSeconds, 
SUM(dbo.timesheetDetail.totalSeconds) 
AS totalSeconds, MIN(dbo.timesheetDetail.effectiveDate) AS effectiveDate
FROM dbo.timesheetDetail INNER JOIN
dbo.timesheet ON dbo.timesheetDetail.sheetid = dbo.timesheet.objid INNER JOIN
dbo.THQ_Objects ON dbo.timesheet.userid = dbo.THQ_Objects.objid INNER JOIN
dbo.THQ_Objects AS THQ_Objects_1 ON dbo.timesheetDetail.chargedToid = THQ_Objects_1.objid
WHERE (dbo.THQ_Objects.deleted = 0) AND (THQ_Objects_1.deleted = 0)

GROUP BY dbo.timesheetDetail.chargedToid, dbo.timesheet.userid, dbo.THQ_Objects.objname, THQ_Objects_1.objname, THQ_Objects_1.objtype
order by effectiveDate desc

SELECT UserObjname as 'Resource', EffectiveDate, (SELECT objname FROM thq_Objects o WHERE o.objid = result.chargedToProjectid) as 'ProjectName', (SELECT objname FROM thq_Objects o WHERE o.objid = result.chargedToid) as 'Project/Task Name', TotalBillableSeconds, TotalNonBillableSeconds, TotalSeconds
FROM @tbl result
WHERE effectiveDate BETWEEN @start AND @end
Order By UserObjname, EffectiveDate
revert


Thanks for the help.
Posted

1 solution

If you have SMS just right-click Stored Procedures and choose New. You'll get the template like this:

SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<author,,name>
-- Create date: <create>
-- Description:	<description,,>
-- =============================================
CREATE PROCEDURE <procedure_name,> 
	-- Add the parameters for the stored procedure here
	<@Param1, sysname, @p1> <datatype_for_param1,> = <default_value_for_param1,>, 
	<@Param2, sysname, @p2> <datatype_for_param2,> = <default_value_for_param2,>
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO</create>


For starters, you are missing the AS keyword.
 
Share this answer
 
Comments
rudolph098 17-Oct-13 13:34pm    
i am a confused, i have never used this to create a stored procedure, so this is new to me. Can you pls offer some assistance.
ZurdoDev 17-Oct-13 14:27pm    
Do you have Sql Management Studio?
rudolph098 17-Oct-13 15:43pm    
Reply thank you, it worked
ZurdoDev 17-Oct-13 15:56pm    
Good to hear.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900