如何将表中序数位置 x 之外的所有列重置为 0?
假设我想将第一个列 id 之后的所有列设置为 0,并且我想重置表中的列名称,它们是在另一个表中定义的。为了说明这一点,让我们假设我正在使用 #tmpFX 表中定义的各种货币。 SQL 2005 UPDATE 命令是什么,允许我根据标识需要的列的 (SELECT * from #tmpFX
) 将 gbp、eur、jpy 和 usd 全部设置为 0,其中 ID = 2更新?
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
即 #tmpCashVal 的第 2 行将在 UPDATE 命令后读取:
id gbp eur jpy usd
2 0 0 0 0
非常感谢, 伯蒂。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就能解决问题:
显然,您现在可以更改 @Query 字符串来执行您喜欢的任何操作。
您还可以使用游标来执行此操作,依次选择 #tmpFX 中的每个值,并使用动态 SQL 来执行它,但显然您会付出随之而来的性能损失。
有关将所有列放入一个字符串的更多信息,请参阅将多行连接到单个文本字符串?
This will do the trick:
Obviously, you can change the @Query string to do whatever you like at this point.
You could also do this with a cursor, selecting each value out of #tmpFX in turn, and using dynamic SQL to execute it as here, but obviously you pay the performance penalty that goes with that.
For more on putting all the columns into one string, see Concatenate many rows into a single text string?
没有标准语法允许您按序号位置引用列,无论如何在 Transact-SQL 中也是如此。
不过,您可以通过查询
INFORMATION_SCHEMA.COLUMNS
来获取有关列序数位置的信息。如果您的查询确实必须围绕列序数位置建立,则您需要构建一个动态查询并为其提供正确的列名称以供引用。像这样的事情,可能是:There's no standard syntax that allows you to reference columns by their ordinal positions, not in Transact-SQL anyway.
You can obtain information about ordinal positions of columns by querying
INFORMATION_SCHEMA.COLUMNS
, though. And if your query must indeed be established around column ordinal positions, you'll need to build a dynamic query and supply it with the right column names to reference. Something like this, possibly:希望这有帮助...
Hope this helps...