Solved

Change Stored Procedure Output Type

Posted on 2010-08-13
6
556 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
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 22

Accepted Solution

by:
Snarf0001 earned 500 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

21 Experts available now in Live!

Get 1:1 Help Now