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() 检验。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它们是日期,则需要将它们转换为 varchar 以附加到查询中。
您无法将日期时间连接到字符串而不先进行转换:
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:
为了省去很多麻烦,您也可以在动态 SQL 中使用参数,是的! - 参数,就像您的实际查询一样。
而不是通过
使用表单
来运行它,这可以解决各种字符串操作、转换/格式问题。例如
To save yourself a lot of trouble, you can use parameters with dynamic SQL as well, yes! - parameters, just like in your real query.
Instead of running it via
use the form
This gets around all sorts of string manipulation, cast/format issues. e.g.