有表Source结构如下:
1、SourceID int 资源ID
2、Name varchar(24)名称
3、Date char(8) 生成日期(格式:YYYYMMDD)要求:编写数据库存储过程AutoCopySource,实现将当日资源表数据复制到明天,资源ID重新生成,资源名称不变,存储过程要求有返回值,0为成功执行,其余为各类自定义错误代码
1、SourceID int 资源ID
2、Name varchar(24)名称
3、Date char(8) 生成日期(格式:YYYYMMDD)要求:编写数据库存储过程AutoCopySource,实现将当日资源表数据复制到明天,资源ID重新生成,资源名称不变,存储过程要求有返回值,0为成功执行,其余为各类自定义错误代码
begin
insert into AutoCopySource(Name,date)
select Name,convert(char(8),dateadd(day,1,getdate()),112)
from AutoCopySource
where date = convert(char(8),getdate(),112)
select @err = @@ERROR
end
@flag int output
as
begin
insert into tb
select id=(select count(*) from tb)+1,
name,
Date=convert(varchar(8),dateadd(dd,1,getdate()),112)
from tb
where date=getdate()
end
datead
@flag int output
as
begin
insert into tb
select id=(select count(*) from tb)+1,
name,
Date=convert(varchar(8),dateadd(dd,1,getdate()),112)
from tb
where date=getdate()
set @flg=@@error
end
SQL codecreate proc AutoCopySource
@flag int output
as
begin
insert into tb
select id=(select count(*) from tb)+1,
name,
Date=convert(varchar(8),dateadd(dd,1,getdate()),112)
from tb
where date=getdate()
end
列名 'date' 无效。
什么情况啊?
错误提示很明显,你的tb表有date字段吗,或者把tb替换成你要查询的表名Source
begin
insert into AutoCopySource(Name,date)
select Name,convert(char(8),dateadd(day,1,getdate()),112)
from AutoCopySource
where date = convert(char(8),getdate(),112)
select @err = @@ERROR
end
create proc AutoCopySource
@flag int output
as
begin
insert into Source
select id=(select count(*) from Source)+1,
Name,
Date=convert(varchar(8),dateadd(dd,1,getdate()),112)
from Source
where Date= convert(char(8),getdate(),112)
set @flag=@@error
end
但是又出现新的问题了
数据表的ID,原本为1,2,3
执行后,为1,2,3,4,4,4
关键处在SourceID=(select count(*) from DepResource)+1,
如何使他最后执行完变为1,2,3,4,5,6
或者说当ID设为PRIMARY KEY 约束时,不产生冲突
begin
declare @i int
select @i = count(1) from Source
insert into Source(SourceID,Name,date)
select 0,Name,convert(char(8),dateadd(day,1,getdate()),112)
from Source
where date = convert(char(8),getdate(),112)
update Source set SourceID = @i,
@i = @i + 1
where SourceID = 0
select @err = @@ERROR
end