在我的表上执行UPDATE时出现sql错误

时间:2013-03-19 17:13:19

标签: c# sql sql-server-2008 tsql

Eror Message

  

消息2601,级别14,状态1,行1无法插入重复的键行   具有唯一索引'唯一键RTU'的对象'dbo.RTU_ADDRESS'。该   重复键值为(2,5,4,6,500,610,IUY)。该   声明已被终止。

表格的脚本

USE [hts]
GO

/****** Object:  Table [dbo].[RTU_ADDRESS]    Script Date: 03/19/2013 21:07:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[RTU_ADDRESS](
    [RowNo] [int] IDENTITY(1,1) NOT NULL,
    [StationId] [int] NULL,
    [SystemId] [int] NULL,
    [CCNumber] [int] NULL,
    [LineNumber] [int] NULL,
    [RTUNumber] [int] NULL,
    [SRTUNumber] [int] NULL,
    [Description] [char](100) NOT NULL,
    [SDescription] [char](100) NOT NULL,
    [Area_ID] [char](10) NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

索引脚本

USE [hts]
GO

/****** Object:  Index [Unique Key RTU]    Script Date: 03/19/2013 21:08:31 ******/
CREATE UNIQUE NONCLUSTERED INDEX [Unique Key RTU] ON [dbo].[RTU_ADDRESS] 
(
    [StationId] ASC,
    [SystemId] ASC,
    [CCNumber] ASC,
    [LineNumber] ASC,
    [RTUNumber] ASC,
    [SRTUNumber] ASC,
    [Area_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

C#代码:

SqlDataAdapter sqlAdapter = null;
                strSelectCmd = "SELECT RowNo FROM RTU_ADDRESS";
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.Connection = m_SqlConn;
                sqlCmd.CommandText = strSelectCmd;
                int i = Convert.ToInt32(sqlCmd.ExecuteScalar());
                sqlAdapter = new SqlDataAdapter();
                sqlAdapter.SelectCommand = sqlCmd;


                strInsCmd = "UPDATE RTU_ADDRESS SET RTUNumber='" + rtunum.Text + "', SRTUNumber='" + srtunum.Text + "', Description='" + desc.Text + "', SDescription='" + sdesc.Text + "' WHERE Area_ID='" + area_ID.Text + "' AND StationId='" + station.Text + "' AND SystemId='" + sys.Text + "' AND CCNumber='" + ccnum.Text + "' AND LineNumber='" + linenum.Text + "'";

                strErrorMsg = Utilities.ExecuteSQLCommand(m_SqlConn, strInsCmd);
                string check = "duplicate";
                bool d = check.Any(strErrorMsg.Contains);
                if (d == true)
                {
                    MessageBox.Show(" Duplicates Entries Found", "Database Insertion");
                }
                else if (string.IsNullOrEmpty(strErrorMsg) == false)
                {
                    MessageBox.Show(this, "Error!Unable to Add New RTU Configurations!", "RTU Configurations error");
                }
                else
                {
                    d = false;
                    this.Close();
                }

我现在迷失了该做什么,我没有提出任何重复。

我只想更新RTUNumber,SRTUNumber,Description和SDescription,同时保持我的stationID,systemID,CCNumber,LineNumber和Area_ID不变。请帮忙

更新: 选择声明

SELECT * FROM RTU_ADDRESS WHERE Area_ID='IUY ' AND StationId='2' AND SystemId='5' AND CCNumber='4' AND LineNumber='6'

给我2个结果,但RTUNumber和SRTUNumber是不同的。

请帮忙

2 个答案:

答案 0 :(得分:0)

嗯,很明显,您尝试进行的更新会在您的表中生成重复内容。您是否尝试根据您尝试更新的值对表进行选择?你很可能会找到一条现有记录......

答案 1 :(得分:0)

查看更新语句的where子句中的条件,如果记录具有相同的AreaId,SystemId,StationId,CCNumber和LineNumber,则会出现此错误,因为您尝试使用相同的RTUNumber更新它们和SRTUNumber。你可以通过运行这个sql来检查这个:

SELECT AreaId, SystemId, StationId, CCNumber, LineNumber, COUNT(*)
FROM RTU_ADDRESS
GROUP BY AreaId, SystemId, StationId, CCNumber, LineNumber
HAVING COUNT(*) > 1

如果返回任何行,则会出现“重复”错误。

事实上,您编辑的版本说的就是这种情况。虽然这两个记录可能有不同的SRTUNumbers和RTUNumbers,但您尝试在更新语句中为它们提供相同的SRTUNumber和RTUNUmber。