'How to call SQL functions in pyodbc
I have recently been working on database project, including SQL. I have written a simple function in SQL Server that counts the sum of some one of the columns.
ALTER FUNCTION [dbo].[Count_kontrakty_sum](@id_z INT)
RETURNS INT
AS
BEGIN
RETURN (SELECT SUM(Kontrakty.wysokosc)
FROM Kontrakty
WHERE id_zawodnika = @id_z)
END
When called as a database query, it works perfectly
PRINT dbo.Count_kontrakty_sum(66);
But when I want to call it from my Python program it gets a bit different.
I have tried a lot of different formats of calling this func, but every time I get some error:
cursor = conn.cursor()
cursor.execute('{CALL Count_kontrakty_sum?}', 60)
record = cursor.fetchall()
I get this error:
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0]Syntax error, permission violation, or other nonspecific error (0) (SQLPrepare)')
cursor.execute('exec dbo.Count_kontrakty_sum?', 66)
results in:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Could not find stored procedure 'dbo.Count_kontrakty_sum@P1'. (2812) (SQLExecDirectW)")
Is there another way to call SQL function in pyodbc ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|