Good afternoon colleague, have the following case to see if they help me to solve it turns out that I am working on sql server 2012
.
I want to insert a consecutive number in a field x
according to a certain order example:
this is my code
DECLARE @id AS INT
SELECT @id =ROW_NUMBER() OVER(ORDER BY a.COD_ART) , a.COD_ART FROM ARTFISIC AS a GROUP BY A.COD_ART ORDER BY a.COD_ART asc
UPDATE ARTFISIC
SET
MARCA_MARB='X',
MARBETE=@id,
CONTEO01='0',
CONTEO02='0',
CONTEO03='0',
CONTEOCERO='0',
DIFERENCIA='0',
EXIST_CONT='0',
ENT_RETRO='0',
SAL_RETRO='0',
EXIST_FISI='0',
FIS_VS_TEO='0'
I get the following error:
A SELECT statement that assigns a value to a variable should not be combined with data retrieval operations.
I look forward to your usual help. I thank you in advance.
God bless you
For what, you have to create a temporary table with the ordered data first, to be able to number them.
This table will put a number in front of each item
Then, with this table, we can update the other table, matching the Label field to Number
Remember to drop the temp table once you're done (although it should be gone once you log out).