Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change Stored Procedure Output Type

Posted on 2010-08-13
6
Medium Priority
?
571 Views
Last Modified: 2012-05-10
I very new to stored procedures..  I've created the follow procedure, everything seems to work OK, but I get an error on the RETURN:how do I change the output type from int to a string varchar?

Msg 245, Level 16, State 1, Procedure GetNexPermitNo, Line 31
Conversion failed when converting the varchar value '10-3198' to data type int.

USE [SQLBurnPermit]
GO
/****** Object:  StoredProcedure [dbo].[GetNexPermitNo]    Script Date: 08/13/2010 11:37:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetNexPermitNo]
	-- Add the parameters for the stored procedure here
	-- <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
	-- <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @PermitNumber varchar(15);

    -- Insert statements for procedure here
	DECLARE contact_cursor SCROLL CURSOR FOR

	SELECT PermitNo FROM tblPermits 
	ORDER BY PermitNo;
	OPEN contact_cursor;

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement. 

	FETCH LAST FROM contact_cursor
	INTO @PermitNumber;

	RETURN @PermitNumber;

	-- SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>

CLOSE contact_cursor;
DEALLOCATE contact_cursor;

END

Open in new window

0
Comment
Question by:DRRobinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33430917
Please see the attached query with your sample data and final query

Raj
create table #table
(
	TicketID              varchar(20),
	SetStatus             varchar(20), 
	SetDate               datetime,                        
	Count				int
)

insert into #table
select '100601BG15',      'Acknowledged',                      '2010-06-01 14:02:45.330',                   1
union all
select '100601BG15',      'Waiting on Business',      '2010-06-01 14:07:28.820',                   0 
union all
select '100601BG15',      'Pending',                              '2010-06-01 14:07:40.597',                   1
union all
select '100601BG15',      'Pending',                              '2010-06-01 14:34:43.737',                   1
union all
select '100601BG15',      'Pending',                           '2010-06-03 18:29:47.120',                   1      
union all
select '100601BG15',      'Post Dated',                      '2010-06-04 09:00:52.587',                   0
union all
select '100601BG15',      'Post Dated',                      '2010-06-07 12:42:37.960',                   0 
union all
select '100601BG15',      'Fulfilled',                              '2010-06-07 14:38:29.677',                   1




with cte as
(
	select *, row_number() over (order by SetDate) row_no 
	from #table
) 
select *, datediff(mi, a.SetDate, b.SetDate) as DiffMinute from cte a
	inner join cte b
		on a.row_no = b.row_no - 1

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33430951
Oops! Ignore my post.  Posted to other question than I intended

Raj

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 33431118
you can't
the return code from a stored procedure is an integer and is only intended to indicate the success of failure of the procedure itself.


to return data from a stored procedure

you either return a result set (select statement ouput)
or an output parameter..   exec yourproc @parm1,@parm2=@myout output

ps...
  is this really what you are attempting to do ?
  return 1 value from a db query?
     if so then just code it aas a select top 1 *.... style query...


 Cursors within stored procedures are very resource consuming and slow
 and should be avoided ... there are several other techniques that are generally applied to
  avoid them and "loop" around the result set... but in the main you need to think in termms of set
processing rather than doing things in a record/row by record/row manner...


hth
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 5

Expert Comment

by:almander
ID: 33431151
Change the line
RETURN @PermitNumber
to
SELECT @PermitNumber

And I would recommend looking at an alternative to cursors.

0
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 2000 total points
ID: 33431169
Unfortunately you can't.
The RETURN from a stored proc isn't meant for what you're trying to do, it can ONLY be an integer.

To do what you want, look at "output parameters" instead:
USE [SQLBurnPermit]
GO
/****** Object:  StoredProcedure [dbo].[GetNexPermitNo]    Script Date: 08/13/2010 11:37:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetNexPermitNo]
	--**** create the parameter here as "output"
	@PermitNumber varchar(15) output
	-- Add the parameters for the stored procedure here
	-- <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
	-- <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	--**** now DON'T declare it in the code...
	--DECLARE @PermitNumber varchar(15);

    -- Insert statements for procedure here
	DECLARE contact_cursor SCROLL CURSOR FOR

	SELECT PermitNo FROM tblPermits 
	ORDER BY PermitNo;
	OPEN contact_cursor;

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement. 

	FETCH LAST FROM contact_cursor
	INTO @PermitNumber;

	--**** now just return normally, the parameter was set in the line above
	RETURN-- @PermitNumber;

	-- SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
	
	
	
	/******** SAMPLE CALL
	declare @pNo varchar(15)
	exec GetNexPermitNo @pNo output
	
	print @pNo
	*/

CLOSE contact_cursor;
DEALLOCATE contact_cursor;

END

Open in new window

0
 

Author Closing Comment

by:DRRobinson
ID: 33431342
Thanks for the details..
0

Featured Post

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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