使用存储过程更新表信息

时间:2012-12-18 14:01:44

标签: asp.net sql-server visual-studio stored-procedures

我有一个带有资产列表的数据库及其信息来自表“tbl_info”,解决方案在sql中检出但是在提交后它不会更新表。我不知道我哪里错了。我对存储过程和w / s的相当新。

这是我的代码。

CREATE PROCEDURE [update_TBL_INFO_4]
(@ASSET_NUMBER  [nvarchar],
 @PORT_NUMBER   [nvarchar],
 @MACHINE_NAME  [nvarchar],
 @TYPE  [nvarchar],
 @BRANCH    [nvarchar],
 @BUILDING_NUMBER   [nvarchar],
 @FLOOR     [nvarchar],
 @ROOM_NUMBER   [nvarchar],
 @TELEPHONE_NUMBER  [nvarchar],
 @USERS     [nvarchar],
 @SERIAL    [nvarchar])

AS 
BEGIN
UPDATE [datasystems].[dbo].[TBL_INFO] 

SET  [ASSET_NUMBER]  = @ASSET_NUMBER,
 [PORT_NUMBER]   = @PORT_NUMBER,
 [MACHINE_NAME]  = @MACHINE_NAME,
 [TYPE]  = @TYPE,
 [BRANCH]    = @BRANCH,
 [BUILDING_NUMBER]   = @BUILDING_NUMBER,
 [FLOOR]     = @FLOOR,
 [ROOM_NUMBER]   = @ROOM_NUMBER,
 [TELEPHONE_NUMBER]  = @TELEPHONE_NUMBER,
 [USERS]     = @USERS,
 [SERIAL]    = @SERIAL 

WHERE 
( [ASSET_NUMBER]     = @ASSET_NUMBER AND
 [PORT_NUMBER]   = @PORT_NUMBER AND
 [MACHINE_NAME]  = @MACHINE_NAME AND
 [TYPE]  = @TYPE AND
 [BRANCH]    = @BRANCH AND
 [BUILDING_NUMBER]   = @BUILDING_NUMBER AND
 [FLOOR]     = @FLOOR AND
 [ROOM_NUMBER]   = @ROOM_NUMBER AND
 [TELEPHONE_NUMBER]  = @TELEPHONE_NUMBER AND
 [USERS]     = @USERS AND
 [SERIAL]    = @SERIAL)
END
GO

1 个答案:

答案 0 :(得分:1)

由于您的WHERE子句检查所有的参数,如果它们中的任何一个不同(即,当您传递更新的值时),WHERE不会找到任何匹配的记录。

如果您定义了主键,请在WHERE子句中使用它并更新其余部分。否则,您可能需要传入旧值以找到适当的匹配。

<强>更新

如果主键为ASSET_NUMBER,则您需要更新不是主键的字段,并在WHERE子句中单独使用主键,如下所示:

UPDATE 
    [datasystems].[dbo].[TBL_INFO] 
SET  
    [PORT_NUMBER] = @PORT_NUMBER,
    [MACHINE_NAME] = @MACHINE_NAME,
    [TYPE] = @TYPE,
    [BRANCH] = @BRANCH,
    [BUILDING_NUMBER] = @BUILDING_NUMBER,
    [FLOOR] = @FLOOR,
    [ROOM_NUMBER] = @ROOM_NUMBER,
    [TELEPHONE_NUMBER] = @TELEPHONE_NUMBER,
    [USERS] = @USERS,
    [SERIAL] = @SERIAL 
WHERE 
    [ASSET_NUMBER] = @ASSET_NUMBER