• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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'
0
britpopfan74
Asked:
britpopfan74
  • 3
  • 3
2 Solutions
 
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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
 
britpopfan74Author Commented:
Thanks to you both for your help...finally got the code working. Splitting the points.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now