I have an SP that work very well when called from SSMS. but when I call it from my application
that written in native C++ and use ODBC for connecting to database, the operation return no error but actually do nothing in the database.
My SP read some values from some temporary tables and either insert them in database or update them.
I had a transaction in SP that guard all the code of SP, I hardly debug my SP and find that function will return in first insert or update and so do nothing. So I remove that transaction and function partly worked, I mean it add some of the items but leave some of them there without adding them to the database.
here is a skeleton of my SP:
--BEGIN TRANSACTION
DECLARE @id bigint, @name nvarchar(50)
DELETE FROM MyTable WHERE NOT( id IN (SELECT id from #MyTable) )
DECLARE cur1 CURSOR FOR SELECT id, name FROM #MyTable
OPEN cur1
WHILE 1 != 0
BEGIN
FETCH cur1 INTO @id, @name
IF @@FETCH_STATUS != 0 BREAK;
UPDATE MyTable SET [Name]=@name WHERE [id]=@id
IF @@ROWCOUNT = 0
INSERT INTO MyTable ( ID, Name ) VALUES ( @id, @name )
END
CLOSE cur1
DEALLOCATE cur1
--COMMIT TRANSACTION
I solve my problem by adding
SET NOCOUNT ONto start of my SP, so I think when SQL return multiple result set as a result of executing my SQL, ODBC close or cancel the command upon receiving first result set.