Rafał Developer
Rafał Developer

Reputation: 2055

equals Test into sql

I would like to use equals into select is it possible to do it?

SELECT EXAMPLE

DECLARE @NAME_SURNAME varchar(200);
SELECT
(CASE WHEN (PLP.NAME +' '+ PLP.SURNAME) != @NAME_SURNAME THEN (SET @NAME_SURNAME = (PLP.NAME +' '+ PLP.SURNAME))  END) AS 'LP.', 

Problem:

(SET @NAME_SURNAME = (PLP.NAME +' '+ PLP.SURNAME))

Upvotes: 0

Views: 69

Answers (3)

Edwin Stoteler
Edwin Stoteler

Reputation: 1228

SELECT @NAME_SURNAME = 
    CASE WHEN PLP.NAME +' '+ PLP.SURNAME != @NAME_SURNAME 
    THEN PLP.NAME +' '+ PLP.SURNAME
    ELSE @NAME_SURNAME
    END AS 'LP.'

But wouldn't this always select the same?

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270513

Your SQL doesn't quite makes sense. You seem to be trying to set a variable and return a value (as "LP.") at the same time. This is not allowed. Here is the note in the documentation:

A SELECT statement that contains a variable assignment cannot be used to also perform typical result set retrieval operations.

The correct syntax for setting a variable using a select is this:

select @NAME_SURNAME = (CASE WHEN (PLP.NAME +' '+ PLP.SURNAME) !=  @NAME_SURNAME
                             THEN (PLP.NAME +' '+ PLP.SURNAME)
                             else @NAME_SURNAME
                        end)

That is, the set keyword is not allowed within a select statement.

Upvotes: 2

Chris
Chris

Reputation: 2950

I don't think you need the word SET in this statement.

(@NAME_SURNAME = (PLP.NAME +' '+ PLP.SURNAME))

Upvotes: 1

Related Questions