Use these scripts with Microsoft SQL Server to archive or purge the interaction history tables on Windows OS. The scripts allow you to move the FACT table records, merge the Dimension records, and delete the records from the FACT table. Before you use any of the scripts, back up the source and target interaction history tables and create indexes on the columns. Indexes improve performance when you read data from the archived tables.
Note: The IH scripts contain variables like <source_user_name>, <target_user_name> which you need to specify before executing any of the sample scripts.
Before you perform this task, make sure you have full access to the source and the destination databases (you need the database admin privileges).
If a table does not exist, execute the following script:
SELECT * INTO <TARGET_DATABASE>.<TARGET_SCHEMA>.PR_DATA_IH_FACT FROM <SOURCE_DATABASE>.<SOURCE_SCHEMA>.PR_DATA_IH_FACT
If a table exists with the exact same columns:
Execute the following script:
INSERT INTO <TARGET_DATABASE>.<TARGET_SCHEMA>.PR_DATA_IH_FACT (column1 [, column2 ...]) SELECT column1 [, column2 ...] FROM <SOURCE_DATABASE>.<SOURCE_SCHEMA>.PR_DATA_IH_FACT
Before you perform this task, make sure that all the dimension tables are created and have index on the PZID column.
Merge the records from the source table to the dimension table.
MERGE INTO <TARGET_DATABASE>.<TARGET_SCHEMA>.PR_DATA_IH_DIM_<DIMENSION_NAME> T USING <SOURCE_DATABASE>.<SOURCE_SCHEMA>.PR_DATA_IH_DIM_<DIMENSION_NAME> SON (T.PZID = S.PZID) WHEN NOT MATCHED THEN INSERT (column1 [, column2 ...]) VALUES (S.column1 [, S.column2 ...]);
If you want to merge the dimension records from the source database to the target database, repeat this procedure for all the dimension tables.
Connect to the source database.
Execute the following script:
DELETE FROM <SOURCE_DATABASE>.<SOURCE_SCHEMA>.PR_DATA_IH_FACT WHERE PXOUTCOMETIME < CONVERT(data_type(length),expression,style);
Example:
DELETE FROM <SOURCE_DATABASE>.<SOURCE_SCHEMA>.PR_DATA_IH_FACT WHERE PXOUTCOMETIME < CONVERT(DATETIME,'30-06-15 10:34:09 PM',5);, where:
data_type is DATETIME
expression is the date value in quotes
style is 5 (Italian format)