What are the challenges you face when working across database platforms? Take the survey

Treating MIN and MAX as non-scalar

kalokalo Posts: 88 Bronze 5
i'm getting a EI003 warning (Subquery may return more than one row.) for the following code where only one value would be returned; putting TOP 1 {} ORDER BY round it seems like overkill just to remove warning

DECLARE @foo INT = (
SELECT (MAX([value].[val]))
FROM (VALUES(1),(2),(3))AS[value]([val])

Best Answers


  • Options
    kalokalo Posts: 88 Bronze 5
    Thanks Jessica I shall add a request : it seems I get the warning even when I and assign a variable to the result of a created scalar function
  • Options
    Jessica RJessica R Posts: 1,319 Rose Gold 4
    Thanks @kalo!

    Would it be possible to get a copy of the create script for one of the scalar functions that are still getting the warning?

    Jessica Ramos | Product Support Engineer | Redgate Software

    Have you visited our Help Center?

  • Options
    kalokalo Posts: 88 Bronze 5
    Hi Jessica, seems it occurs only if i wrap use a select in the right hand side of the statement assigning the value so I can remove the warning by removing the SELECT keyword.


    IF EXISTS(SELECT (1) FROM sys.objects [o] WHERE (SCHEMA_NAME([o].[schema_id]) = (N'dbo')) AND [o].[name] = (N'fn_myfunc') AND [o].[type]=(N'FN')) DROP FUNCTION [dbo].[fn_myfunc];
    CREATE FUNCTION [dbo].[fn_myfunc] ()
    RETURN(N'hello world')
    ;DECLARE @HasWarning nvarchar(11) = (SELECT [dbo].[fn_myfunc]())
    ;DECLARE @NoWarning nvarchar(11) = ([dbo].[fn_myfunc]())

  • Options
    kalokalo Posts: 88 Bronze 5
    Therefore i can remove the warning from MIN/MAX also by not initializing when i declare the variable , but I'd rather keep to the original syntax style

    no squiggly line in this example

    DECLARE @foo INT

    SELECT @foo = (MAX([value].[val]))
    FROM (VALUES(1),(2),(3))AS[value]([val])

    SELECT @foo
Sign In or Register to comment.