I have the following table where all fields except Value as a part of a unique index on the table. I'd like to inherit the Value from a record with Value<>NULL
to the next revision record only if that record has a NULL value (See example below):
Var[n]Value
fields ate varchar and there maybe 1-n number of Var[n]value
fields in my table.
Source:
Document# Revision Project# config# Var1Value Var2Value
1744 1 2 1 NULL NULL
1744 2 2 1 NULL NULL
1744 3 2 1 Tit1 ABC
1744 4 2 1 Tit2 ABD
1744 5 2 1 NULL NULL
1744 6 2 1 NULL SDC
1744 7 2 1 AS
1744 8 2 1 Tit3 NULL
Needed result (notice change for revision records 5 and 6 var1value
and 5,8 for var2value
):
Document# Revision Project# config# Var1Value Var2Value
1744 1 2 1 NULL NULL
1744 2 2 1 NULL NULL
1744 3 2 1 Tit1 ABC
1744 4 2 1 Tit2 ABD
1744 5 2 1 Tit2 ABD
1744 6 2 1 Tit2 SDC
1744 7 2 1 AS
1744 8 2 1 Tit3 AS
Any idea how to handle it by SQL?
Please advise.
I tried the following:
declare @TableName as VarChar(32) = 'MYTABLE'
declare @SetClause as VarChar(1024)
declare @LWhereClause as VarChar(1024)
declare @RWhereClause as VarChar(1024)
-- Get the column names.
select Column_Name
into #Columns
from Information_Schema.Columns
where Table_Name = @TableName and Column_Name like 'Var%'
--select * from #Columns
-- Assemble the clauses we'll need for the UPDATE statement.
declare @ColumnName as VarChar(32)
while ( @@RowCount > 0 )
begin
select top 1 @ColumnName = Column_Name
from #Columns
order by Column_Name
set @SetClause = case when @SetClause is NULL then '' else @SetClause + ', ' end +
@ColumnName + ' = Coalesce( L.' + @ColumnName + ', R.' + @ColumnName + ' )'
set @LWhereClause = case when @LWhereClause is NULL then '' else @LWhereClause + ' or ' end +
'L.' + @ColumnName + ' is NULL'
set @RWhereClause = case when @RWhereClause is NULL then '' else @RWhereClause + ' or ' end +
'R.' + @ColumnName + ' is not NULL'
delete from #Columns
where Column_Name = @ColumnName
end
--select @SetClause, @LWhereClause, @RWhereClause
-- Put together the UPDATE statement.
declare @Update as nVarChar(max)
set @Update=''
set @Update=@Update +
'update L set ' + @SetClause + ' from ' + @TableName +
' as L inner join ' + @TableName + ' as R on R.DocId = L.DocId and R.Rev = L.Rev - 1 and R.Proj = L.Proj and R.Conf = L.Conf' +
' where ( ' + @LWhereClause + ' ) and ( ' + @RWhereClause + ' )'
-- Put together the entire loop. This needs work.
declare @Loop as nVarChar(max)
set @Loop =''
set @Loop=@Loop+
'@declare Eleanor as Int = 42;
while ( @Eleanor > 0 ) '
+ @Update + '
set @Eleanor = @@RowCount
end'
--select @Loop
-- Execute it.
exec @Loop
drop table #Columns
and I get the following error on exec loop. Why it is truncating the nvarchar string?
Msg 203, Level 16, State 2, Line 53
The name '@declare Eleanor as Int = 42;
while ( @Eleanor > 0 ) update L set variable104 = Coalesce( L.variable104, R.variable104 ), variable105 = Coalesce( L.variable105, R.variable105 ), variable106 = Coalesce( L.variable106, R.variable106 ), variable107 = Coalesce( L.variable107, R.variable107 ), variable112 = Coalesce( L.variable112, R.variable112 ), variable116 = Coalesce( L.variable116, R.variable116 ), variable119 = Coalesce( L.variable119, R.variable119 ), variable120 = Coalesce( L.variable120, R.variable120 ), variable121 = Coalesce( L.variable121, R.variable121 ), variable122 = Coalesce( L.variable122, R.variable122 ), variable124 = Co' is not a valid identifier.