Solved

Change Stored Procedure Output Type

Posted on 2010-08-13
6
557 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

17 Experts available now in Live!

Get 1:1 Help Now