DECLARE @claves nvarchar(max)
DECLARE lectorClaves CURSOR FOR SELECT [clave] FROM [dbo].[nombres]
DECLARE @claves nvarchar(max)
DECLARE lectorClaves CURSOR
FOR
SELECT [clave] FROM [dbo].[nombres]
OPEN lectorClaves
FETCH NEXT FROM lectorClaves INTO @claves
WHILE (@@FETCH_STATUS = 0)
BEGIN
IF NOT EXISTS (
SELECT clave FROM nombres n
inner join diagnosticos d
ON n.clave = d.svMaeDia_pky and n.clave = @claves
)
BEGIN
INSERT INTO diagnosticos (
[svMaeDia_pky],
[svMaeDia_diagnostico],
[svMaeDia_servicio_fky]
)
SELECT
clave,
nombre,
apellido
FROM nombres WHERE clave = @claves
END
ELSE
BEGIN
UPDATE nombres
SET clave = dbo.NuevaClase()
WHERE clave = @claves
INSERT INTO diagnosticos (
[svMaeDia_pky],
[svMaeDia_diagnostico],
[svMaeDia_servicio_fky]
)
select
clave,
nombre,
apellido
FROM nombres WHERE clave = @claves
END
SELECT [clave] FROM [dbo].[nombres]
WHERE [clave] = @claves
FETCH NEXT FROM lectorClaves INTO @claves
END
CLOSE lectorClaves
DEALLOCATE lectorClaves
DEALLOCATE lectorClaves