Note that there are some explanatory texts on larger screens.

plurals
  1. POwhat is a reliable way to return number of records inserted from a stored procedure
    primarykey
    data
    text
    <p>I am using <em>INSERT Trigger</em> on that table. Once trigger is executed (it update the table if a condition is meet), that is where the problem is.</p> <pre><code> int records = sc.ExecuteNonQuery(); // works ok if trigger does not update the record </code></pre> <p>The above code always ruturns -1 if I leave <code>SET NOCOUNT ON;</code> in the stored procedure itself. If I remove it, I get correct result but if trigger does update the record, then wrong result. I sometime get 10 or a different number. My Trigger looks like this</p> <pre><code>UPDATE students SET status = 'Inactive' FROM Inserted i INNER JOIN students T2 ON i.sname = T2.sname AND i.id &lt;&gt; t2.id </code></pre> <p>That means it can return more than one record (esp in test cases). Can someone tell me what is the cure? I am open to use Functions if that solves the problem or any better approach. </p> <p>Thanks</p> <h2>Adding Insert SP</h2> <pre><code>CREATE PROCEDURE sp_InsertSudent -- Add the parameters for the stored procedure here @student_name varchar(25) = null, @status varchar(20) = null, @renew varchar(15) = null, @edate datetime = null AS BEGIN --SET NOCOUNT ON; insert into students VALUES(@student_name,@status,@renew,@edate) END GO </code></pre> <p>Note: I am looking for an error because the fields are picked from Excel. if any field is in wrong format or empty, the Insert SP will produce error. I must convey that error to the user.</p> <h2>Adding Actual SP</h2> <p>So the whole problem is in the SP. If I remove it, everything works fine. Here is my actual SP</p> <pre><code>UPDATE CustomerTbl SET [Account Status] = 'Inactive', [End Date] = DateAdd(day,-1,[Supplier End Date]), [EnrollmentStatus] = 'Waiting' WHERE OET.[Account No] = (SELECT [Account No] FROM CustomerTbl WHERE id = @@identity) AND id &lt;&gt; @@identity </code></pre> <p>The logic is the same as above but stated differently. The ExecuteNonQuery oupts the result of this trigger than than the actual storedprocedure, so what is he cure? Can suppress its output somehow.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload