-3

大家好,我在 sqlserver 表中有大约 5 个值,其中一个字段是“orderid”,我想更新其中的 1 个值并更改其订单 ID。所以例如

apples is 1
oranges is 2
bananas is 3
pears 4
grapes 5
etc etc

我想更新香蕉,所以它的订单 id 是 2,但它也更新了表的其余部分,所以 orderid 就像它是一个唯一的字段

任何人都可以建议最好的方法来解决这个问题,使用存储过程或 asp.net c#

4

3 回答 3

0
Update tablename set orderid=2 where name='bananas'
于 2013-10-14T13:23:17.063 回答
0
UPDATE TableName 
SET orderid = 2 
WHERE Name = 'bananas'
于 2013-10-14T13:24:11.683 回答
0

由于您正在使用新 ID 更新现有项目的顺序。确保已经持有该订单的商品获得订单 ID。可能是正在更新的商品的订单 ID。所以这是一种可以根据您的要求执行的交换操作。

这是交换项目 orderid 的代码:

-- Create test table
Create table Orders(
OrderId int, 
Name varchar(100)
)
Go

-- Create data in table
insert Orders(
OrderId,
Name
)
select 1 as OrderId,'Apple' as Name
Union
select 2 as OrderId,'Oranges' as Name
Union
select 3 as OrderId,'Bananas' as Name
Union
select 4 as OrderId,'Pears' as Name
Union
select 5 as OrderId,'Grapes' as Name

Go

-- Create proc 
Alter Proc SetOrder
@OrderId as int, 
@Name as varchar(100)

AS

-- Swap/Rearrange ordering of old order of item with requested Order and item

Update Orders set OrderId = (Select TOP 1 Orderid from Orders where Name = @Name)
Where Name = (Select TOP 1 Name from Orders where orderId = @OrderId)

-- Now update the request item.
Update Orders set orderId = @OrderId
Where Name = @Name

Go

-- Test
Execute dbo.SetPriority 2, 'Bananas'
于 2013-10-14T13:43:01.043 回答