English 中文(简体)
缩略语
原标题:SQL UPDATE SET one Column to another value and change value in same step
  • 时间:2011-09-15 15:47:31
  •  标签:
  • tsql

发表以下最新声明。

UPDATE  TABLE_1
SET     Units2 = ABS(Units1)
        ,Dollars2=ABS(Dallars1)
        ,Units1 =0
        ,Dollars1 =0
WHERE Units1 < 0
AND   Dollars2 = 0

我的问题是:

(1) 这是否合法? 它禁止并“种子”工作(在试验桌上),但总是工作,或者我只是拿正确的记录来审查。

2)这是更好的办法。

感谢

最佳回答

它是合法的,只要你想要基本上保留<代码>Units1和Dollars1Units2_Dollars2上的旧价值,就应当工作。

试验:

CREATE TABLE #Table_1
(
    Units1 INT,
    Dollars1 MONEY,
    Units2 INT,
    Dollars2 MONEY
)
GO

INSERT INTO #Table_1 (Units1, Dollars1, Units2, Dollars2)
VALUES (-1,12.00,3,0.00)
GO

UPDATE  #TABLE_1
SET     Units2 = ABS(Units1)
        ,Dollars2=ABS(Dollars1)
        ,Units1 =0
        ,Dollars1 =0
WHERE Units1 < 0
AND   Dollars2 = 0
GO

SELECT * 
FROM  #Table_1

产出:

Units1 | Dollars1 | Units2| Dollars2
0      | 0.00     | 1     | 12.00
问题回答

假设你再谈论美元1栏,我认为这应该罚款。 阅读时应当使用现值,在计算后进行书写。

如果你现在再次问这个问题,但我建议将其分为两场发言。 你回过提交人,对你来说不很清楚。 在必须维持的伪装上一点点,现在就清楚这一点。

你的问询是正确的,在很大程度上是这样做的方法。





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...