Sql查询正常工作,但在动态时失败

时间:2011-04-04 20:14:58

标签: sql tsql sql-server-2000 dynamic-sql

我有一个查询,它的运行方式如下:

  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()结束。

2 个答案:

答案 0 :(得分:3)

如果它们是日期,则需要将它们转换为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) '

答案 1 :(得分:2)

为了省去很多麻烦,你也可以使用带动态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

这解决了各种字符串操作,转换/格式问题。 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