根据当前行中另一列的参数,从表达式更新具有不同值的所有行中的列
Update a Column in All Rows with Different Values from an expression based on a parameter from another Column in the Current Row
拜托,我最近将我的数据库从 MS Access 更改为 SQL 服务器 Express,Access 是一个非常棒的小型数据库,适用于单个用户,具有非常简单的 VBA 我错过的功能在 SQL 服务器中!
在我的旧访问数据库中,我有 [Account] table 和一个子过程,该子过程使用此表达式的结果更新 table 中所有行中的字段:
[SortOrder] = [AccountNumber] * (10 ^ (Len(MaximumAccountNumber) - Len([AccountNumber])))
where MaximumAccountNumber is a Variable represent the Max AccountNumber in the table.
我已经搜索了很多天的解决方案,但没有一个示例可以让我了解如何使用同一行中的列中的值来计算该行中另一列的结果等等table 中的所有行就像在以下 VBA 代码中一样:
Do while Not rst.EOF
rst.Edit
rst![Field1] = rst![Field2] * ( 10 ^ ( (Len(MaximumAccountNumber) - Len(rst![Field2]) ) )
rst.Update
rst.MoveNext
Loop
请问如何在 SQL 服务器 T-SQL 中有效地实现这样的更新而不使用游标 因为 [=35 中的行计数=] 可以 达到 > 100,000?
拜托,我想通过创建一个 SP 来做到这一点,我可以在每次插入新帐户后触发它(触发器)以重新计算 table 中所有行的排序顺序,如以下:
CREATE PROCEDURE [dbo].[SortingOrder]
@MaxOrder Numeric(38,0) = 0,
@Digits int = 0,
As
BEGIN
set @MaxOrder = (select MAX([AccNumber]) from Account)
set @Digits = (select LEN(@MaxOrder))
Update dbo.Account
Set [SortOrder] = (Select ([AccNumber] * (POWER(10 ,(@Digits -
LEN([AccNumber]))) from [Account] )
END
GO
如本示例中所示 Table [帐户]:
AccID AccNumber SortOrder
----- --------- ---------
023 23 2300
054 243 2430
153 5434 5434
但是当插入新记录时,我希望将所有行的排序顺序更新为具有相同数字计数的数字,基于 10 次方(最大 AccNumber 的长度),如下所示:
AccID AccNumber SortOrder
----- --------- ---------
023 23 230000000
054 243 243553000
153 5434 543400000
233 432345625 432345625
试试这个:
Table 架构:
CREATE TABLE Account(AccID INT,AccNumber BIGINT,SortOrder BIGINT)
INSERT INTO Account VALUES(23,23,23)
INSERT INTO Account VALUES(54,254,254)
INSERT INTO Account VALUES(125,25487,25487)
T-SQL 查询:
DECLARE @MaxValLen INT
SELECT @MaxValLen = LEN(MAX(AccNumber)) FROM Account
UPDATE Account
SET SortOrder = AccNumber * POWER(10,@MaxValLen - LEN(AccNumber))
输出:
| AccID | AccNumber | SortOrder |
|-------|-----------|-----------|
| 23 | 23 | 23000 |
| 54 | 254 | 25400 |
| 125 | 25487 | 25487 |
拜托,我最近将我的数据库从 MS Access 更改为 SQL 服务器 Express,Access 是一个非常棒的小型数据库,适用于单个用户,具有非常简单的 VBA 我错过的功能在 SQL 服务器中! 在我的旧访问数据库中,我有 [Account] table 和一个子过程,该子过程使用此表达式的结果更新 table 中所有行中的字段:
[SortOrder] = [AccountNumber] * (10 ^ (Len(MaximumAccountNumber) - Len([AccountNumber])))
where MaximumAccountNumber is a Variable represent the Max AccountNumber in the table.
我已经搜索了很多天的解决方案,但没有一个示例可以让我了解如何使用同一行中的列中的值来计算该行中另一列的结果等等table 中的所有行就像在以下 VBA 代码中一样:
Do while Not rst.EOF
rst.Edit
rst![Field1] = rst![Field2] * ( 10 ^ ( (Len(MaximumAccountNumber) - Len(rst![Field2]) ) )
rst.Update
rst.MoveNext
Loop
请问如何在 SQL 服务器 T-SQL 中有效地实现这样的更新而不使用游标 因为 [=35 中的行计数=] 可以 达到 > 100,000?
拜托,我想通过创建一个 SP 来做到这一点,我可以在每次插入新帐户后触发它(触发器)以重新计算 table 中所有行的排序顺序,如以下:
CREATE PROCEDURE [dbo].[SortingOrder]
@MaxOrder Numeric(38,0) = 0,
@Digits int = 0,
As
BEGIN
set @MaxOrder = (select MAX([AccNumber]) from Account)
set @Digits = (select LEN(@MaxOrder))
Update dbo.Account
Set [SortOrder] = (Select ([AccNumber] * (POWER(10 ,(@Digits -
LEN([AccNumber]))) from [Account] )
END
GO
如本示例中所示 Table [帐户]:
AccID AccNumber SortOrder
----- --------- ---------
023 23 2300
054 243 2430
153 5434 5434
但是当插入新记录时,我希望将所有行的排序顺序更新为具有相同数字计数的数字,基于 10 次方(最大 AccNumber 的长度),如下所示:
AccID AccNumber SortOrder
----- --------- ---------
023 23 230000000
054 243 243553000
153 5434 543400000
233 432345625 432345625
试试这个:
Table 架构:
CREATE TABLE Account(AccID INT,AccNumber BIGINT,SortOrder BIGINT)
INSERT INTO Account VALUES(23,23,23)
INSERT INTO Account VALUES(54,254,254)
INSERT INTO Account VALUES(125,25487,25487)
T-SQL 查询:
DECLARE @MaxValLen INT
SELECT @MaxValLen = LEN(MAX(AccNumber)) FROM Account
UPDATE Account
SET SortOrder = AccNumber * POWER(10,@MaxValLen - LEN(AccNumber))
输出:
| AccID | AccNumber | SortOrder |
|-------|-----------|-----------|
| 23 | 23 | 23000 |
| 54 | 254 | 25400 |
| 125 | 25487 | 25487 |