SQL查询正常,但动态查询失败

发布于 2024-10-30 19:28:26 字数 1922 浏览 0 评论 0原文

我有一个查询,当它像这样运行时可以工作:

  declare @nvRecipients varchar(4000)
    ,@CustomerCode varchar(6)
    ,@start datetime
    ,@end datetime

 SELECT @CustomerCode = '10095'
       ,@start = '01/01/2011'
       ,@end = '02/01/2011'

  Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , 
  convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped 
  From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH 
  Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber 
  Where Cast ( PKSDate As DateTime ) >= @start
    And Cast ( PKSDate As DateTime ) <= @end
    And PSD.Quantity > 0  
    And ( CompanyCode = @CustomerCode)
   Group By  substring(PSD.DTSItemCode,1,4) 
   Order By  substring(PSD.DTSItemCode,1,4) 

但是抛出错误

转换日期时间时出现语法错误 字符串。

当在提供参数 @customercode、@start 和 @end 的存储过程中像这样运行时:

       Set @nvQuery = ' Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , '
  Set @nvQuery = @nvQuery + ' convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped ' 
  Set @nvQuery = @nvQuery + ' From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH '
  Set @nvQuery = @nvQuery + ' Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber '
  Set @nvQuery = @nvQuery + ' Where Cast ( PKSDate As DateTime ) >= ''' + @start + ''' '
  Set @nvQuery = @nvQuery + '   And Cast ( PKSDate As DateTime ) <= ''' + @end + ''' '
  Set @nvQuery = @nvQuery + '   And PSD.Quantity > 0 '  
  Set @nvQuery = @nvQuery + '   And ( CompanyCode = ''' + @CustomerCode + ''') '
  Set @nvQuery = @nvQuery + '  Group By  substring(PSD.DTSItemCode,1,4) '
  Set @nvQuery = @nvQuery + '  Order By  substring(PSD.DTSItemCode,1,4) '

任何人都可以看到我的错误吗?我好像找不到啊数据全部用 ISDATE() 检验。

I have a query, which works when its run like this:

  declare @nvRecipients varchar(4000)
    ,@CustomerCode varchar(6)
    ,@start datetime
    ,@end datetime

 SELECT @CustomerCode = '10095'
       ,@start = '01/01/2011'
       ,@end = '02/01/2011'

  Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , 
  convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped 
  From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH 
  Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber 
  Where Cast ( PKSDate As DateTime ) >= @start
    And Cast ( PKSDate As DateTime ) <= @end
    And PSD.Quantity > 0  
    And ( CompanyCode = @CustomerCode)
   Group By  substring(PSD.DTSItemCode,1,4) 
   Order By  substring(PSD.DTSItemCode,1,4) 

But throws the error

Syntax error converting datetime from
character string.

When ran like this within a stored procedure where @customercode, @start, and @end are supplied parameters:

       Set @nvQuery = ' Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , '
  Set @nvQuery = @nvQuery + ' convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped ' 
  Set @nvQuery = @nvQuery + ' From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH '
  Set @nvQuery = @nvQuery + ' Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber '
  Set @nvQuery = @nvQuery + ' Where Cast ( PKSDate As DateTime ) >= ''' + @start + ''' '
  Set @nvQuery = @nvQuery + '   And Cast ( PKSDate As DateTime ) <= ''' + @end + ''' '
  Set @nvQuery = @nvQuery + '   And PSD.Quantity > 0 '  
  Set @nvQuery = @nvQuery + '   And ( CompanyCode = ''' + @CustomerCode + ''') '
  Set @nvQuery = @nvQuery + '  Group By  substring(PSD.DTSItemCode,1,4) '
  Set @nvQuery = @nvQuery + '  Order By  substring(PSD.DTSItemCode,1,4) '

Can anyone see my error? I can't seem to find it. The data all checks out with ISDATE().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-11-06 19:28:26

如果它们是日期,则需要将它们转换为 varchar 以附加到查询中。

您无法将日期时间连接到字符串而不先进行转换:

Set @nvQuery = @nvQuery + '   And Cast( PKSDate As DateTime) <= ''' + CAST(@end AS VARCHAR(20)) + ''' '

  Set @nvQuery = ' Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , '
  Set @nvQuery = @nvQuery + ' convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped ' 
  Set @nvQuery = @nvQuery + ' From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH '
  Set @nvQuery = @nvQuery + ' Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber '
  Set @nvQuery = @nvQuery + ' Where Cast ( PKSDate As DateTime ) >= ''' + CONVERT(varchar(20), @start, 101) + ''' '
  Set @nvQuery = @nvQuery + '   And Cast ( PKSDate As DateTime ) <= ''' + CONVERT(varchar(20), @end, 101) + ''' '
  Set @nvQuery = @nvQuery + '   And PSD.Quantity > 0 '  
  Set @nvQuery = @nvQuery + '   And ( CompanyCode = ''' + @CustomerCode + ''') '
  Set @nvQuery = @nvQuery + '  Group By  substring(PSD.DTSItemCode,1,4) '
  Set @nvQuery = @nvQuery + '  Order By  substring(PSD.DTSItemCode,1,4) '

If they're dates, the you need to cast them to varchar to append to the query.

You cannot concatenate a datetime to a string w/o casting it first:

Set @nvQuery = @nvQuery + '   And Cast( PKSDate As DateTime) <= ''' + CAST(@end AS VARCHAR(20)) + ''' '

  Set @nvQuery = ' Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , '
  Set @nvQuery = @nvQuery + ' convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped ' 
  Set @nvQuery = @nvQuery + ' From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH '
  Set @nvQuery = @nvQuery + ' Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber '
  Set @nvQuery = @nvQuery + ' Where Cast ( PKSDate As DateTime ) >= ''' + CONVERT(varchar(20), @start, 101) + ''' '
  Set @nvQuery = @nvQuery + '   And Cast ( PKSDate As DateTime ) <= ''' + CONVERT(varchar(20), @end, 101) + ''' '
  Set @nvQuery = @nvQuery + '   And PSD.Quantity > 0 '  
  Set @nvQuery = @nvQuery + '   And ( CompanyCode = ''' + @CustomerCode + ''') '
  Set @nvQuery = @nvQuery + '  Group By  substring(PSD.DTSItemCode,1,4) '
  Set @nvQuery = @nvQuery + '  Order By  substring(PSD.DTSItemCode,1,4) '
凶凌 2024-11-06 19:28:26

为了省去很多麻烦,您也可以在动态 SQL 中使用参数,是的! - 参数,就像您的实际查询一样。

  Set @nvQuery = ' Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , '
  Set @nvQuery = @nvQuery + ' convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped ' 
  Set @nvQuery = @nvQuery + ' From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH '
  Set @nvQuery = @nvQuery + ' Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber '
  Set @nvQuery = @nvQuery + ' Where Cast ( PKSDate As DateTime ) >= @start '
  Set @nvQuery = @nvQuery + '   And Cast ( PKSDate As DateTime ) <= @end '
  Set @nvQuery = @nvQuery + '   And PSD.Quantity > 0 '  
  Set @nvQuery = @nvQuery + '   And ( CompanyCode = @CustomerCode) '
  Set @nvQuery = @nvQuery + '  Group By  substring(PSD.DTSItemCode,1,4) '
  Set @nvQuery = @nvQuery + '  Order By  substring(PSD.DTSItemCode,1,4) '

而不是通过

EXEC (@nvQuery)

使用表单

exec sp_executeSQL @nvQuery,
     N'@start datetime,@end datetime,@customerCode varchar(6)', --list of params
     @start, @end, @customerCode -- params, matching list

来运行它,这可以解决各种字符串操作、转换/格式问题。例如

declare @start datetime,@end datetime,@customerCode varchar(6)
select @start = getdate(), @end = getdate()+2, @customerCode = 'TEST'

set @nvQuery ... -- build the statement

exec sp_executeSQL @nvQuery,
     N'@start datetime,@end datetime,@customerCode varchar(6)', --list of params
     @start, @end, @customerCode -- params, matching list

To save yourself a lot of trouble, you can use parameters with dynamic SQL as well, yes! - parameters, just like in your real query.

  Set @nvQuery = ' Select substring(PSD.DTSItemCode,1,4) As ItemCompanyCode , '
  Set @nvQuery = @nvQuery + ' convert(varchar(50),Cast(Sum(PSD.Quantity) as money),1) As TotalShipped ' 
  Set @nvQuery = @nvQuery + ' From  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipHeader PSH '
  Set @nvQuery = @nvQuery + ' Inner Join  [AVANTISERVER\NCL_MASTER].AVANTI.dbo.PackingSlipsDetail PSD On PSH.PKSNumber = PSD.PKSNumber '
  Set @nvQuery = @nvQuery + ' Where Cast ( PKSDate As DateTime ) >= @start '
  Set @nvQuery = @nvQuery + '   And Cast ( PKSDate As DateTime ) <= @end '
  Set @nvQuery = @nvQuery + '   And PSD.Quantity > 0 '  
  Set @nvQuery = @nvQuery + '   And ( CompanyCode = @CustomerCode) '
  Set @nvQuery = @nvQuery + '  Group By  substring(PSD.DTSItemCode,1,4) '
  Set @nvQuery = @nvQuery + '  Order By  substring(PSD.DTSItemCode,1,4) '

Instead of running it via

EXEC (@nvQuery)

use the form

exec sp_executeSQL @nvQuery,
     N'@start datetime,@end datetime,@customerCode varchar(6)', --list of params
     @start, @end, @customerCode -- params, matching list

This gets around all sorts of string manipulation, cast/format issues. e.g.

declare @start datetime,@end datetime,@customerCode varchar(6)
select @start = getdate(), @end = getdate()+2, @customerCode = 'TEST'

set @nvQuery ... -- build the statement

exec sp_executeSQL @nvQuery,
     N'@start datetime,@end datetime,@customerCode varchar(6)', --list of params
     @start, @end, @customerCode -- params, matching list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文