1

I have done the IF statement and am prepared to return the value based on the conditions provided. I tried RETURN CASE too and I receive the exact same error

The intention is to convert currency from £ to $ or EURO, it does the multiplication and returns.

I will polish the conversation latter but help make it return the value please.

If you look near the end of my query you will see RETURN @Converted, although it looks fine here but it gives the following error

[ Msg 178, Level 15, State 1, Procedure fnCurrencyConverter, Line 23 A RETURN statement with a return value cannot be used in this context. ]

Thanks

USE temp
GO

CREATE FUNCTION dbo.fnCurrencyConverter (@inPutMoneey INT, @inPutRegionCode VARCHAR(5))
RETURNS @Return TABLE
(
    Monee MONEY, 
    RegionCode CHAR(5)
)
AS
BEGIN

DECLARE @Converted MONEY

SET @Converted = 0

    BEGIN
        IF(@inPutRegionCode = 'US')
            SET @Converted = @inPutMoneey * 1.29
        ELSE IF(@inPutRegionCode = 'EU')
            SET @Converted = @inPutMoneey * 1.13
        ELSE IF(@inPutRegionCode = 'AFG')
            SET @Converted = @inPutMoneey * 0.50
    END
RETURN 
    @Converted
END

If region code is US then multiply the value by 1.29 which is the current POUND to DOLLAR rate and so on.

1
  • 1
    Your function is declared as returning a table but it returns MONEY? Commented Jan 21, 2019 at 5:11

2 Answers 2

2

No need to mention table variable name. Just mention RETURN. that is enough. You are not populating the table properly.

USE tempdb
GO

CREATE FUNCTION dbo.fnCurrencyConverter (@inPutMoneey INT, @inPutRegionCode VARCHAR(5))
RETURNS @Return TABLE
(
    Monee MONEY, 
    RegionCode CHAR(5)
)
AS
BEGIN

DECLARE @Converted MONEY

SET @Converted = 0

    BEGIN
        IF(@inPutRegionCode = 'US')
        BEGIN
            SET @Converted = @inPutMoneey * 1.29
            INSERT INTO @Return VALUES (@Converted, 'US')
        END 
        ELSE IF(@inPutRegionCode = 'EU')
        BEGIN
            SET @Converted = @inPutMoneey * 1.13
            INSERT INTO @Return VALUES (@Converted, 'EU')
        END
        ELSE IF(@inPutRegionCode = 'AFG')
        BEGIN
            SET @Converted = @inPutMoneey * 0.50
            INSERT INTO @Return VALUES (@Converted, 'AFG')
        END
    END
RETURN     
END
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, my query was coded exactly as urs but missing INSERT statement. i kept changing it but made no difference got a little frustrated lol. I created this function to struggle on purpose, i will comment on here if I have any further queries about this. thanks a lot
Glad that it helped
1

You can use single insert at last because at a time one condition would be true and executed

CREATE FUNCTION dbo.fnCurrencyConverter (@inPutMoneey INT, @inPutRegionCode VARCHAR(5))
RETURNS @Return TABLE
(
    Monee MONEY, 
    RegionCode CHAR(5)
)
AS
BEGIN

DECLARE @Converted MONEY

SET @Converted = 0

    BEGIN
        IF(@inPutRegionCode = 'US')
            SET @Converted = @inPutMoneey * 1.29
        ELSE IF(@inPutRegionCode = 'EU')
            SET @Converted = @inPutMoneey * 1.13
        ELSE IF(@inPutRegionCode = 'AFG')
            SET @Converted = @inPutMoneey * 0.50
    END

    INSERT INTO @Return VALUES(@Converted, @inPutRegionCode)
    --You can also use variable for Region code
    RETURN
END

1 Comment

Thanks a lot, it will reduce coding by a big margin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.