Question

Is it possible to insert something like a dataset/array in a stored procedure, then have a looping insert statement in the stored proc? I have a project that would insert 15 records in a table. Currently, I am doing the looping in my c# code. But there are times when 1 of the 15 records would not be inserted. I would like to do something that rollbacks or deletes are the inserted record when at least 1 is not inserted. I cannot do the rollback in c#, so is this possible in my sql stored procedure? Can you give an example? Thank you in advance.

Was it helpful?

Solution

One perfect solution for this is table value parameters. C# does support using them. Check an earlier answer of mine.

However, since you are using ancient technology (SQL 2000), it is not supported.

Do yourself a favor and upgrade.

The only restriction to this would be some application dependent upon the security model of SQL 2000. At my old company, we had an application that could not be upgraded because of this. Also, they were not willing to spend $. Catch 22!

I guess the question is how many processes are calling this stored procedure.

You could always do the following.

1 - Create a dynamic staging table in the C# application.

2 - Load the table with data from C#

3 - Pass the name of the dynamic staging table to the SPROC.

4 - In a transaction, do the following

BEGIN TRAN
INSERT INTO [TAGET TABLE]
SELECT * FROM [STAGING TABLE];

IF (@@ERROR <> 0)
BEGIN
ROLLBACK;
RETURN;
END

COMMIT

5 - Last but not least, drop the dynamic stating table.

I do not know if this gets you anywhere since c# should support transactions.

Example of ADONET C# program w/transactions

http://www.codeproject.com/Articles/10223/Using-Transactions-in-ADO-NET

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top