上下文:
我有一个SQL查询,它通过master.dbo.xp_cmdshell
大量使用了临时表和一些外部资源,我需要重构T-SQL,以便可以将其传递给Pyodbc游标并成功执行。
从我在这里读到的内容:Create SQL Server temporary tables inside Python script和这里:Pyodbc execution failed on temp table来看,让pyodbc接受本地/全局临时表操作很棘手。我还没弄清楚。
问题: 有人可以帮我修改下面的T-SQL块,以便它可以成功地通过pyodbc查询游标运行吗?用CTE替换临时表似乎不起作用。
IF OBJECT_ID('tempdb..##jsondump') IS NOT NULL DROP TABLE ##jsondump
IF OBJECT_ID('tempdb..##jsonparsed') IS NOT NULL DROP TABLE ##jsonparsed
IF OBJECT_ID('tempdb..##json_loop') IS NOT NULL DROP TABLE ##json_loop
CREATE TABLE ##jsondump (
[my_json] [nvarchar](max) NULL
)
-- Create a table to house the parsed content
CREATE TABLE ##jsonparsed (
[id] [int] NULL,
[url] [varchar](255) NULL,
[company_name] [varchar](255) NULL,
[domain] [varchar](255) NULL
)
-- Clear ##jsondump
TRUNCATE TABLE ##jsondump;
GO
-- Clear ##jsonparsed ( only if you don't want to keep what's already there )
TRUNCATE TABLE ##jsonparsed;
GO
BULK INSERT ##jsondump
FROM 'C:\Users\ebarnes\etl_project\company_data.json'
WITH (
ROWTERMINATOR = '\n'
);
-- Select JSON into ##jsonparsed
SELECT my_json
INTO ##json_loop
FROM ##jsondump
INSERT INTO ##jsonparsed (
id, [url], company_name, domain
)
SELECT
jsn.id, jsn.[url], jsn.company_name, jsn.domain
FROM ##json_loop
OUTER APPLY (
SELECT * FROM OPENJSON(##json_loop.my_json, '$.companies' )
WITH (
id INT '$.id',
[url] VARCHAR(255) '$.url',
company_name VARCHAR(255) '$.company_name',
domain VARCHAR(255) '$.domain'
)
) AS jsn
exec master.dbo.xp_cmdshell 'bcp "SELECT ''Company_ID'', ''MatterMark_URL'', ''Company_Name'', ''Domain'' UNION ALL SELECT DISTINCT cast(id as varchar( 12 ) ), url, company_name, domain FROM ##jsonparsed" queryout C:\Users\ebarnes\etl_project\company_data.txt -c -T -t'
--SELECT DISTINCT * FROM ##jsonparsed
--ORDER BY id ASC;
DROP TABLE ##jsondump
DROP TABLE ##jsonparsed
DROP TABLE ##json_loop
exec xp_cmdshell 'powershell ""C:\Users\ebarnes\etl_project\open_file.ps1""'