使用python Popopen时sqlcmd字符串解析错误

时间:2018-07-08 01:22:41

标签: python sqlcmd

我正在使用subproccess.Popopen在python 3.6中将sql从文件传递到sqlcmd。

这是我进入sdtout的错误。谁能弄清楚字符串中的错误在哪里?到目前为止,我找不到它。与SQL客户端连接时,我从中加载字符串的.sql文件可以正常工作。

  

stderr:   b'Sqlcmd:\'ETL \“ \ n:setvar DefaultFilePrefix \” ETL \“ \ n:setvar Col1Name \” ID \“ \ n:setvar Col1Type \” INT \“ \ n:setvar Col1Nullable \” NOT NULL \“ \ n:setvar Col2Name \“名称\” \ n:setvar Col2Type \“ NVARCHAR(128)\” \ n:setvar Col2Nullable \“ NOT NULL \” \ n:setvar Col3Name \“ Rate \” \ n:setvar Col3Type \ “ NUMERIC(3,2)\” \ n:setvar Col3Nullable \“ NOT NULL \” \ n \ n \ nGO \ n:错误退出时\ nGO \ n \ n:setvar __IsSqlCmdEnabled \“ True \” \ nGO \ nIF N \'$(__ IsSqlCmdEnabled)\'不喜欢N \'True \'\ n BEGIN \ n PRINT N \'必须启用SQLCMD模式才能成功执行此脚本。\'; \ n SET NOEXEC ON; \ n END \ n \ n \ nGO \ nUSE [$(DatabaseName)]; \ n \ nDECLARE @strSQL NVARCHAR(MAX)\ nIF(存在(选择* \ n \ t \ t \ tFROM INFORMATION_SCHEMA.TABLES \ n \ t \ t \ tWHERE TABLE_SCHEMA = \'dbo \'\ n \ t \ t \ tAND TABLE_NAME = \'TEST \')))\ n \ tBEGIN \ n \ t \ tTRUNCATE TABLE dbo.TEST \ n \ tEND \ nELSE \ n \ tBEGIN \ n \ t \ tSET @strSQL = \'\ n \ t \ t \ t \ t创建表dbo.TEST(\ n \ t \ t \ t \ t \ t \ t [$(Col1Name)] $(Col1Type)$(Col1Nullable) ,\ n \ t \ t \ t \ t \ t [$(Col2Name)] $(Col2Type)$(Col2Nullable), \ n \ t \ t \ t \ t \ t [$(Col3Name)] $(Col3Type)$(Col3Nullable)\ n \ t \ t \ t \ t)\'\ n \ t \ tEXECUTE(@strSQL)\ n \ tEND \ nGO \ n \ n插入dbo.TEST([ID],[Name],[Rate])\ nVALUES(0,N \'Alison Ball \',181.24),(1,N \'Tiffany Ramos \',444.82),(2,N \'Joseph Love \',660.88); \ nGO \ n“ \':意外参数。输入\'-?\'寻求帮助。\ r \ n'

这是从python传递的sql字符串:


GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO

:setvar DatabaseName "ETL"
:setvar DefaultFilePrefix "ETL"
:setvar Col1Name "ID"
:setvar Col1Type "INT"
:setvar Col1Nullable "NOT NULL"
:setvar Col2Name "Name"
:setvar Col2Type "NVARCHAR(128)"
:setvar Col2Nullable "NOT NULL"
:setvar Col3Name "Rate"
:setvar Col3Type "NUMERIC(3,2)"
:setvar Col3Nullable "NOT NULL"


GO
:on error exit
GO

:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
    BEGIN
        PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
        SET NOEXEC ON;
    END


GO
USE [$(DatabaseName)];

DECLARE @strSQL NVARCHAR(MAX)
IF (EXISTS (SELECT *
            FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_SCHEMA = 'dbo'
            AND TABLE_NAME = 'TEST'))
    BEGIN
        TRUNCATE TABLE dbo.TEST
    END
ELSE
    BEGIN
        SET @strSQL = '
                CREATE TABLE dbo.TEST(
                    [$(Col1Name)] $(Col1Type) $(Col1Nullable),
                    [$(Col2Name)] $(Col2Type) $(Col2Nullable),
                    [$(Col3Name)] $(Col3Type) $(Col3Nullable)
                )'
        EXECUTE(@strSQL)
    END
GO

0 个答案:

没有答案