Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server存储过程正在插入两条记录_Sql_Sql Server_Stored Procedures - Fatal编程技术网

SQL Server存储过程正在插入两条记录

SQL Server存储过程正在插入两条记录,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我的存储过程应该克隆该项一次,但它正在创建两条克隆记录 我的C#程序有一个按钮调用这个方法。调试只触发一次,并返回第二个克隆项的neweventid protected void cloneEvent(object sender, EventArgs e) { using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnection"].C

我的存储过程应该克隆该项一次,但它正在创建两条克隆记录

我的C#程序有一个按钮调用这个方法。调试只触发一次,并返回第二个克隆项的neweventid

protected void cloneEvent(object sender, EventArgs e)
{
        using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString))
        {
            using (SqlCommand myCommand = new SqlCommand("addRecycleEventAcceptedMaterialsClone"))
            {
                //object returnValue;

                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Connection = myConnection;
                myCommand.Parameters.AddWithValue("@event_id", qsEventId);
                myConnection.Open();
                myCommand.ExecuteNonQuery();

                //returnValue = myCommand.ExecuteScalar();
                NewEventID = (int)myCommand.ExecuteScalar();
            }
        }
        Response.Redirect("eventDetail.aspx?eventid=" + NewEventID);
    }
我检查了,没有重复的事件id,它是主键/标识列

ALTER PROCEDURE [dbo].[addRecycleEventAcceptedMaterialsClone] 
    -- Add the parameters for the stored procedure here
    --Pass in the original event_id
    @event_id int
    --@newEvent_id INT OUTPUT
AS
BEGIN
   -- SET NOCOUNT ON added to prevent extra result sets from
   -- interfering with SELECT statements.
   SET NOCOUNT ON;

   INSERT INTO RECYCLE_EVENT (event_nm, start_dt, end_dt, start_tm, end_tm,
                              website_ad, address_ad, city_nm, state_cd, zip_cd,
                              county_id, description_ds, moreinfo_ds, 
                              latitude, longitude, phone_nr)
     SELECT 
        event_nm, start_dt, end_dt, start_tm, end_tm,
        website_ad, address_ad, city_nm, state_cd, zip_cd,
        county_id,  description_ds, moreinfo_ds,
        latitude, longitude, phone_nr 
    FROM 
        RECYCLE_EVENT
    WHERE
        event_id = @event_id

    --SET @newEvent_id = IDENT_CURRENT('RECYCLE_EVENT');    
    --SELECT @newEvent_id = SCOPE_IDENTITY()
      SELECT CAST(scope_identity() AS int);
    --SELECT @newEvent_id AS newEventID

--NEW CLONED EVENT HAS BEEN ADDED AND A NEW ID (newEventID) has been generated.
--NOW INSERT all materials accepted for the @event_id and insert them into the RECYCLER_EVENT_MATERIALS_ACCEPTED table
--Give it the newEventID

INSERT INTO RECYCLER_EVENT_MATERIALS_ACCEPTED ( 
        event_id,
        material_type_id,
        acceptance_cd,
        residential_fl,
        commercial_fl,
        service_type_cd,
        end_dt,
        event_material_cloned_id
        )
        SELECT 
        IDENT_CURRENT('RECYCLE_EVENT'),
        material_type_id,
        acceptance_cd,
        residential_fl,
        commercial_fl,
        service_type_cd,
        end_dt,
        event_material_id
        FROM RECYCLER_EVENT_MATERIALS_ACCEPTED
        where event_id = @event_id

        --Grab all the records that have the old event_material_id
        --insert a new row using the same county id and the new event_material_id
        --eventid-76: material-id's: 18, 21, 22
        --eventid-124: material-id's: 24, 25, 26, 27, 28, 29, 30, 31, 32, 33

        INSERT INTO RECYCLER_EVENT_COUNTY_SERVED (
            county_id,
            event_material_id )
            SELECT s.county_id, a.event_material_id
            FROM RECYCLER_EVENT_COUNTY_SERVED s
            INNER JOIN RECYCLER_EVENT_MATERIALS_ACCEPTED a ON s.event_material_id = a.event_material_cloned_id

谢谢

您从代码中执行它两次,首先通过
ExecuteNonQuery
,然后通过
ExecuteScalar
,这样插入就不是一次而是两次了。

您从代码中执行它两次,首先通过
ExecuteNonQuery
,然后通过
ExecuteScalar
,这样就有了两次而不是一次的插入。

您将执行两次存储过程

 myCommand.ExecuteNonQuery();
 NewEventID = (int)myCommand.ExecuteScalar();

两者都调用insert,我将注释掉第一行。

您将执行两次存储过程

 myCommand.ExecuteNonQuery();
 NewEventID = (int)myCommand.ExecuteScalar();

这两个都称为插入,我将注释掉第一行。

谢谢您的推荐!没问题!很乐意帮忙。谢谢你的推荐!没问题!很乐意帮忙。