SQL Server dynamic stored procedures save data by date sample _mssql

Source: Internet
Author: User
Tags datetime getdate one table

There is often a large amount of data in your project that is saved to the database, such as saving it with just one table. The preferred solution is to create dynamic tables by date to hold the data. With a dynamic stored procedure as the preferred option, a date calculation in a SQL Server stored procedure, and a date-efficient table, which is the most effective, is pasted as part of a dynamic stored procedure for a company project, without changing the method of saving the code:

-----SQL statement: ALTER proc [dbo]. [Eventinsert] @chrTagData varchar (50),--Number @intEData int, @chrJZData varchar (), @intDYData int, @intXHData int, @create  Data datetime, @chrtype varchar (1)--query condition as BEGIN declare @chrTitle varchar (1000) declare @chrSql nvarchar (4000) DECLARE @chrdate varchar @chrMetabname varchar (50)--New alarm declare daily, declare @chrSendtabname varchar (50)--New message popup every day name de

Clare @chrSockDatatabname varchar (50)--Daily raw data new table name set @chrdate =replace (CONVERT (varchar (), GETDATE (), 120), '-', ' Set @chrMetabname = ' Socketme ' + @chrdate set @chrSendtabname = ' messsend ' + @chrdate set @chrSockDatatabname = ' Sockdata ' +@ Chrdate If IsNull (@chrtype, ') = ' begin return to select @chrTitle =categorytitle from EventCategory where categoryid=@c Hrtype----New daily information simulate table 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), TagdataMe varchar, pcount int NULL, Content varchar (5000), UserID int null, Jzdata varchar (m), Edata int, dydata int, xhdat a int, Type varchar (1), State varchar (1), Ifclose varchar (1), Closedate datetime, CreateDate datetime,) end '--print @c Hrsql exec (@chrsql)--------new information Simulate table 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 V Archar (tabprid int, messtitle varchar (), tagdata varchar (), Tagdatame varchar (1000), Content varchar (pdf), TYP e varchar (1), createdate datetime) End '--print @chrsql exec (@chrsql)-----The simulated environment determines that the eligible data is inserted into the----------------------SE T @chrsql = ' If Not exists (select 1 from ' + @chrMetabname + ' wheretagdata= ' + @chrTagData + ' "and type= ' + @chrtype + '" and ifclose= ' 0 ') Begin-Inserts a table insert INTO ' + @chrMetabname + ' (tabname,tagdata,tagdatame,content, Jzdata,edata,dydata, Xhdata,type,ifclose,createdate,State)--Analog data Select ' + @chrMetabname + ' ', ' + @chrTagData + ' ', Dbo.funtagdatameget_all (' + @chrTagData + '), ' +@ Chrtitle+ ', Position: [' +dbo.fungetevenaddget ' + @chrJZData + '] + '] ', ' + @chrJZData + ', ', ' +cast (@intEData as varchar) + ', ' +cast (@intDYData as varchar) + ', ' +cast (@intXHData as varchar) + ', ', ' + @chrtype + ' ', ' ' 0 ', GETDATE (), ' 0 '----dbo.fungetevenaddget is a custom function declare @intSMeID int DECLARE @chrtempdate varchar set @intSMeID =@@ Identity Delete ' + @chrSendtabname + ' wheretagdata= ' + @chrTagData + ' andtype= ' + @chrtype + ' '---insert table two into ' +@c Hrsendtabname+ ' (tabname,tabprid,messtitle,tagdata,content,type,createdate) Select ' + @chrMetabname + ' ', @
Intsmeid,dbo.funtagdatameget_all (' + @chrTagData + ') + ' + @chrTitle + ', ' + @chrTagData + ', Dbo.funtagdatameget_all (' + @chrTagData + ') + ' + @chrTitle + ', Position: [' +dbo.fungetevenaddget ' + @chrJZData + '] + ' '] ', ' + @chrtype + ', GETDATE () end ' Print @chrsql exec (@chrsql) to insert a dynamic table after logical processing based on the actual business

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.