Only one expression can be specified in the select list when the subquery is not introduced with EXISTS

1

This is my query ...

DECLARE @fecha0 datetime
SET @fecha0 = (select top 1* 
    FROM events
    WHERE plate like '%D7Z716%' AND
        CONVERT(varchar, received, 103) = CONVERT(varchar, GETDATE(), 103) AND ADDRESS LIKE '%W BAYER%')

IF EXISTS(SELECT ISNULL(MIN(received),'') AS fecha 
    FROM events 
    WHERE plate LIKE '%D7Z716%'
        AND CONVERT(varchar,received,103)=CONVERT(varchar, GETDATE(), 103) AND ADDRESS LIKE '%W BAYER%')
BEGIN
    SELECT TOP(1) received 
    FROM events 
    WHERE received > @fecha0
END

The error that comes to me.

  

Only one expression can be specified in the select list when the subquery is not   introduced with EXISTS.

    
asked by Kevin Quevedo 26.10.2018 в 20:28
source

1 answer

2

Your problem is in the first subselect, when loading the variable @date0 it does not return a specific column.

DECLARE @fecha0 datetime
SET @fecha0 = (SELECT TOP(1) * -- Aquí debes especificar la columna, no usar "*"
    FROM events
    WHERE plate like '%D7Z716%' AND
        CONVERT(varchar, received, 103) = CONVERT(varchar, GETDATE(), 103) 
        AND ADDRESS LIKE '%W BAYER%')

IF EXISTS(
    SELECT ISNULL(MIN(received),'')
    FROM events
    WHERE plate LIKE '%D7Z716%'
        AND CONVERT(varchar,received,103) = CONVERT(varchar, GETDATE(), 103) 
        AND ADDRESS LIKE '%W BAYER%'
)
BEGIN
    SELECT TOP(1) received 
    FROM events 
    WHERE received > @fecha0
END

I hope my answer has helped you.

    
answered by 26.10.2018 в 22:15