Solved

Help coding a simple stored procedure

Posted on 2014-02-25
7
255 Views
Last Modified: 2014-02-26
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
Comment
Question by:britpopfan74
  • 3
  • 3
7 Comments
 
LVL 6

Assisted Solution

by:ButlerTechnology
ButlerTechnology earned 250 total points
ID: 39886221
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
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39886291
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
 

Author Comment

by:britpopfan74
ID: 39886541
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39886729
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
 

Author Comment

by:britpopfan74
ID: 39887305
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
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 250 total points
ID: 39888693
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
 

Author Comment

by:britpopfan74
ID: 39890268
Thanks to you both for your help...finally got the code working. Splitting the points.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 41
word0 challenge 4 53
Why is "packages element is not declared"? 2 39
Turning python script into an applet 12 39
This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now