Solved

SQL Server - Function Error for SELECT - RETURN Value

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

705 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