【问题标题】:Toad and SQL Server 2005蟾蜍和 SQL Server 2005
【发布时间】:2011-04-18 06:21:46
【问题描述】:
where a.system_nr =''''5300'''' and
      a.external_status_cd = '''''''' and
      a.cust_acct_id = b.rel_cust_acct_id and
      b.cust_acct_id = c.cust_acct_id and
      c.cust_acct_id = d.cust_acct_id and
      d.acct_status_cd = ''''OPEN'''' and
      d.time_mnth_gen_id =''''' + @BegDate + ''''' and
      a.cust_acct_id = e.cust_acct_id and
      e.tran_dt >=''''' + @BegDate + ''''' and 
      e.tran_dt<=''''' + @EndDate + ''''' and 
      d.portfolio_cd = ''''HEQ'''' and
      a.time_mnth_gen_id =''''' + @BegDate + ''''' '')'

这里是已经写好的where条件,我需要修改一下。

你能告诉我他们为什么使用'''''+@begdate'''''吗?我可以使用'+Bedate' 吗? 我的意思是为什么他们每边都使用'''''

【问题讨论】:

  • 因为列值中包含单引号?
  • 我怀疑您认为构建动态查询的完整语句太大而无法包含在您的问题中。但是在完整版本上显示哪对引号包含什么会容易得多。尽管如此,基本上答案是:它与引用引号字符(')有关。

标签: sql-server toad


【解决方案1】:

在 SQL Server 中试试这个:

select '''''someval'''''

您注意到该项目给出:

''someval''

在 SQL Server 中'' 将等同于单引号字符,所以上面的行是

select [open string][single quote][single quote]someval[single quote][single quote][close string]

【讨论】:

    【解决方案2】:

    没有看到其余的 SQL,我的猜测是:

    1. 用于动态 SQL,因为 @BegDate 是一个变量,并且语句以单引号结尾
    2. 数据包含一堆单引号

    您不应该只使用'+BegDate',因为它是一个变量,并且剥离@ 会导致它被作为一个字段进行评估。

    如果您只想减少单引号的数量,我想原作者将它们放在那里是有原因的。您可以使用原始单引号运行查询,然后使用减少的单引号再次运行查询,看看您是否得到相同的结果集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-26
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多