سؤال

I have done scripts which I need for generating varchar as number but its only work for MySQL but I have SQL Server where it does not work.

Scripts is here:

SET @reference = 0;

UPDATE dbo.expozitura 
SET reference_subjektu = LPAD(@reference := @reference + 1, 8, '0') 
ORDER BY cislo_subjektu ASC;

This scripts in SQL Server still has a problem with syntax: it doesn't like ":".

I explain my problem so I have some varchar data in reference_subjekt. This data is not important for me so but I need generating new data to this column as number but it is still varchar type.

For example

My actually data is:

reference_subjektu
TR.Mac
Orgins
Kernel
Tybru
Serenity

But I need replace my before data for this data:

reference_subjektu
"00000001"
"00000002"
"00000003"
"00000004"
"00000005" and etc.....

Have you any idea please?

My SQL Server information:

  • Version: 11.0.2218

I try use your scripts: (a1ex07)

I using this from your code:

UPDATE dbo.expozitura 
SET reference_subjektu = replicate('0', 8 - len(rn) ) + b.rn
FROM dbo.expozitura  a 
INNER JOIN 
(
   SELECT dbo.expozitura.cislo_subjektu, ROW_NUMBER() OVER (ORDER BY cislo_subjektu) rn
   FROM dbo.expozitura
) b ON (b.cislo_subjektu = a.cislo_subjektu)

My primary key is cislo_subjektu

And its really return me only 123456 and etc to rows. But I need different style.

MY DB output when I used scripts:

cislo_subjektu  reference_subjektu  organizace  adresa_ulice    psc ico
1                       1                2                          729544866
2                       2                0        Linkoln 507       729544866
3                       3                0                     403 31   729544866
4                       4                0                          729544866
5                       5                0        Linkoln 66   578 99   729544866
6                       6                0                     558 41   729544866

Do you know that where I have problem?

هل كانت مفيدة؟

المحلول 2

Assuming you have a primary key dbo.expozitura.expozitura_id column, the following should do the job:

UPDATE dbo.expozitura 
SET reference_subjektu = replicate('0', 8-len(rn) )+b.rn
FROM dbo.expozitura  a 
INNER JOIN 
(
   SELECT expozitura_id, 
   CAST(ROW_NUMBER() OVER (ORDER BY cislo_subjektu) AS varchar) AS rn
   FROM dbo.expozitura
) b ON (b.expozitura_id = a.expozitura_id)

نصائح أخرى

This line isn't valid:

ORDER BY cislo_subjektu ASC

Order by isn't a valid clause in UPDATE statement.

This line isn't valid as well:

LPAD(@reference:=@reference+1, 8, '0')

You can't increment @reference this way.

This is th proper way to do this:

WITH UpdateData  As
(
SELECT reference_subjektu ,
ROW_NUMBER() OVER (ORDER BY cislo_subjektu ASC) AS RN
FROM dbo.expozitura
)

UPDATE dbo.expozitura
SET reference_subjektu = replicate('0', 8-len(RN) )
FROM dbo.expozitura 
INNER JOIN UpdateData ON expozitura.reference_subjektu = UpdateData.reference_subjektu
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top