I'm having the following issue. I have a stored procedure that I use to either update or insert data into the DB called say UpdateData. It looks roughly something like this (though simplified):
CREATE PROCEDURE [dbo].[UpdateData]
@dataId as int,
@data as int,
AS
BEGIN
SET NOCOUNT ON;
declare @count int
select @count = (select COUNT(*) from DataTable data where data.id = @dataId)
if @count = 1
begin
update DataTable set data = @data from DataTable where data.id = @dataId
select 'Updated' [operation] , @@ROWCOUNT [count]
end
else
begin
insert into DataTable (id, data) values(@dataId, @data)
select 'Inserted' [operation] , @@ROWCOUNT [count]
end
END
I call this stored procedure from perl using DBI through a prepared statement iterating over my data. I then use a call to fetchrow_array to get the information about which operation was performed:
my $dbh = getDBHandle($debug);
foreach (@Data) {
$updateData->execute($->[0], $_->[1]);
my @row = $updateData->fetchrow_array;
my ($action, $count) = ($row[0], $row[1]);
print $row[0] .",$action, $count\n";
}
What happens is that once any update statement is run then subsequently all action inserted descriptions are truncated form 'inserted' to 'inserte '. I think this is happening because the string 'updated' has one less character then 'inserted' and once fetchrow_array is called with that string in the column it resets some kind of limit. If I make the difference between the two description strings more then one charter like say modify the store procedure to use 'Update' instead of 'Updated' (diffrence of two characters to 'Inserted')
select 'Update' [operation] , @@ROWCOUNT [count]
I get the error:
DBD::ODBC::st fetchrow_array failed: [Microsoft][ODBC SQL Server Driver]String data, right truncation (SQL-01004)
So in summary, the output looks like
1,Inserted,10
2,Updated,15
3,Inserte ,20
4,Updated,5
Any ideas on why the executions are not independent and what is the best way to solve this problem. I know I can make the actions the same but I would like a better solution.
EDIT: A follow up question. If the UpdateData procedure needed to call another procedure which also returned data. Is it possible in Perl to get both of the result sets. One coming from the inner procedure and one from the outer. Right now, >fetchrow_array only gets the inner result set.
EDIT 2: In regards to the original issue of data truncation I am wondering why does calling $updateData->finish after every execute not cause the width to be reset on every execute. IE
foreach (@Data) {
$updateData->execute($->[0], $_->[1]);
my @row = $updateData->fetchrow_array;
my ($action, $count) = ($row[0], $row[1]);
print $row[0] .",$action, $count\n";
$updateData->finish;
}