I have multiple .sql files that have DROP IF EXISTS and CREATE TABLE statements that I want to execute automatically without having to click on them through python. I'm getting errors using this script:
import os
import fnmatch
for root, dirnames, filenames in os.walk("C:/Users/user/Desktop/Generated"):
for filename in fnmatch.filter(filenames, '*.sql'):
exec(filename)
The .sql files each look something like this
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tab1]') AND type in (N'U'))
BEGIN
PRINT 'DROPPING TABLE [dbo].[tab1]....'
DROP TABLE [dbo].[tab1];
END;
CREATE TABLE [tab1] ( [a] nvarchar(5) NOT NULL,
[b] nvarchar(8) NOT NULL,
[c] nvarchar(2) NULL,
[d] nvarchar(400) NULL,
[e] int NULL,
[f] int NULL,
[g] real NULL,
[h] bit NULL,
[i] bit NULL,
[j] int NULL,
[k] nvarchar(2) NULL,
[l] bit NULL,
)ALTER TABLE tab1 ADD CONSTRAINT k PRIMARY KEY NONCLUSTERED ([f], [g])
Any suggestions or tips would be greatly appreciated!