Hello, I hope you can help me as I'm new to using SQL. I'm trying to create a procedure, but when I test it in Query Analyzer, it doesn't recognize the variables that will be passed in as having been declared. Is there something wrong with my syntax? I've looked at several things and have added "Set nocount on" and "with recompile" thinking that might be the problem, but no luck. It isn't recognizing the @TherapistName or the @BeginDate, @EndDate, or @MM. Do these go out of scope or something? It also says the temp table exists in the db, but this code hasn't been run yet, so I don't know how it could.
Here's the code:
Here's the code:
Code:
CREATE PROCEDURE dbo.usp_ProgressNotes
@TherapistName VARCHAR(60),@ProgramOrCampus VARCHAR(60), @BeginDate DATETIME, @EndDate DATETIME, @ServiceCodeGroup VARCHAR(30)
WITH RECOMPILE
AS
BEGIN
DECLARE @Campus CHAR(3)
,@Len INT
,@MM VARCHAR(2)
,@DD VARCHAR(2)
,@YY VARCHAR(4)
,@Date VARCHAR(18)
/**Get rid of temp table if exists before running**/
IF object_id('tempdb..#TmpProgressNotes') IS NOT NULL
BEGIN
DROP TABLE #TmpProgressNotes
END
GO
SET NOCOUNT ON
/**Force 12AM on begin date***/
SET @MM = DATEPART(mm,@BeginDate)
SET @DD = DATEPART(dd,@BeginDate)
SET @YY = DATEPART(yy,@BeginDate)
SET @Date = @MM + '/' + @DD + '/' + @YY + ' 00:00:00'
SET @BeginDate = @Date
/**Force midnight on End date***/
SET @MM = DATEPART(mm,@EndDate)
SET @DD = DATEPART(dd,@EndDate)
SET @YY = DATEPART(yy,@EndDate)
SET @Date = @MM + '/' + @DD + '/' + @YY + ' 23:59:59'
SET @EndDate = @Date
/**PROGRESS NOTE***/
SELECT
v.CampusInNote AS Campus
,'PNGen' AS NoteType
,v.DocID
,v.TFSclientnumber AS TFSClientNumber
,v.TFSProgramInNote
,v.CPTCd
,v.ServiceDt AS ServiceDt
,DatePart(ww,v.ServiceDt) AS ServiceWeek
,'FundingSource' = ISNULL(v.FundingSource,'Not Entered In Progress Note')
,v.ProblemDesc AS ProblemDesc
,v.Detail
,v.ServiceCd AS ServiceCd
,v.ServiceUnits AS ServiceUnits
,v.ServiceMinutes AS ServiceMinutes
,v.ServiceTm AS ServiceTm
,v.ServiceProvider
,v.ServiceDesc
,'Held' AS SessionStatus
,v.DocLastSavedNm AS TherapistOrProvider
,FS.NameCalcInit
,FS.MedicaidNumber AS MedicaidNbr
,FS.ClientDOB AS DOB
,v.DocCreatedDt
,v.DocStatusCd
,v.DocSignedNm
,v.DocCreatorNm
,v.DocLastSavedDt
,v.MHODesc
,v.RateType
,v.MinutesRequired
,v.BillingUnitRate
,v.SupervisorUnitRate
,v.ServiceCodeGrpDesc
,v.ServiceLocation
,v.ServiceLocationBillingCd
,NULL AS PhysComments
,NULL AS InfectionControl
,v.NonServiceMinutes
INTO #TmpProgressNotes
FROM v_ProgressNotesGeneral v
INNER JOIN FD__TFS_FACE_SHEET FS ON v.TFSClientNumber = FS.TFSClientNumber
WHERE ServiceDt > = @BeginDate AND ServiceDt < = @EndDate
UNION
/**PROGRESS NOTE HEALTH SERVICES***/
Select
v.CampusInNote AS Campus
,'PNHS'
,v.DocID
,v.TFSclientnumber
,v.TFSProgramInNote
,v.CPTCd
,v.ServiceDt
,DatePart(ww,v.ServiceDt) AS ServiceWeek
,'FundingSource' = ISNULL(v.FundingSource,''Not Entered In Progress Note'')
,NULL
,v.Detail
,v.ServiceCd
,v.ServiceUnits
,v.ServiceMinutes
,NULL
,v.ServiceProvider
,v.ServiceDesc
,'Held' AS SessionStatus
,v.DocLastSavedNm AS TherapistOrProvider
,FS.NameCalcInit
,FS.MedicaidNumber AS MedicaidNbr
,FS.ClientDOB AS DOB
,v.DocCreatedDt
,v.DocStatusCd
,v.DocSignedNm
,v.DocCreatorNm
,v.DocLastSavedDt
,v.MHODesc
,v.RateType
,v.MinutesRequired
,v.BillingUnitRate
,v.SupervisorUnitRate
,v.ServiceCodeGrpDesc
,v.ServiceLocation
,v.ServiceLocationBillingCd
,v.PhysComments
,v.InfectionControl
,v.NonServiceMinutes
INTO #TmpProgressNotes
FROM v_ProgressNotesHS v
INNER JOIN FD__TFS_FACE_SHEET FS ON v.TFSClientNumber = FS.TFSClientNumber
WHERE ServiceDt > = @BeginDate AND ServiceDt < = @EndDate
UNION
/**PROGRESS NOTE OP***/
Select
v.CampusInNote AS Campus
,'PNOP'
,v.DocID
,v.TFSclientnumber
,v.TFSProgramInNote
,v.CPTCd
,v.ServiceDt
,DatePart(ww,v.ServiceDt) AS ServiceWeek
,'FundingSource' = ISNULL(v.FundingSource,'Not Entered In Progress Note')
,NULL
,v.Detail
,v.ServiceCd
,v.ServiceUnits
,v.ServiceMinutes
,v.ServiceTm
,NULL
,v.ServiceDesc
,v.SessionStatus
,v.ServiceProvider AS TherapistOrProvider
,FS.NameCalcInit
,FS.MedicaidNumber AS MedicaidNbr
,FS.ClientDOB AS DOB
,v.DocCreatedDt
,v.DocStatusCd
,v.DocSignedNm
,v.DocCreatorNm
,v.DocLastSavedDt
,v.MHODesc
,v.RateType
,v.MinutesRequired
,v.BillingUnitRate
,v.SupervisorUnitRate
,v.ServiceCodeGrpDesc
,v.ServiceLocation
,v.ServiceLocationBillingCd
,NULL AS PhysComments
,NULL AS InfectionControl
,v.NonServiceMinutes
INTO #TmpProgressNotes
FROM v_ProgressNotesOP v
INNER JOIN FD__TFS_FACE_SHEET FS ON v.TFSClientNumber = FS.TFSClientNumber
WHERE ServiceDt > = @BeginDate AND ServiceDt < = @EndDate
/** Retrieve values from temp table**/
Select * from #TmpProgressNotes
WHERE v.DocCreatorNm = @TherapistName
AND vl.TFSProgramInNote = ISNULL(@ProgramOrCampus,'')
AND v.ServiceCodeGrpDesc=ISNULL(@ServiceCodeGroup,'')
END