【问题标题】:Unclosed quotation mark after the character string 'BDR_POST-BI ( SCT'字符串 'BDR_POST-BI ( SCT' 后的非闭合引号
【发布时间】:2018-09-26 06:11:59
【问题描述】:

我之前有可以流畅运行的存储过程。但是现在当我尝试运行它时,会出现错误“Msg 105, Level 15, State 1, Line 84 字符串 'BDR_POST-BI ( SCT' ) 后面的非闭合引号。 消息 102,第 15 级,状态 1,第 84 行 'BDR_POST-BI ( SCT'." 附近的语法不正确。顺便说一下,这是我的存储过程

USE [dbbib]
GO
/****** Object:  StoredProcedure [dbo].[sp_QuerySummary]    Script Date: 10/4/2018 4:13:57 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_QuerySummary]
AS
BEGIN
DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + cast(FailureMode as varchar) + ']',
'[' + cast(FailureMode as varchar)+ ']')
FROM (SELECT    distinct    FailureMode FROM tblBIB )as s
GROUP BY FailureMode
order by FailureMode

DECLARE @columns2 VARCHAR(8000)

SELECT @columns2 = COALESCE(@columns2 + ',isnull([' + cast(FailureMode as varchar) + '],0) as ['+ cast(FailureMode as varchar) +']',
'isnull([' + cast(FailureMode as varchar)+ '],0) as ['+ cast(FailureMode as varchar)+']')
FROM (SELECT    distinct    FailureMode FROM tblBIB )as s
GROUP BY FailureMode
order by FailureMode

DECLARE @columns3 VARCHAR(8000)

SELECT @columns3 = COALESCE(@columns3 + '+isnull([' + cast(FailureMode as varchar) + '],0)',
'isnull([' + cast(FailureMode as varchar)+ '],0)')
FROM (SELECT    distinct    FailureMode FROM tblBIB where FailureMode not like 'QUARANTINE' and FailureMode not like 'BARE' and FailureMode not like 'PM' and FailureMode not like 'TEMPORARY HOLD_SET B')as s
GROUP BY FailureMode
order by FailureMode

DECLARE @query VARCHAR(8000)

SET @query = '

declare @table table (BibType nvarchar(100), CntActive int)

insert into @table
select BibType,count(BibID) as CntActive from(
select substring(BibID,1,6) as BibType, BibID from(
select * ,
case
when len(BibID)>1   and len(BIBRecall)>1 then 1
when len(BibID)=1   and len(BIBRecall)>1 then 1
else 0
end as selection
from(
SELECT        isnull(a.BibID,0) as BibID, isnull(tblBIB.BIBRecall,0)as BIBRecall
FROM            (SELECT DISTINCT BibID
                          FROM            Orca.dbo.tblLatestTurnAllBIB
                          WHERE        (CONVERT(date, DTDone) = CONVERT(date, GETDATE()))) AS a full outer JOIN
                         tblBIB ON a.BibID = tblBIB.BIBRecall
)as s
)as s where selection=0
)as q group by BibType



declare @table2 table ( CntActive int, rn int)

insert into @table2
select count(BibID) as CntActive ,0 as rn from(
select substring(BibID,1,6) as BibType, BibID from(
select * ,
case
when len(BibID)>1   and len(BIBRecall)>1 then 1
when len(BibID)=1   and len(BIBRecall)>1 then 1
else 0
end as selection
from(
SELECT    isnull(a.BibID,0) as BibID, isnull(tblBIB.BIBRecall,0)as BIBRecall
FROM            (SELECT DISTINCT BibID
                          FROM            Orca.dbo.tblLatestTurnAllBIB
                          WHERE        (CONVERT(date, DTDone) = CONVERT(date, GETDATE()))) AS a full outer JOIN
                         tblBIB ON a.BibID = tblBIB.BIBRecall
)as s
)as s where selection=0
)as q 


select*,row_number()over(order by total desc) as rn from(
SELECT Device,DeviceType,'+ @columns3 +' as Total,isnull(q.CntActive,0) as CntActive,'+ @columns2 +',q.BibType
FROM (SELECT        a.DeviceType, tbldevtypev2.Device, a.Cnt, a.FailureMode
FROM            (SELECT        DeviceType, COUNT(BIBRecall) AS Cnt, FailureMode
                          FROM            tblBIB
                          GROUP BY DeviceType, FailureMode) AS a left outer JOIN
                         tbldevtypev2 ON a.DeviceType = tbldevtypev2.[BIB Type]
)as a
PIVOT
(
MAX(Cnt)
FOR [FailureMode]
IN (' + @columns + ')
)
AS a left outer  join @table q on a.DeviceType = q.BibType
)as p 


union  all


SELECT ''Grand Total'' as Device,'''' as DeviceType,'+@columns3+' as Total,*,'''' as BibType  from(
Select isnull(i.CntActive,0) as CntActive,o.* from(
SELECT '+@columns2+',0 as rn
FROM (
SELECT        COUNT(BIBRecall) AS Cnt, FailureMode
                          FROM            tblBIB
						  
                          GROUP BY  FailureMode 
)as a
PIVOT
(
MAX(Cnt)
FOR [FailureMode]
IN (' + @columns + ')
)
AS p
)as o full outer join @table2 i on o.rn= i.rn
)as p

'

EXECUTE(@query)
END

【问题讨论】:

  • 可能你的“行字符串”上有'或"字符。考虑使用参数,或者执行一行。替换(""","")等删除它们

标签: sql-server vb.net


【解决方案1】:

将数据插入数据库时​​,请考虑使用SQL parameters 以避免SQL injection

cmd.Parameters.Add("@line", SqlDbType.VarChar).Value = line 

我也会考虑实现Using

有时您的代码需要非托管资源,例如文件句柄、COM 包装器或 SQL 连接。 Using 块保证在您的代码完成后处理一个或多个此类资源。这使得它们可供其他代码使用。

Using con As New SqlConnection(str),
      cmd As New SqlCommand("INSERT INTO table2 ([a], [roll], [c]) VALUES (1, 2, @line)", con)

    cmd.Parameters.Add("@line", SqlDbType.VarChar).Value = line

    con.Open()

    cmd.ExecuteNonQuery()
  End Using

我还会考虑循环遍历 Using 语句中的文件,以节省一遍又一遍地创建 SQL 对象:

Using con As New SqlConnection(str),
      cmd As New SqlCommand("INSERT INTO table2 ([a], [roll], [c]) VALUES (1, 2, @line)", con)
    cmd.Parameters.Add("@line", SqlDbType.VarChar)

    con.Open()

        Using sr As New StreamReader(path)
            Do While sr.Peek() >= 0
                cmd.Parameters("@line").Value = sr.ReadLine
                cmd.ExecuteNonQuery()
            Loop
        End Using
    End Using
End Sub

此代码未经测试,我没有环境,但它应该可以为您提供一些工作。

【讨论】:

    【解决方案2】:

    line的值是多少?

    试试

    cmd = New SqlCommand("insert into table2 ([a], [roll],[c]) values (1, 2, '''" & line & "''')", con)
    

    【讨论】:

    • 日志文件的数据
    • 在日志文件的数据中,您有符号'或符号“。尝试使用双重或三重',就像我发布的那样
    【解决方案3】:

    一定要使用参数。检查您的数据库以获取正确的“SqlDbType”

    Dim cmd As New SqlCommand(("insert into table2 ([a], [roll],[c]) values (1, 2, @line);", con))
    cmd.Parameters.Add("@line", SqlDbType.VarChar).Value = line
    

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      相关资源
      最近更新 更多