Hi guys,
I have the following stored process that doesn't work, every time I run it it stays in an infinite loop.
The objective of the SP is to go through the table of products for each row to evaluate the result and if the result when counting the rows is greater than 0, the code must be inserted in a table, if the result is 0 the record must be inserted according to the third query.
ALTER PROCEDURE [dbo].[SP_Fuentes]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @suministro AS VARCHAR(8000)
DECLARE @QUERY AS VARCHAR (800)
DECLARE @fecha as varchar (90)
DECLARE @FUENTE AS VARCHAR (800)
DECLARE @ID as varchar (90)
DECLARE @NOMBRE AS VARCHAR (800)
DECLARE #P CURSOR FOR
-- Cursor para recorrer rescatar todos los Tags a procesar
SELECT NRO_SUMINISTRO
FROM PRUEBA
group by nro_suministro
OPEN #P
-- Recupera primera fila
FETCH NEXT FROM #P INTO
@suministro
-- check for a new row
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @QUERY = count (*) from (
SELECT TOP 1 [ID]
,[NRO_SUMINISTRO]
,[FEC_MAX]
,[FUENTE]
,[NOMBRE]
FROM PRUEBA
WHERE ( FUENTE = 1 OR FUENTE =2 OR FUENTE = 3 ) and nro_suministro = @suministro
order by FEC_MAX DESC
) a
--SELECT @QUERY;
IF @QUERY > 0
BEGIN
INSERT INTO dbo.IZACION
SELECT @suministro,@fecha,@NOMBRE, @FUENTE , @ID ;
END
if @QUERY < 0
INSERT INTO ZACION
SELECT top 1 @suministro,@fecha,@NOMBRE, @FUENTE , @ID
FROM ZACION
WHERE nro_suministro = @suministro
order by FUENTE ASC
END
FETCH NEXT FROM #P INTO @suministro
END
CLOSE #P
DEALLOCATE #P
please help , regards and thanks!