Help coding a simple stored procedure

britpopfan74
britpopfan74 used Ask the Experts™
on
Hello,

I'm a novice to stored procedures but need to learn b/c the number of queries I'm running is mind-boggling!

In the below simple stored procedure, I would like to set it up to enable getting back 3 pieces of information:

1. Region
2. Start date
3. End date

I started to try with 'Region'; however, I'm obviously not coding correctly because am getting error that no parameters and arguments were supplied.

If someone could help me get the 'Region' part running, I can figure out the other pieces. Thank you in advance!

--DROP PROCEDURE usp_surgery_code_lookup

USE INFORMATICS
GO

CREATE PROCEDURE usp_surgery_code_lookup
@Region varchar(4)
AS
 SET NOCOUNT ON;
go
SELECT DISTINCT
mc.[DOCUMENT]
, CLAIM_LINE_NUMBER
, mc.REGION
, mcd.PAR_NONPAR
, mc.FIRST_DOS
, mcd.PAY_DT
, mc.TOTAL_PAY_AMT AS TOTAL_PD
, mcd.GL_NO
, MCD.LINE_CODE
, C.DESCRIPTION AS CODEDEC
    FROM MHC_Custom.dbo.MASTER_CLAIM AS mc INNER JOIN
                      MHC_Custom.dbo.MASTER_CLAIM#DETAIL AS mcd ON mc.[DOCUMENT] = mcd.[DOCUMENT] LEFT OUTER JOIN
                      MHC_Custom.dbo.CODE C ON C.PROC_CD = MCD.LINE_CODE
WHERE (mc.[DOCUMENT] IS NOT NULL)
AND (mc.VALID_CLAIM IS NULL)
AND ((MCD.LINE_CODE IN('111','121','131','141','151')))
--and (first_dos BETWEEN @FDOS AND @EFF_THRU)
--and (mcd.PAY_DT BETWEEN @FDOS AND @PAY_THRU)
AND (MC.REGION LIKE '@Region%')
AND (MCD.CLAIM_LINE_NUMBER = 1)

GO

EXEC usp_MY_simple_proc  @Region = N'5xxx'
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I believe that you do not need the Go Statement after the SET NOCOUNT ON;.  Also, I am not seeing the BEGIN and END that should be part of your stored procedure.

Try this:
CREATE PROCEDURE usp_surgery_code_lookup
@Region varchar(4)
AS begin
 SET NOCOUNT ON;
SELECT DISTINCT 
mc.[DOCUMENT]
, CLAIM_LINE_NUMBER
, mc.REGION
, mcd.PAR_NONPAR
, mc.FIRST_DOS
, mcd.PAY_DT
, mc.TOTAL_PAY_AMT AS TOTAL_PD
, mcd.GL_NO
, MCD.LINE_CODE
, C.DESCRIPTION AS CODEDEC
    FROM MHC_Custom.dbo.MASTER_CLAIM AS mc INNER JOIN
                      MHC_Custom.dbo.MASTER_CLAIM#DETAIL AS mcd ON mc.[DOCUMENT] = mcd.[DOCUMENT] LEFT OUTER JOIN
                      MHC_Custom.dbo.CODE C ON C.PROC_CD = MCD.LINE_CODE 
WHERE (mc.[DOCUMENT] IS NOT NULL) 
AND (mc.VALID_CLAIM IS NULL)
AND ((MCD.LINE_CODE IN('111','121','131','141','151')))
--and (first_dos BETWEEN @FDOS AND @EFF_THRU)
--and (mcd.PAY_DT BETWEEN @FDOS AND @PAY_THRU)
AND (MC.REGION LIKE '@Region%') 
AND (MCD.CLAIM_LINE_NUMBER = 1)
end

Open in new window

I use a stored-procedure (SP) with arguments to filter data, and the final statement in the SP is a select-statement to pass the result-set back to the caller, example (result-set returned contains field1, field2, field3):
CREATE PROCEDURE usp_surgery_code_lookup
(	@Region	varchar(4)
)
AS
SELECT	field1
,	field2
,	field3
FROM	table1	a
JOIN	table2	b	ON  a.fieldx = b.fieldx
...
WHERE	a.regionid = @Region
...

Open in new window

Each SP argument may have the reserved word OUTPUT, which means if you alter the argument in the SP then the final argument value is available to the caller, example (no result-set, only 2 output arguments are altered):
CREATE PROCEDURE usp_surgery_code_lookup
(	@Region		varchar(4)
,	@StartDate	DateTime OUTPUT
,	@EndDate	DateTime OUTPUT
)
AS
SELECT	@StartDate	= start_date
,	@EndDate	= end_date
FROM	SomeTable
WHERE	regionid = @Region

Open in new window

Author

Commented:
Thank you both for your advice...now I have the code working.

But I'm still stuck at executing it...

If I write, for example, EXEC usp_MY_simple_proc  @Region = N'5SUO' , I get:

Command(s) completed successfully.

Is this why I would need to specify @Region in addition to the @Dates as OUTPUTs?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

You have no syntax-error from executing your SP, but I don't think your SP is meeting your needs.

Calling the SP depends on which technology you are using, i.e., calling from a development language (like .NET) requires different call compared to calling from within your database.

I would test the call in your database prior to implementing the call in a development language.  Even within a development language, extracting OUTPUT values from your parameters is different than capturing/extracting values from a result-set.

You haven't provided the SP code so we won't know what you're attempting.

Author

Commented:
Good point...well at the moment I am trying to call the so fro SQL Server directly. The SQL code is in the first box
Assume this is the code behind your SP:
CREATE PROCEDURE usp_surgery_code_lookup
(	@Region		varchar(4)
,	@StartDate	DateTime OUTPUT
,	@EndDate	DateTime OUTPUT
)
AS
SELECT	@StartDate	= start_date
,	@EndDate	= end_date
FROM	SomeTable
WHERE	regionid = @Region

Open in new window

The following would call the above SP from within your SQL Server tool (note, your variable-names don't need to match the names used in the SP, but they should match in data-type; you must use variables if OUTPUT is used):
DECLARE @myRC		int
,	@myRegion	varchar(4)
,	@myStartDate	datetime
,	@myEndDate	datetime

SET	@myRegion	= N'5SUO'

EXECUTE	@myRC		= usp_surgery_code_lookup
	@Region		= @myRegion
,	@StartDate	= @myStartDate	OUTPUT
,	@EndDate	= @myEndDate	OUTPUT

IF @myRC <> 0
	-- do some error-handling here
ELSE
	SELECT	@myRC
	,	@myRegion
	,	@myStartDate
	,	@myEndDate

Open in new window

Author

Commented:
Thanks to you both for your help...finally got the code working. Splitting the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial