Multiple Inserts Using SqlCommand Object  
Author Message
Steve_King





PostPosted: .NET Framework Data Access and Storage, Multiple Inserts Using SqlCommand Object Top

I've a case the of a stored procedure that is executed using a SqlCommand object using ExecuteNonQuery. I expect to insert one record but it inserts 22 exact records because I'm using and identity field as the key. I used debug to test that the code is only executed once and reviewed the stored procedure that passes every parameter to be inserted. I'm perplexed and could use some good recommendations as to why this might be happening.

ALTER PROCEDURE dbo.spProjectAdd

)
AS

INSERT INTO dbo.vtblProjID (ProjID, ProjName, TrbReq, CcbReq, PrincipalContact, LocalDir,
DeployTarget, ApplName)


FROM dbo.vtblProjID



.NET Development18  
 
 
SvenC





PostPosted: .NET Framework Data Access and Storage, Multiple Inserts Using SqlCommand Object Top

Hi,

your INSERT syntax is not like what you intend I suppose. Use this:

INSERT INTO dbo.vtblProjID (ProjID, ProjName, TrbReq, CcbReq, PrincipalContact, LocalDir,
DeployTarget, ApplName)


--
SvenC


 
 
MihaiBejenariu





PostPosted: .NET Framework Data Access and Storage, Multiple Inserts Using SqlCommand Object Top

Your statement inserts a row for each record in vtblProjID table.

Rewrite your statement as follows:

INSERT INTO dbo.vtblProjID (ProjID, ProjName, TrbReq, CcbReq, PrincipalContact, LocalDir, DeployTarget, ApplName)

I don’t understand why you are using INSERT/SELECT as you don’t select any row from vtblProjID table.

Is it working now


 
 
Steve_King





PostPosted: .NET Framework Data Access and Storage, Multiple Inserts Using SqlCommand Object Top

You were both correct of course. I have a tendency to always use the select statement, which works just fine until I add the FROM statement. Why I overlooked the answer to my question I'll never know. Thanks!