Methods for merging user log tables in SQL Server

Source: Internet
Author: User
Tags log table name
Server

In the process of maintaining SQL Server databases, do you often encounter thousands of log20050901 such as the log table, each table is not a lot of data, one open look is very inconvenient, or sometimes we need to put the data in these tables, One open operation is also very troublesome. Here is a way to automate the merging of tables.

My idea is to create a user stored procedure to complete a series of automated operations, the following is the code.

--Stored procedures I named Backupdata, you can use your own definition of the name.

--the name of the target table generated by the parameter 1:@tabletarget

--Parameter 2:@tablestart the table name of the merge start

--Parameter 3:@tableend the table name at the end of the merge

CREATE PROCEDURE backupdata @TableTarget sysname, @TableStart sysname, @TableEnd sysname

As

DECLARE tnames_cursor Cursor

For

SELECT table_name

From INFORMATION_SCHEMA. TABLES

OPEN Tnames_cursor

DECLARE @TableName sysname

DECLARE @TablePref sysname

DECLARE @IsTargetExist Integer

--Determine if the target table exists

SET @IsTargetExist = (SELECT count (table_name) from INFORMATION_SCHEMA. TABLES WHERE table_name = @TableTarget)

--Create a new table if the target table does not exist

IF @istargetexist =0

BEGIN

The statements in--exec can be replaced with table scripts written by SQL Server. Note You cannot have fields in the destination table that have the same AutoNumber type as the names in the table you want to merge.

EXEC (' CREATE TABLE [dbo].[' + @TableTarget + ']

(

[LOG1] [nvarchar] (a) COLLATE chinese_prc_ci_as NULL,

[LOG2] [nvarchar] (a) COLLATE chinese_prc_ci_as NULL,

......

)')

End



FETCH NEXT from Tnames_cursor into @TableName

while (@ @FETCH_STATUS <>-1)

BEGIN

IF (@ @FETCH_STATUS <>-2)

BEGIN

SELECT @TableName = RTRIM (@TableName)

--The following two lines change according to the name of the log table

--Take the first 3 digits of the log table name as an identity

SELECT @TablePref = Left (@TableName, 3)

--Determine whether the table name is attached to the requirement

IF (@TablePref = ' log ') and (@TableName >= @TableStart) and (@TableName <= @TableEnd)

--Start the import

BEGIN

EXEC (' INSERT into ' + @TableTarget + ' SELECT * from ' + @TableName)

PRINT ' table ' + @TableName + ' imported ' + @TableTarget + ' in '

End

End

FETCH NEXT from Tnames_cursor into @TableName

End

--Free memory

Close Tnames_cursor

Deallocate tnames_cursor

Run the above code in SQL Query Analyzer, which generates stored procedure backupdata.

The use of Backupdata is as follows:

EXEC backupdata ' merged table name ', ' Start table name ', ' End table name ', for example:

EXEC backupdata ' _logs200508 ', ' log200508000000 ', ' log200508319999 '.

Because I didn't find an SQL statement that could do this, so write a stored procedure, if you have a better and simpler way please send me a message or a letter:pujiang10@gmail.com.



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.