数据类型varchar和int在concat运算符中不兼容

时间:2015-04-26 08:01:02

标签: sql asp.net sql-server-2008-r2

过去两天我一直被困住,执行它会给出错误:

  

数据类型varchar和int在concat运算符

中不兼容

这是我的表:

create table salestable
(
     id int identity(1,1) not null primary key, 
     empid char(5), 
     datesold date, 
     monthonly varchar(50), 
     amount money
)

此代码将虚拟记录插入dbo.salestable以便在salestable中工作,调试并逐步进入提供调试和理解代码的代码

declare @outercounter int = 1
declare @innercounter int = 1

while @outercounter <= (select count(name) from namestable)
begin 
    while @innercounter <= datediff(day, getdate() -datepart(day, getdate()), {fn concat('12/31/',Datepart(year,getdate()))})
    begin 
        insert into salestable (empid, datesold, monthonly, amount) 
        values (@outercounter, 
                getdate() - datepart(day, getdate()) + @innercounter,
                Datename(month, getdate() - datepart(day, getdate()) + @innercounter),
                rand() * 10000)
        set @innercounter =  @innercounter +1           
    end

    set @outercounter = @outercounter + 1
    set @innercounter = 1
end

select * from dbo.salestable

1 个答案:

答案 0 :(得分:0)

fn concat('12/31/',CAST(Datepart(year,getdate()) AS VARCHAR(10)))

试试这个