日期:2014-05-17 浏览次数:20573 次
----------------------------
-- Author :磊仔
-- Date :2013-01-26 12:43:10
-- Version:
-- Microsoft SQL Server 2008 (SP2) - 10.0.4000.0 (Intel X86)
-- Sep 16 2010 20:09:22
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------
--> 测试数据:#TA
if object_id('tempdb.dbo.#TA') is not null drop table #TA
go
create table #TA([考号] int,[语文] int,[数学] int,[英语] int,[总分] sql_variant)
insert #TA
select 1,50,null,90,null union all
select 2,null,80,70,null union all
select 3,50,80,null,null union all
select 4,null,null,null,null union all
select 5,0,0,0,null
--------------开始查询--------------------------
UPDATE A SET
[总分] = B.x.value('sum(//row/*)','int') - [考号]
FROM #TA AS A
CROSS APPLY (SELECT x = (SELECT A.* FOR XML PATH('row'),TYPE)) AS B;
SELECT * FROM #TA;
----------------结果----------------------------
/*
考号 语文 数学 英语 总分
----------- ----------- ----------- ----------- --------
1 50 NULL 90 140
2 NULL 80 70 150
3 50 80 NULL 130
4 NULL NULL NULL &n