Is it possible to pass values to a table type parameter from PYODBC to SQL Server?

Robert Criqui

I created a test type as a table with the below columns:

CREATE TYPE [dbo].[TestType] AS TABLE
                                (
                                    [TestField] [varchar](10) NULL,
                                    [TestField2] [int] NULL
                                )

I then created a stored procedure the takes that table type as a parameter.

CREATE PROCEDURE TestTypeProcedure (@tt TestType READONLY)
AS
    SELECT *
    FROM @tt;

My goal is to be able to pass something like a list of lists as the parameter for the table type. Is that even possible?

myList = [['Hello!', 1], ['Goodbye!', 2]]
....
cursor.execute('{{Call {TestTypeProcedure} ({?})}}', myList)
Gord Thompson

pyodbc.ProgrammingError: ('The SQL contains 1 parameter markers, but 2 parameters were supplied', 'HY000')

You are getting that error because a table-valued parameter is a list of iterables (preferably tuples) ...

my_tvp = [('Hello!', 1), ('Goodbye!', 2)]
print(f"my_tvp contains {len(my_tvp)} row(s)")
# my_tvp contains 2 row(s)

... and if you pass that directly to .execute() then each row is interpreted as a parameter value:

sql = "{CALL TestTypeProcedure (?)}"
params = my_tvp
print(f"calling SP with {len(params)} parameter value(s)")
# calling SP with 2 parameter value(s)
crsr.execute(sql, params)  # error

Therefore, you need to wrap your tvp inside a tuple to make it a single parameter value

sql = "{CALL TestTypeProcedure (?)}"
params = (my_tvp, )  # tuple containing a single tvp "object"
print(f"calling SP with {len(params)} parameter value(s)")
# calling SP with 1 parameter value(s)
crsr.execute(sql, params)  # no error

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it possible to Pass Empty Parameter to sql server from code behind?

Can you pass table input parameter to SQL Server from Python

How to pass values from other table in SQL Server

SQL Agent job to select a value from table and pass the values as input parameter to execute stored procedure

How to pass User defined table type values to Value field for the Execute Procedure in SQL Server Management Studio?

Is it possible to pass parameter inside With Clause in SQL Server SSIS Job?

How to get the values from the Input parameter of (Object Type Table) in procedure in Oracle PL SQL?

How to calculate table values dynamically and find Min and Max target from parameter in SQL Server

SQL Server temp table not available in pyodbc code

Pass Default value to Table Valued parameter - SQL Server

Delphi - pass table valued parameter to SQL Server stored procedure

Is it possible to write a function in SQL Server 2000 accepting input parameter as a table?

Is it possible to pass parameter in the cognos by SQL?

Is it possible to pass in Table-Valued Parameters to PYODBC query?

Pass multiple string values as parameter from server side to javascript function

SQL Server values from Detail table

How to count the number of values in a table with 2 columns as the parameter on SQL Server?

Authenticate from Linux to Windows SQL Server with pyodbc

Get data from pandas into a SQL server with PYODBC

Speed up inserts into SQL Server from pyodbc

Pyodbc not returning error from SQL Server

Extracting data from SQL Server using pyodbc

Table get locked when called an SQL Server SP from pyodbc Python

Is it possible to pass a NULL into an SQL Server Stored Procedure from Typescript

Inserting values from one table into another table in SQL Server

SQL function, that returns values from table by using parameter

How to pass optional column in TABLE VALUE TYPE in SQL from ADF

SQL Server How to Select * from table_name where in (@parameter )

Retrieving SQL Server data using pyodbc is returning unexpected data values