1

我正在尝试将数据表中的多行插入和更新到数据库表中。以下是将 Datatable 传递给存储过程的代码:

 DataTable dtUserRights = new DataTable();
        PropertyInfo[] piT = typeof(clsRightsAL).GetProperties(); //Gets all column values with there datatype
        foreach (PropertyInfo pi in piT)
        {
              //create a datacolumn for each property
              DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
              dtUserRights.Columns.Add(dc);

        }

        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            for (int item = 0; item < objUserRightsALLst.Count; item++)
            {
                DataRow dr = dtUserRights.NewRow();


                for (int property = 0; property < dtUserRights.Columns.Count; property++)
                {
                    dr[property] = piT[property].GetValue(objUserRightsALLst[item], null);
                }

                dtUserRights.Rows.Add(dr);
            }
            objCmd = new SqlCommand();
            objCmd.CommandText = "sp_AD_InsertUpdateGroupRights";
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Connection = con;
            objCmd.Parameters.Clear();
            SqlParameter tableParameter =  objCmd.Parameters.AddWithValue("@GroupRightslist", dtUserRights);
            tableParameter.SqlDbType = SqlDbType.Structured;
 intCount = objCmd.ExecuteNonQuery();

            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
        catch (Exception e)
        {
            throw e;
        }

以下是我正在调用的 SP:

CREATE PROCEDURE [dbo].[sp_AD_InsertUpdateGroupRights]  
@GroupRightslist as  [dbo].[GroupRightsList] ReadOnly  
AS  
BEGIN   
if Not Exists(Select intID from tblADGroupRights where intID = (Select l.intID from @GroupRightslist l))  
Begin  
  insert into tblADGroupRights(IntGroupID,intMenuID,blAdd,blEdit,blView,blGrant,blStatus,strUserCreated
) Select l.IntGroupID,l.intMenuID,l.blAdd,l.blEdit,l.blView,l.blGrant,l.blStatus,
l.strBy from GroupRightslist l   

End     
 else    

  begin  
update A set blAdd = l.blAdd,blEdit = l.blEdit,blView = l.blView,blGrant = l.blGrant   
 FROM  tblADGroupRights A INNER JOIN @GroupRightslist l on A.intID = l.intID  
where A.intID = l.intID     
end
END

当我将单行传递给存储过程时,数据被插入,但是当数据表有多个记录时,我得到了异常:

子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

而且我的更新查询根本不起作用。

我的用户定义表类型:

CREATE TYPE [dbo].[GroupRightsList] AS TABLE(
[intID] [int] NULL,
[intProfileID] [int] NULL,
[IntGroupID] [int] NULL,
[intMenuID] [int] NULL,
[blAdd] [bit] NULL,
[blEdit] [bit] NULL,
[blView] [bit] NULL,
[blGrant] [bit] NULL,
[blStatus] [bit] NULL,
[strBy] [nvarchar](max) NULL
) 

我知道,当 Select 查询返回超过 1 个值并且我们没有正确处理它时,通常会引发此异常,但在我使用用户类型表的情况下,我看到许多网站以相同的方式显示它我已经做好了。

4

1 回答 1

1

子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

这纯粹是一个sp错误。这意味着在您的 sp 中,您有一个子查询返回多个不允许的记录。很可能下面是这条线

Select intID from tblADGroupRights where intID = (Select l.intID from @GroupRightslist l)

要么将其更改为

Select intID from tblADGroupRights where intID = (Select Top 1 l.intID from @GroupRightslist l)

或者

Select intID from tblADGroupRights where intID in (Select l.intID from @GroupRightslist l)
于 2013-08-12T05:52:51.690 回答