CREATE OR ALTER PROC StokNo_Farklilari_Degistir
AS   
BEGIN  
	DECLARE @StokNo int,@NoId int,@NoId2 int

	DECLARE db_cursor CURSOR FOR 
	SELECT StokNo,MIN(NoId) NoId from  STOKKARTI group by StokNo having count(*) >  1 
	OPEN db_cursor  

	FETCH NEXT FROM db_cursor INTO @StokNo,@NoId 

	WHILE @@FETCH_STATUS = 0  
	BEGIN
	
		DECLARE db_cursor2 CURSOR FOR 
		SELECT NoId FROM STOKKARTI WHERE StokNo = @StokNo and NoId <> @NoId

		OPEN db_cursor2  
		FETCH NEXT FROM db_cursor2 INTO @NoId2  

		WHILE @@FETCH_STATUS = 0  
		BEGIN  
			  update STOKKARTI  SET StokNo = (select MAX(StokNo) + 1 from STOKKARTI) where  NoId = @NoId2			  
			  UPDATE n  SET StokNo = k2.StokNo  FROM STOKHAR  n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2
			  UPDATE n  SET StokNo = k2.StokNo  FROM SIPARISHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokBarkodlari   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokFiyat   n ,  STOKKARTI k2 where n.Kodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokDB   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM StokHarLog   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM MuadilStoklar   n ,  STOKKARTI k2 where n.MuadilStokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM Buton   n ,  STOKKARTI k2 where n.ButonDegeri =k2.Kodu   and k2.NoId = @NoId2
			  UPDATE n  SET StokNo = k2.StokNo  FROM PDegFiyatlar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu  and k2.NoId = @NoId2 
			  UPDATE n  SET StokNo = k2.StokNo  FROM SipHar   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM TEKLIFHAR   n ,  STOKKARTI k2 where n.StokKodu =k2.Kodu and k2.NoId = @NoId2  
			  UPDATE n  SET StokNo = k2.StokNo  FROM Tevkifat   n ,  STOKKARTI k2 where n.Kod =k2.Kodu and k2.NoId = @NoId2   

		FETCH NEXT FROM db_cursor2 INTO @NoId2 
		END 

		CLOSE db_cursor2 
		DEALLOCATE db_cursor2

	FETCH NEXT FROM db_cursor INTO @StokNo,@NoId  
	END 

	CLOSE db_cursor  
	DEALLOCATE db_cursor
END;  