无法将smalldatetime注入D-SQL语句

发布于 2024-07-26 08:08:37 字数 440 浏览 9 评论 0原文

当我尝试执行此sql语句时,我收到错误。将字符串转换为smalldatetime数据类型时转换失败。

有谁知道我做错了什么?

declare @modality varchar(50)
declare @datefrom smalldatetime

set @modality = 'xxxxxxx'
set @datefrom = '20090101'

declare @var1 nvarchar(4000)
select @var1 = 
    'select
        sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +'
    from dbo.vw_RawData
    where vw.date >= ' + @datefrom + ''

exec sp_executesql @var1

when i try to execute this sql statement i am getting the error.. Conversion failed when converting character string to smalldatetime data type.

Does anyone know what i am doing wrong?

declare @modality varchar(50)
declare @datefrom smalldatetime

set @modality = 'xxxxxxx'
set @datefrom = '20090101'

declare @var1 nvarchar(4000)
select @var1 = 
    'select
        sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +'
    from dbo.vw_RawData
    where vw.date >= ' + @datefrom + ''

exec sp_executesql @var1

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

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

发布评论

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

评论(2

凡尘雨 2024-08-02 08:08:37

您正在尝试将smalldatetime 与varchar 连接起来。
更改

解决方案 1

declare @datefrom smalldatetime

declare @datefrom varchar(8) 

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ' + @datefrom + ''

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ''' + @datefrom + ''''

解决方案 2

更改

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ' + @datefrom + ''

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ''' + convert(varchar(10), @datefrom, 121) + ''''

You are trying to concatenate the smalldatetime with a varchar.
Change

Solution 1

declare @datefrom smalldatetime

to

declare @datefrom varchar(8) 

and

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ' + @datefrom + ''

to

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ''' + @datefrom + ''''

Solution 2

change

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ' + @datefrom + ''

to

select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +
' from dbo.vw_RawData where vw.date >= ''' + convert(varchar(10), @datefrom, 121) + ''''
当爱已成负担 2024-08-02 08:08:37

在语句中 select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +' from dbo.vw_RawData where vw.date >= ' + @datefrom + ' ' SQL Server 尝试通过将所有周围的字符串转换为smalldatetime 来进行日期算术,而不是将@datefrom 转换为字符串并执行字符串连接。

可能的修复。

  • 将 @DateFrom 更改为刺痛,以便
    连接有效。 注意你
    必须添加一些引号,以便
    @Var1 中的字符串正确
    格式化。
  • 使用convert函数将@datefrom转换为字符串。 在在线图书中查找正确的转换数字。 我现在没时间。 不要使用强制转换,它不会给出“
  • 使用参数化 SQL 字符串”。 在联机丛书中查找 sp_executesql。 (或者等等,StackOverflow 总是有人指出如何避免动态 SQL。)

编辑:刚刚检查过。 cast(@DateTime as Varchar(...)) 给出了一个我认为可能难以解析的字符串,但它似乎有效,可以尝试而不是转换。 确保 varchar() 足够大

In the statement select @var1 = 'select sum('+ @modality +') as ' + dbo.fnc_titlecase(@modality) +' from dbo.vw_RawData where vw.date >= ' + @datefrom + '' SQL Server is trying to do date arithmetic by casting all the surrounding strings to a smalldatetime instead of converting @datefrom to a string and performing string concatenation.

Possible fixes.

  • Change @DateFrom to a sting so that
    the concatenation works. Note you
    will have to add some quotes so that
    the string in @Var1 is properly
    formated.
  • Use convert function to convert @datefrom to a string. Look up the right conversion number in Books online. I don't have time to right now. Don't use cast, it won't give a
  • Use a paramertized SQL String. Look up sp_executesql in Books Online. (Or wait, StackOverflow always has someone to point out how to avoid dynamic SQL.)

EDIT: Just checked. cast(@DateTime as Varchar(...)) gives a string that I thought might be hard to parse, but it seems to work, might try that instead of convert. Make sure the varchar() is big enough

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文