I'm invoking SQL commands as well as stored procedures from python. Some of the commands work well. In fact, I've done this kind of thing dozens of times with no problem.
Here's an example of some working code in the same file where I have the problem:
cursor.execute(r'select top 10 * from xyz')
for result in cursor.fetchall():
last_predecessor = result[0]
last_successor = result[1]
print(last_predecessor, last_successor)
This works great!
I have this stored procedure:
alter PROCEDURE make_new_table
AS
BEGIN
drop table if exists abc
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
CREATE TABLE abc(
[id] [bigint] IDENTITY(1,1) NOT NULL,
[ida_last_update_date] [datetime] NULL,
[ida_last_update_source_file] [nvarchar](500) NULL,
[ID_RSSD_PREDECESSOR] [nvarchar](500) NULL,
[ID_RSSD_SUCCESSOR] [nvarchar](500) NULL,
[D_DT_TRANS] [date] NULL,
[TRNSFM_CD] [int] NULL,
[ACCT_METHOD] [nvarchar](500) NULL,
[DT_TRANS] [date] NULL
) ON [PRIMARY]
END
I can invoke this from the ssms query window with
exec make_new_table
Also works fine.
But when I try to execute this from python with:
cursor.execute("exec make_new_table ")
It fails. (Tried it with and without the trailing space) No error! It just doesn't do anything.
I also tried to invoke:
cursor.execute('drop table if exists xyz ')
This also fails - doesn't drop the table, but produces no error (and it shouldn't).
So is there some limit to what can be done from pyodbc? Can we not drop and create tables?
question from:
https://stackoverflow.com/questions/65900690/some-sql-commands-and-stored-procedures-work-in-ssms-but-not-from-a-python-call 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…