Skip to content

Instantly share code, notes, and snippets.

@nathairtras
Created May 19, 2019 02:24
Show Gist options
  • Select an option

  • Save nathairtras/ffdc94f085d1bd8df09386206889b0d0 to your computer and use it in GitHub Desktop.

Select an option

Save nathairtras/ffdc94f085d1bd8df09386206889b0d0 to your computer and use it in GitHub Desktop.

Revisions

  1. nathairtras created this gist May 19, 2019.
    26 changes: 26 additions & 0 deletions pytds-tvp-demo.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import os
    import pytds

    # Grabbing the user and password from environment variables
    # Load these however you would like
    server = os.environ["MSSQL_SERVER"]
    user = os.environ["MSSQL_USER"]
    password = os.environ["MSSQL_PASS"]

    # Fake some rows, these could be from CSV
    rows = [
    ["abc"],
    ["def"],
    ["ghi"],
    ]

    # Make a connection
    with pytds.connect(server=server, user=user, password=password, autocommit=True) as cnx:
    # Create a cursor
    with cnx.cursor() as cur:
    # Create the TVP from the rows
    tvp = pytds.TableValuedParam(type_name='dbo.StringList', rows=rows)
    # Use the TVP in a query / exec
    cur.execute('SELECT * FROM %s', (tvp,))
    # Prove we got rows back
    print(cur.fetchall())