SQL 2005 INSERT statement inserts two rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sangam56
    New Member
    • Nov 2007
    • 68

    SQL 2005 INSERT statement inserts two rows

    Greetings.
    I suppose people out there can help in my problem.
    I am using sql server of microsoft visual studio 2005 itself. I have created a stored procedure which is give below. It works fine but it always inserts two records into the database. All columns of the two rows are same but the primary key is different which is no surprise. Please help me get out of this problem.

    --------------------------------
    [CODE] code goes here
    --------------------------------

    ALTER PROCEDURE dbo.myProcedure

    (
    @variable1 nvarchar(50),
    @variable2 nvarchar(500),
    @variable3 datetime,
    @variable4 datetime,
    @variable5 bit,
    @variable6 tinyint,
    @variable7 tinyint,
    @variable8 nvarchar(50),
    @variable9 nvarchar(50),
    @variable10 nvarchar(50),
    @variable11 nvarchar(256),
    @variable12 int,
    @variable13 nvarchar(50)
    )

    AS
    BEGIN
    Declare @temp1 int,
    @temp2 int,
    @temp3 int,
    @temp4 uniqueidentifie r;
    /*Set NOCOUNT on*/
    SET NOCOUNT ON ;



    SET @variable1=(SEL ECT value1 from table1 where <condition>)
    SET @variable2=(SEL ECT value2 from table1 where <condition>)

    SET @variable1=(SEL ECT value3 from table1 where <condition>)

    SET @variable1=(SEL ECT value4 from table1 where <condition>)


    INSERT INTO mytable (field1,field2, field3,field4,f ield5,field6,fi eld7,field8,fie ld9,field10,fie ld11,field12,fi eld13)
    VALUES (variable1,vari able2,variable3 ,variable4,vari able5,variable6 ,variable7,vari able8,variable9 ,variable10,var iable11,variabl e12,variable13) ;

    SELECT CAST(scope_iden tity() AS int);


    /*Reset NOCOUNT*/
    SET NOCOUNT OFF

    RETURN

    END

    [end of CODE]
    ----------------------------------------------------
    Here is my ASP.NET part.

    [CODE} code goes here
    ------------------------------------------------------------------
    int thisvalue1;
    string connStr = "Data Source=.\\SQLEX PRESS;AttachDbF ilename=|DataDi rectory|\\learn ing.mdf;Integra ted Security=True;U ser Instance=True";
    SqlConnection con = new SqlConnection(c onnStr);
    SqlCommand command = new SqlCommand("myP rocedure", con);
    command.Command Type = CommandType.Sto redProcedure;
    command.Paramet ers.Add(new SqlParameter("@ variable1",valu e1));
    command.Paramet ers.Add(new SqlParameter("@ variable2",valu e2));
    command.Paramet ers.Add(new SqlParameter("@ variable3",valu e3));
    command.Paramet ers.Add(new SqlParameter("@ variable4",valu e4));
    command.Paramet ers.Add(new SqlParameter("@ variable5",valu e5));
    command.Paramet ers.Add(new SqlParameter("@ variable6",valu e6));
    command.Paramet ers.Add(new SqlParameter("@ variable7",valu e7));
    command.Paramet ers.Add(new SqlParameter("@ variable8",valu e8));
    command.Paramet ers.Add(new SqlParameter("@ variable9",valu e9));
    command.Paramet ers.Add(new SqlParameter("@ variable10",val ue10));
    command.Paramet ers.Add(new SqlParameter("@ variable11",val ue11));
    command.Paramet ers.Add(new SqlParameter("@ variable12",val ue12));
    command.Paramet ers.Add(new SqlParameter("@ variable13",val ue13));


    con.Open();
    Int32 tempPrjID=(Int3 2)command.Execu teScalar();
    con.Close();
    thisPrjID=(int) tempPrjID;
    return thisPrjID;

    [end of code]
    =============== =============== =============== ========
  • pinaldave
    New Member
    • Dec 2007
    • 3

    #2
    Update: For SQL Server 2008 there is even better method of Row Construction, please read it here : SQL SERVER – 2008 – Insert Multiple Records Using One Insert Statement – Use of Row ConstructorThis is very interesting question I have received from new developer. How can I insert multiple values in table using only one insert? Now this is interesting question. When there are multiple records are to be inserted in the table following is the common way using T-SQL.


    should fix your problem.

    Comment

    • sangam56
      New Member
      • Nov 2007
      • 68

      #3
      I found the problem. It occurred because it happend like this:

      One: I put OnClick="Calcul ateMyData" in the aspx page.
      Second: I again put the delegate in Page_Init.
      So the event fired twice.
      I am happy I found the solution.

      Comment

      Working...