Solved

SQL Server - Function Error for SELECT - RETURN Value

Posted on 2013-01-02
1
457 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 47
sql server computed columns 11 29
optimize stored procedure 6 23
VB.NET 2008 - SQL Timeout 9 22
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

813 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

13 Experts available now in Live!

Get 1:1 Help Now