'How to create SQL Server user define-function using python

I am trying to create a simple user-defined SQL Server function using pyodbc but the function is not created on the database. If I execute the same query within SQL Server then it is created.

import pyodbc

sql = """
CREATE FUNCTION east_or_west (
    @long DECIMAL(9,6)
)
RETURNS CHAR(4) AS
BEGIN
    DECLARE @return_value CHAR(4);
    SET @return_value = 'same';
    IF (@long > 0.00) SET @return_value = 'east';
    IF (@long < 0.00) SET @return_value = 'west';
    
    RETURN @return_value
END;
"""

con = pyodbc.connect('Driver=ODBC Driver 17 for SQL Server;Server='+server+';Database='+dbname+';Trusted_Connection=yes;')
cursor = con.cursor()
cursor.execute(sql) 


Solution 1:[1]

Do you need create

CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;

CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dale K