Help coding a simple stored procedure

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'
britpopfan74Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ButlerTechnologyCommented:
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

0
John_VidmarCommented:
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

0
britpopfan74Author 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?
0
Determine the Perfect Price for Your IT Services

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

John_VidmarCommented:
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.
0
britpopfan74Author 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
0
John_VidmarCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
britpopfan74Author Commented:
Thanks to you both for your help...finally got the code working. Splitting the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.