Solved

SQL Server - Function Error for SELECT - RETURN Value

Posted on 2013-01-02
1
455 Views
Last Modified: 2013-01-02
Hi ....

Here is a Function I want to build in order to return me one value which is a DateTime format ...

I got this error any Time I tried to execute it :

Msg 444, Level 16, State 3, Procedure DateInterval, Line 20
Select statements included within a function cannot return data to a client

I will make a Stored Procedure as a last alternative, but I would like to know if this is related to the fact that I am using a SELECT to get my Return Value ? Make no sense for me

Any help


ALTER FUNCTION [dbo].[DateInterval] (@Month Int, @Year Int, @Interval Int)

RETURNS DateTime  

AS

BEGIN

      DECLARE @MyDate DateTime
      DECLARE @EOMonthDate Datetime
      DECLARE @DateInterval Datetime

      -- Get a date from the two Inputs @Month & @Year
      SET @MyDate = CONVERT(DateTime, CONVERT(VARCHAR(4), @Year) + RIGHT('0' + CONVERT(VARCHAR(2), @Month), 2) + '01', 102)
      SELECT CONVERT(VARCHAR(10),@MyDate ,105)

      -- Get the latest Day-Month from the built Date
      SET @EOMonthDate = (SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, @MyDate) +1, 0)))

      -- Return the Date after calculation  
      SET @DateInterval = (SELECT DATEADD(MM, @Interval, @EOMonthDate ))

      -- Return the result of the function
      RETURN @DateInterval
      
END
0
Comment
Question by:venmarces
1 Comment
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38738647
The error is that you have this statement in the function where the output is not assigned to a variable

SELECT CONVERT(VARCHAR(10),@MyDate ,105)

The default behavior is to output a select statement like that to the client.  In a function it can't do that.

So assign it to a variable if it's important, or remove it if it's not.

(Tested on SQL Server 2005, removing the line made it compile - but if you need it, I suggest assigning it to a variable and using it for whatever you need it for).
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

929 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

14 Experts available now in Live!

Get 1:1 Help Now