sql - Using 'with' clause for update purpose -
i have following relation in sql server database:
assume want modifications in relation. want remove cpu_type table , add 1 column 'cpu' in computer table.
now want use 'with' clause in sql server query data joined 2 tables , update statement update computer table.
this sql server query:
select computer.compid, computer.serialno, computer.cpuid, cpu_type.name, cpu_type.cpuspeed, computer.cpu computer left outer join cpu_type on computer.cpuid = cpu_type.cpuid
and results follows:
now create dataset using 'with' clause follows:
with ds ( select computer.compid, computer.serialno, computer.cpuid, cpu_type.name, cpu_type.cpuspeed, computer.cpu computer left outer join cpu_type on computer.cpuid = cpu_type.cpuid ) update ds set cpu = name +', '+ cpuspeed;
and results follws:
the query executed problem computer table has not changed! how solve problem?
your code fine. data off. cpuspeed
null
result of concatenation null
. so, use coalesce()
:
with ds ( select computer.compid, computer.serialno, computer.cpuid, cpu_type.name, cpu_type.cpuspeed, computer.cpu computer left outer join cpu_type on computer.cpuid = cpu_type.cpuid ) update ds set cpu = name +', '+ coalesce(cpuspeed, '');
Comments
Post a Comment