Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is the query which I am using


SQL
Create procedure [STGRPT].[PROC_DOH_CURRENT_CENSUS]
(
	@facility varchar(200),
	@unit varchar (max)
)

AS 

;WITH census as 
(
SELECT Distinct FCY.FCY_NM as [Facility]
,Cast(CS.CSS_TS as Date) as Date
,RTRIM(CASE WHEN Len(u.LVL_CD) > 1 Then u.LVL_CD
		    ELSE Substring(
			u.UNIT_ABR_CD, 
			CHARINDEX('-',u.UNIT_ABR_CD) + 1
			, 25
			) End) as [Unit]
, cs.CSS_TS_DIM_ID as [Hour], CS.TOT_PERM_BED_CNT + cs.TOT_ASGN_BED_CNT as CENSUS
FROM PTM_DMART.dbo.CSS_SMY CS WITH (NOLOCK)
JOIN PTM_DMART.dbo.FCY_LKP FCY WITH (NOLOCK) ON CS.SRC_FCY_ID = FCY.SRC_FCY_ID
JOIN PTM_DMART.dbo.UNIT_LKP U WITH (NOLOCK) ON CS.UNIT_LKP_ID = U.UNIT_LKP_ID
Where Cast(CS.CSS_TS as Date) = DateAdd(day,-1,Cast(GetDate() as Date))
and cs.CSS_TS_DIM_ID IN ('0000', '0800','1600')
)

,datapivot as 
(
SELECT Facility, [Date], Unit, [0000], [0800], [1600]
FROM Census
PIVOT
(
		
		Min(Census)
		FOR [Hour] 
		IN ([0000], [0800], [1600])
) AS PT
--Where Facility = 'LONG ISLAND JEWISH HOSPITAL'
)

SELECT Facility, [Date], Unit, IsNULL([0000],0) as [00:00], IsNull([0800],0) as [08:00], IsNull([1600],0) as [16:00]
FROM datapivot
Order by 1,3

GO


What I have tried:

I tried to execute by different ways but getting the same error.
Posted
Updated 6-Feb-23 5:11am
v2
Comments
Dave Kreskowiak 2-Feb-23 10:07am    
i cna't test this and I'm not SQL expert, but the first thing I would try is getting rid of the semi colon before "WITH census as".

1 solution

I don't get that error when I run this SQL to create this procedure - probably because this is the only SQL in my query window. You probably have some other code above this.

Try putting GO before the create procedure
SQL
GO
Create procedure [STGRPT].[PROC_DOH_CURRENT_CENSUS]
(
	@facility varchar(200),
	@unit varchar (max)
)
.
.
.
As to the semi-colon before the WITH - you only "need" that semi-colon if there is code precding the CTE definition that has not been terminated with a semi-colon - I took this as further evidence that you had been experimenting with code and then wrapped it into a Stored Procedure create
 
Share this answer
 
Comments
Maciej Los 7-Feb-23 11:40am    
5ed!

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