欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sql server动态存储过程按日期保存数据示例

程序员文章站 2023-11-27 23:45:16
在项目中经常有大量数据信息保存到数据库,如只用一张表保存那肯定不现实,首选解决方案为按日期建立动态表来保存数据。在不改变保存方式的代码的情况下,用动态存储过程是首选,在sq...

在项目中经常有大量数据信息保存到数据库,如只用一张表保存那肯定不现实,首选解决方案为按日期建立动态表来保存数据。在不改变保存方式的代码的情况下,用动态存储过程是首选,在sql server存储过程中进行日期计算,按日期建表效率最高,下面就公司项目的部分动态存储过程粘贴出来:

-----sql语句:

alter proc [dbo].[eventinsert]
@chrtagdata varchar(50), --编号
@intedata int,
@chrjzdata varchar(50),
@intdydata int,
@intxhdata int,
@createdata datetime,
@chrtype varchar(1) --查询条件
as
begin

declare @chrtitle varchar(1000)
declare @chrsql nvarchar(4000)
declare @chrdate varchar(50)
declare @chrmetabname varchar(50) --每日新建报警新表名
declare @chrsendtabname varchar(50) --每日新建消息弹出框新表名
declare @chrsockdatatabname varchar(50) --每日原始数据新表名

set @chrdate =replace(convert(varchar(10),getdate(),120),'-','')

set @chrmetabname='socketme'+@chrdate
set @chrsendtabname='messsend'+@chrdate
set @chrsockdatatabname='sockdata'+@chrdate

if isnull(@chrtype,'')=''
begin
return
end

select @chrtitle=categorytitle from eventcategory where categoryid=@chrtype

----新建每日信息模拟表1

set @chrsql= '
if not exists(select 1 from sysobjects where name='''+@chrmetabname+''' and type=''u'')
begin
create table '+@chrmetabname+'(
smeid int identity(1,1) primary key,
tabname varchar(50),
tagdata varchar(50),
tagdatame varchar(500),
pcount int null,
content varchar(5000),
userid int null,
jzdata varchar(50),
edata int,
dydata int,
xhdata int,
type varchar(1),
state varchar(1),
ifclose varchar(1),
closedate datetime,
createdate datetime, 
)
end
'
--print @chrsql 
exec(@chrsql)

--------新建信息模拟表2------------

set @chrsql= '
if not exists(select 1 from sysobjects where name='''+@chrsendtabname+''' and type=''u'')
begin
create table '+@chrsendtabname+'(
messid int identity(1,1) primary key,
tabname varchar(50),
tabprid int,
messtitle varchar(500),
tagdata varchar(50),
tagdatame varchar(1000),
content varchar(2000),
type varchar(1),
createdate datetime
)
end
'
--print @chrsql 
exec(@chrsql)

-----模拟环境 判断符合条件的数据则插入----------------------

set @chrsql= '
if not exists(select 1 from '+@chrmetabname+' wheretagdata='''+@chrtagdata+''' and type='''+@chrtype+''' and ifclose=''0'')
begin

--插入表一

insert into '+@chrmetabname+' (tabname,tagdata,tagdatame,content,
jzdata,edata,dydata,xhdata,type,ifclose,createdate,state)

--模拟数据

select '''+@chrmetabname+''','''+@chrtagdata+''',dbo.funtagdatameget_all('''+@chrtagdata+'''),
'''+@chrtitle+',位置:[''+dbo.fungetevenaddget('''+@chrjzdata+''')+'']'','''+@chrjzdata+''','''+cast(@intedata as varchar)+''','''+cast(@intdydata as varchar)+''','''+cast(@intxhdata as varchar)+''',
'''+@chrtype+''',''0'',getdate(),''0''

----dbo.fungetevenaddget 为自定义函数

declare @intsmeid int
declare @chrtempdate varchar(50)
set @intsmeid =@@identity 

delete '+@chrsendtabname+' wheretagdata='''+@chrtagdata+''' andtype='''+@chrtype+'''

---插入表二

insert into '+@chrsendtabname+' (tabname,tabprid,messtitle,tagdata,content,type,createdate)
select '''+@chrmetabname+''',@intsmeid,dbo.funtagdatameget_all('''+@chrtagdata+''')+'''+@chrtitle+''','''+@chrtagdata+''',
dbo.funtagdatameget_all('''+@chrtagdata+''')+'''+@chrtitle+',位置:[''+dbo.fungetevenaddget('''+@chrjzdata+''')+'']'','''+@chrtype+''',getdate()

end 
' 
print @chrsql
exec(@chrsql)

end

---根据实际业务进行逻辑处理后插入动态表