go
sp_rename 'xsmeta.datainicial', 'datainicialprevistaacoes', 'COLUMN'
go
sp_rename 'xsmeta.datafinal', 'datafinalprevistaacoes', 'COLUMN'
go
sp_rename 'xsmeta.datainicioprojeto', 'datainicialrealizadaacoes', 'COLUMN'
go
sp_rename 'xsmeta.dataterminoprojeto', 'datafinalrealizadaacoes', 'COLUMN'
go
sp_rename 'xsmeta.prazo', 'terminoprojeto', 'COLUMN'
go
sp_rename 'xshistoricoicprojeto.prazo', 'terminoprojeto', 'COLUMN'
go
update dbo.xsmeta set datainicialprevistaacoes=inicioprojeto where datainicialprevistaacoes is null
go
update dbo.xsmeta set datafinalprevistaacoes=terminoprojeto where datafinalprevistaacoes is null
go
create   function [dbo].[xsfnDuracaoAcao]
(@datainicial datetime,@datafinal datetime)
returns integer
as
begin
  declare @duracao integer
  declare @auxdata datetime
  if (@datainicial is null) or (@datafinal is null) 
    select @duracao = null
  else
  begin
    select @auxdata=@datainicial
	select @duracao=0
	while @auxdata<=@datafinal 
	begin
      if not ((DATEPART(weekday,@auxdata)=7) or (DATEPART(weekday,@auxdata)=1))
       select @duracao=@duracao+1
      select @auxdata=DATEADD(dd,1,@auxdata)
	end
  end
  return @duracao
end
go
update xsacao set duracao=dbo.xsfnDuracaoAcao(datainicioprevisto,dataterminoprevisto) where duracao is null
go
drop function dbo.xsfnDuracaoAcao
go
