Reputation: 2055
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
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
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
Reputation: 2950
I don't think you need the word SET
in this statement.
(@NAME_SURNAME = (PLP.NAME +' '+ PLP.SURNAME))
Upvotes: 1