Link to home
Start Free TrialLog in
Avatar of britpopfan74
britpopfan74Flag for United States of America

asked on

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'
SOLUTION
Avatar of ButlerTechnology
ButlerTechnology

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John_Vidmar
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

Avatar of britpopfan74

ASKER

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?
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.
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to you both for your help...finally got the code working. Splitting the points.