declare @sql nvarchar(200)
declare @time smalldatetime
declare @time1 smalldatetime
set @time=convert(datetime,convert(char(10),getdate(),120),120)
set @time1=convert(datetime,convert(char(10),getdate(),120),120)+1
set @sql='alter table dbo.viewlist_tmp add constraint CK_viewlist_tmp check (vdate IS NOT NULL and vdate>=' + @time + ' and vdate<' + @time1 + ')'
--print @sql
exec(@sql)
错误日系:
将字符串转换为 smalldatetime 数据类型时失败。
@time和@time1前后加两引号,三引号都试了,报同样错,如何处理?
------解决思路----------------------
declare @sql nvarchar(200)try this
declare @time smalldatetime
declare @time1 smalldatetime
set @time=convert(datetime,convert(char(10),getdate(),120),120)
set @time1=convert(datetime,convert(char(10),getdate(),120),120)+1
set @sql='alter table dbo.viewlist_tmp add constraint CK_viewlist_tmp check (vdate IS NOT NULL and vdate>=''' + CONVERT(VARCHAR,@time) + ''' and vdate<''' + CONVERT(VARCHAR,@time1) + ''')'
--print @sql
exec(@sql)
------解决思路----------------------
中间折腾多了一趟。改成如下
declare @time CHAR(10)
declare @time1 CHAR(10)
set @time=convert(char(10),getdate(),120)
set @time1=convert(char(10),getdate()+1,120)
------解决思路----------------------
按照2楼写法,直接转一次就拼上去就没有问题。