?
Solved

SQL Server - Function Error for SELECT - RETURN Value

Posted on 2013-01-02
1
Medium Priority
?
478 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 2000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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, show how to setup several different housekeeping processes for a SQL Server.

762 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