I'm trying to RETURN an integer variable called @iReturnCode after comparing
2 columns from 2 different tables after comparing the columns. I'm not real
familiar with using RETURN for this type of purpose. If the 2 columns aren't
equal, I'd like to RETURN a 1, if they are equal, just RETURN 0.
Can some shed some light? I commented the 2 places below where I'm trying to
RETURN the @iReturnCode variable.
-- CODE: ****************************************
BEGIN
declare @dtStartDate datetime, @dtEndDate datetime
set @dtStartDate = '20080717'
set @dtEndDate = '20080717'
declare @TimeRun1 int, @TimeRun2 int, @iReturnCode int
IF @dtStartDate > @dtEndDate
SELECT 50002 As iRetCode,
'Start Date must be before
than End Date' As sErrMsg
ELSE
BEGIN
SET @TimeRun1 = (SELECT SUM(time_run) FROM myTable1)
SET @TimeRun2 = (SELECT SUM(time_run) FROM myTable2)
If @TimeRun1 <> @TimeRun2
-- RETURN @iReturnCode = 1
ELSE
-- RETURN @iReturnCode = 0
PRINT @TimeRun1
END
END
Aaron Bertrand [SQL Server MVP] - 22 Jul 2008 20:46 GMT
Seems more appropriate to use an OUTPUT parameter for this purpose.
However, if you want to continue using RETURN, simply change your syntax:
IF @TimeRun1 <> @TimeRun2
RETURN 1;
ELSE
RETURN 0;
There is no point using a returncode variable here, and note that anything
after RETURN (e.g. your print statement) is not processed.
On 7/22/08 3:33 PM, in article #vX4yID7IHA.4108@TK2MSFTNGP04.phx.gbl,
> I'm trying to RETURN an integer variable called @iReturnCode after comparing
> 2 columns from 2 different tables after comparing the columns. I'm not real
[quoted text clipped - 34 lines]
>
> END
Scott - 23 Jul 2008 01:58 GMT
I keep getting the error listed below. All my sql statements are returning
correct values, it's the RETURN statement. I can't even RETURN one of the
variables.
Any ideas?
-- CODE: ****************************
BEGIN
declare @dtStartDate datetime, @dtEndDate datetime
set @dtStartDate = '20080717'
set @dtEndDate = '20080717'
declare @accTimeRun int, @sqlTimeRun int
IF @dtStartDate > @dtEndDate
SELECT 50002 As iRetCode,
'Start Date must be before
than End Date' As sErrMsg
ELSE
SET @accTimeRun = (SELECT SUM(p.time_run) FROM myTable1)
SET @sqlTimeRun = (SELECT SUM(p.time_run) FROM myTable2)
If @accTimeRun > @sqlTimeRun
RETURN 1;
ELSE
RETURN 0;
END
-- Error: ****************************
A RETURN statement with a return value cannot be used in this context.
> Seems more appropriate to use an OUTPUT parameter for this purpose.
> However, if you want to continue using RETURN, simply change your syntax:
[quoted text clipped - 51 lines]
>>
>> END
Eric Isaacs - 23 Jul 2008 02:42 GMT
RETURN has to be used from within a Stored Procedure or a User Defined
Function. If you just want the SQL to provide the value to you
without a RETURN without a function or stored procedure, just use
SELECT (or PRINT.)
IF @accTimeRun > @sqlTimeRun
SELECT 1 AS ReturnValue;
ELSE
SELECT 0 AS ReturnValue;
IF @accTimeRun > @sqlTimeRun
PRINT 1
ELSE
PRINT 0
Also, if you don't care about the 0 or 1, you could just RETURN/SELECT/
PRINT @accTimeRun > @sqlTimeRun and get a Boolean value back.
SELECT @accTimeRun > @sqlTimeRun AS ReturnValue
-Eric Isaacs
Plamen Ratchev - 23 Jul 2008 03:05 GMT
> Also, if you don't care about the 0 or 1, you could just RETURN/SELECT/
> PRINT @accTimeRun > @sqlTimeRun and get a Boolean value back.
>
> SELECT @accTimeRun > @sqlTimeRun AS ReturnValue
This will not work in the current versions of SQL Server. The SQL standard
supports boolean data type but it is not implemented in SQL Server.
Plamen Ratchev
http://www.SQLStudio.com
Eric Isaacs - 23 Jul 2008 03:27 GMT
>> SELECT @accTimeRun > @sqlTimeRun AS ReturnValue
> This will not work in the current versions of SQL Server. The SQL standard
> supports boolean data type but it is not implemented in SQL Server.
I don't know what I was thinking. Thanks!
-Eric Isaacs
Alex Kuznetsov - 22 Jul 2008 20:47 GMT
> I'm trying to RETURN an integer variable called @iReturnCode after comparing
> 2 columns from 2 different tables after comparing the columns. I'm not real
[quoted text clipped - 34 lines]
>
> END
It is not clear what is your problem. Are you getting an error message?
Scott - 23 Jul 2008 01:25 GMT
no errors, i'm just trying to compare two values and return a 1 or 0
depending on the comparison. Aaron gave me what I was asking for.
Thanks for your interest.
On Jul 22, 2:33 pm, "Scott" <sbai...@mileslumber.com> wrote:
> I'm trying to RETURN an integer variable called @iReturnCode after
> comparing
[quoted text clipped - 38 lines]
>
> END
It is not clear what is your problem. Are you getting an error message?