Apply stored procedures to build data tables on a monthly basis

Source: Internet
Author: User
Tags date current time datetime query table name
Stored Procedures | data
In order to facilitate the storage of the database, data backup, recovery, in the actual library, we hope to be able to build the data table according to the specific date, for example, the event table we can establish as ev_yyyymm (yyyy for the year, MM for the month), using stored procedures can solve dynamic table. (source code is as follows:)

****************************************************
Stored Procedure Original Code * * *
****************************************************

= = = The stored procedure for generating tables prcreatedatetable===
SET QUOTED_IDENTIFIER OFF
Go
SET ansi_nulls off
Go

ALTER PROCEDURE prcreatedatetable
As
--Initialization
DECLARE @intErrorCode int,--error number, successfully displayed 0
@dtmCheckDay datetime,--System current time
@str varchar, @SubStr varchar (10),
@chrnSQL nvarchar (1000)--sql query statement
DECLARE @chvSuffixTableName varchar (50), date suffix of table name
@chvFinalTableName1 varchar (40)--Name of the table to be detected
@chvFinalTableName2 varchar (40),
@chvFinalTableName3 varchar (40),
@chvFinalTableName4 varchar (40),
@chvFinalTableName5 varchar (40),
@chvFinalTableName6 varchar (40),
@chvFinalTableName7 varchar (40),
@chvFinalTableName8 varchar (40)

SELECT @dtmCheckDay = getdate ()
SELECT @chvSuffixTableName = dbo.fnformatdate_month (@dtmCheckDay)---a formatted month to use the custom function

SELECT @chvFinalTableName1 = ' ev_ ' + ' _ ' + @chvSuffixTableName

--The query has no @chvtablename_xxxxxx (years), that is, the @chvfinaltablename table, if not the establishment
BEGIN
IF not EXISTS (SELECT * from sysobjects WHERE NAME = @chvFinalTableName1 and xtype= ' U ')--event table
BEGIN
SELECT @chrnSQL = ' CREATE TABLE [dbo]. [' + @chvFinalTableName8 + '] ( '
+ ' [evid] [char] (a) COLLATE chinese_prc_ci_as not NULL, '
+ ' [startime] [datetime] not NULL, '
+ ' [starstake] [varchar] (9) COLLATE chinese_prc_ci_as not NULL, '
+ ' [endstake] [varchar] (9) COLLATE chinese_prc_ci_as not NULL, '
+ ' [direcation] [char] (1) COLLATE chinese_prc_ci_as not NULL, '
+ ' [evtype] [varchar] (3) COLLATE chinese_prc_ci_as not NULL, '
+ ' [Endtime] [datetime] NULL, '
+ ' [Description] [varchar] COLLATE chinese_prc_ci_as NULL, '
+ ' [Advice] [varchar] COLLATE chinese_prc_ci_as NULL, '
+ ' [econloss] [int] NULL, '
+ ' [deathtoll] [tinyint] NULL, '
+ ' CONSTRAINT ' + @chvFinalTableName8 + ' _pk ' + ' PRIMARY KEY CLUSTERED ([evid]) on [PRIMARY] '
+ ') on [PRIMARY] '
EXEC sp_executesql @chrnSQL
End
End;

Go
SET QUOTED_IDENTIFIER OFF
Go
SET ANSI_NULLS on
Go

= = = Get Date Custom Function = = =
SET QUOTED_IDENTIFIER ON
Go
SET ANSI_NULLS on
Go

ALTER FUNCTION fnformatdate_month (@DATE datetime)
RETURNS varchar (50)
As
BEGIN
DECLARE @intDateYear int,---year to process data
@intDateMonthNo int---months to process data
DECLARE @chvMonthNo varchar (10),
@chvTableName varchar (50)

Select @intDateYear = year (@DATE)
Select @intDateMonthNo = Month (@DATE) Select @chvMonthNo = ' +convert ' (varchar (2), @intDateMonthNo)
Select @chvMonthNo =substring (@chvMonthNo, Len (@chvMonthNo)-1, 2
Select @chvTableName =convert (varchar (4), @intDateYear) + @chvMonthNo
Return (@chvTableName)
End

Go
SET QUOTED_IDENTIFIER OFF
Go
SET ANSI_NULLS on
Go
============================================================

Just graduated, do software, beginners to use stored procedures.


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.