CREATE OR ALTER PROCEDURE sp_DepartmanGuncelle(@Tip int,@EskiDepartman nvarchar(8),@YeniDepartman nvarchar(8))
AS
BEGIN
	declare @Sorgu nvarchar(1000);
	declare @SelectSorgu nvarchar(max);
	declare @tablename nvarchar(50)
	declare csr_dep cursor for 	
		SELECT t.name
		  FROM sys.tables t,
			   sys.columns c
		 WHERE t.object_id = c.object_id
		   AND c.name = 'Departman';
	
	open csr_dep
	fetch next from csr_dep into @tablename
	while @@FETCH_STATUS = 0
	begin	
			IF @Tip = 0
			BEGIN
				SET @sorgu = ' UPDATE '+@tablename+' SET  Departman = ''' + @YeniDepartman+ ''' WHERE Departman = ''' + @EskiDepartman  + '''';
			END
			ELSE
			BEGIN
				SET @sorgu = ' UPDATE '+@tablename+' SET  Departman = ''' + @YeniDepartman+ '''';
			END;

			EXEC sp_executesql @sorgu;  			

	fetch next from csr_dep into @tablename
	end
	close csr_dep
	deallocate csr_dep
END;

--exec sp_DepartmanGuncelle 1,NULL,'05000000'

