Lets imagine that I want to set all the columns after the first column id to 0 and that the column names in the table I would like to reset, they are defined in another table. To illustrate, lets imagine I am working with various currencies as defined in #tmpFX table. What is the SQL 2005 UPDATE command that allows me to set gbp, eur, jpy and usd all equal to 0 where ID = 2 based on a (SELECT * from #tmpFX
) that identifies the columns that need updating?
use tempdb
GO
CREATE TABLE #tmpFX(iso_code VARCHAR(3))
INSERT #tmpFX VALUES('gbp')
INSERT #tmpFX VALUES('eur')
INSERT #tmpFX VALUES('jpy')
INSERT #tmpFX VALUES('usd')
SELECT * FROM #tmpFX
CREATE TABLE #tmpCashVal (id INT, gbp REAL, eur REAL, jpy REAL, usd REAL)
INSERT #tmpCashVal VALUES (1, 0, 0, 0, 1000)
INSERT #tmpCashVal VALUES (2, 0, 0, 2000, 0)
INSERT #tmpCashVal VALUES (3, 500, 0, 0, 0)
SELECT * FROM #tmpFX
SELECT * FROM #tmpCashVal
DROP TABLE #tmpFX
DROP TABLE #tmpCashVal
i.e. row 2 of #tmpCashVal would read after the UPDATE command:
id gbp eur jpy usd
2 0 0 0 0
Many thanks, Bertie.