sql - Using 'with' clause for update purpose -


i have following relation in sql server database:

enter image description here

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:

enter image description here

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:

enter image description here

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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -