jika di kolom id ada beberapa data yang kosong, maka bisa di isi dengan di lanjutkan dari id yang besar (desc). caranya seperti dibawah ini
WITH T
AS (SELECT ISNULL((SELECT MAX(id) FROM nama_table), 0) +
ROW_NUMBER() OVER (ORDER BY id) AS id_baru,
id
FROM nama_table
WHERE id is null or id='')
UPDATE T
SET id = id_baru
Jumat, 24 Agustus 2018
Selasa, 31 Juli 2018
update ID yang sama.
pertama buat table temporary untuk menyimpan ID yang unik atau sama.
SELECT
PFN_ID
into temp_table
FROM
PFM_TEMP_PENILAIAN
GROUP BY
PFN_ID
HAVING
COUNT (pfn_id) > 1
setelah itu lanjut dengan script di bawah ini
SELECT a.PFN_ID,b.NIP,'PFN-' + RIGHT (
'z0000000000000' + CONVERT (
VARCHAR (30),
(
(
SELECT
dbo.nvl (
CAST (
dbo.substr (MAX(PFN_ID), 5, 6) AS INTEGER
),
0
)
FROM
PFM_TEMP_PENILAIAN
) + (
row_number () OVER (ORDER BY a.PFN_ID)
)
)
),
6
) AS PFN_ID_BARU INTO temp_table2 from temp_table AS a LEFT JOIN PFM_TEMP_PENILAIAN AS b ON a.PFN_ID=b.PFN_ID
Sekarang update ID baru.
UPDATE T
SET t.PFN_ID = r.PFN_ID_BARU
FROM
PFM_TEMP_PENILAIAN AS T
LEFT JOIN temp_table2 AS r ON T.NIP = r.NIP
AND T.PFN_ID = r.PFN_ID
WHERE
r.PFN_ID IS NOT NULL
SELECT
PFN_ID
into temp_table
FROM
PFM_TEMP_PENILAIAN
GROUP BY
PFN_ID
HAVING
COUNT (pfn_id) > 1
setelah itu lanjut dengan script di bawah ini
SELECT a.PFN_ID,b.NIP,'PFN-' + RIGHT (
'z0000000000000' + CONVERT (
VARCHAR (30),
(
(
SELECT
dbo.nvl (
CAST (
dbo.substr (MAX(PFN_ID), 5, 6) AS INTEGER
),
0
)
FROM
PFM_TEMP_PENILAIAN
) + (
row_number () OVER (ORDER BY a.PFN_ID)
)
)
),
6
) AS PFN_ID_BARU INTO temp_table2 from temp_table AS a LEFT JOIN PFM_TEMP_PENILAIAN AS b ON a.PFN_ID=b.PFN_ID
Sekarang update ID baru.
UPDATE T
SET t.PFN_ID = r.PFN_ID_BARU
FROM
PFM_TEMP_PENILAIAN AS T
LEFT JOIN temp_table2 AS r ON T.NIP = r.NIP
AND T.PFN_ID = r.PFN_ID
WHERE
r.PFN_ID IS NOT NULL
Langganan:
Postingan (Atom)