Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem With Rowcount while trying to bulk insert #128

Open
naresh561 opened this issue Jun 18, 2021 · 2 comments
Open

Problem With Rowcount while trying to bulk insert #128

naresh561 opened this issue Jun 18, 2021 · 2 comments

Comments

@naresh561
Copy link

naresh561 commented Jun 18, 2021

I have a sp

ALTER PROCEDURE [dbo].[sp_select_Test2] @Test [dbo].[TestTableType] READONLY 
AS
    BEGIN
        INSERT INTO dbo.test
        (
            Code,
            Text,
            Type
        ) 
        SELECT Name,
               Name,
               ID
        FROM   @Test		
    END;

I have this code

def executeSP(self, SPName, params=[]):
      with pytds.connect(self.server, self.dbname, self.user, self.dbpwd) as connection:
          connection.autocommit = True
          # Check how to create Secure query
          with connection.cursor() as cursor:
              try:
                  cursor.callproc(SPName, params)
                  rows = None
                  if(cursor.rowcount < 0):
                      rows = cursor.fetchall()
                  cursor.close()
                  return rows

              except Exception as e:
                  if(self.logger):
                      self.logger.error(
                          "error executeQuery {0}, {1}".format(SPName, e))
                  else:
                      print(e)
dbObj = DBWrapper(DB_NAME, DB_PASSWORD, DB_USERNAME, DB_SERVER)
ipData = [ ["abc", 1],["def", 2],["ghi", 3]]

The Problem occurs when I am trying to call the SP with following code

ipTvp = dbObj.makeTVP('dbo.TestTableType', ipData)
data = dbObj.executeSP('dbo.sp_select_Test2', [ipTvp])

the RowCount is -1 while it should be non negative value. Please help Me, Thank you.

@naresh561
Copy link
Author

naresh561 commented Jun 18, 2021

I found that in tds.py Line number 707

if done_count_valid:
            self.rows_affected = rows_affected
        else:
            self.rows_affected = -1

is causing the issue

@naresh561 naresh561 changed the title Problem With Rowcount while trying to insert Problem With Rowcount while trying to bulk insert Jun 18, 2021
@takoau
Copy link
Contributor

takoau commented Jul 18, 2022

@naresh561
For your reference: MS-TDS: DONEPROC

The DONE_COUNT flag in DONEPROC message is 0, simply states that bulk insert does not returns a row count, so it's natural to have -1 as return value.
0x10: DONE_COUNT. The DoneRowCount value is valid. This is used to distinguish between a valid value of 0 for DoneRowCount or just an initialized variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants