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
--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)
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.