title: Returning Number of affected rows from SQL Server with SQLAlchemy
body: <p>I use sqlalchemy to make changes to a table in SQL Server database, and would like to get back number of affected rows.</p>
  
  <p>I know there is <code>.rowcount</code> attribute to <code>ResultProxy</code>, but as, for example, <a href="https://stackoverflow.com/a/43538254/4077912">this</a> answer is demonstrating <code>.rowcount</code> is not necessarily the same as number of affected rows.</p>
  
  <p>SQL Server uses <a href="https://docs.microsoft.com/en-us/sql/t-sql/functions/rowcount-transact-sql" rel="nofollow noreferrer"><code>@@ROWCOUNT</code></a> to access number of affected rows from the previous statement execution.</p>
  
  <p>Is there a way to modify an sqlalchemy expression that uses insert / update statement to end with <code>SELECT @@ROWCOUNT</code>?</p>
  
  <p>For example, given:</p>
  
  <pre><code>from sqlalchemy import Table, Column, Integer, String, MetaData, create_engine
  
  url = 'mssql+pyodbc://dsn'
  engine = create_engine(url)
  
  metadata = MetaData()
  users = Table('users', metadata,
      Column('id', Integer, primary_key=True),
      Column('name', String),
      Column('fullname', String),
  )
  
  ins = users.insert().values(name='jack', fullname='Jack Jones')
  upd1 = users.update().values(fullname='Jack Doe').where(users.c.name == 'jack')
  upd2 = users.update().values(fullname='Jack Doe').where(users.c.name == 'jack')
  </code></pre>
  
  <p>I could prepend <code>SELECT @@ROWCOUNT</code> to an <code>update</code> statement:</p>
  
  <pre><code>sel = select([text('@@ROWCOUNT')])
  
  sql1 = sel.suffix_with(upd2)
  
  print(sql1.compile(engine, compile_kwargs={"literal_binds": True}))
  </code></pre>
  
  <p>Yielding "wrong" query:</p>
  
  <pre><code>SELECT @@ROWCOUNT UPDATE users SET fullname='Jack Doe' WHERE users.name = 'jack'
  </code></pre>
  
  <p>Trying to do the "right" thing:</p>
  
  <pre><code>sql2 = upd2.suffix_with(sel)
  </code></pre>
  
  <p>Raises <code>AttributeError</code> since 'Update' object has no attribute 'suffix_with'.</p>
  
  <p>So is there a way to get desired sql query:</p>
  
  <pre><code>UPDATE users SET fullname='Jack Doe' WHERE users.name = 'jack';
  SELECT @@ROWCOUNT
  </code></pre>
  
  <p>Using sql expression language without fully textual constructs.</p>