x

backup of schema using extract

Hi

I am trying to create backup of the extract schema tables by appending cob_dt to table name. I get all the extract tables in my cursor.Then I process these tables one by one.Butafter creation of these backup tables..the backup tables too get into the cursor and a backup of the backup table is created which i dont want. I just want backup of the extract tables and not backup of the backup tables

USE [VALUEATRISK_d_temp]
GO
/****** Object:  StoredProcedure [dbo].[udsp_Source_Backup_Data]   Script Date: 08/02/2011 09:58:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[udsp_Source_Backup_Data]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[udsp_Source_Backup_Data]

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[udsp_Source_Backup_Data] (@In_Purge_Days INT)
AS
BEGIN
--EXEC [dbo].[udsp_Source_Backup_Data]  5

--Declare local variables
DECLARE @TODAY AS DATETIME;
DECLARE @FIVE_DAYS_OLD_DATE AS DATETIME
DECLARE @COB_DT AS DATETIME
DECLARE @TabName VARCHAR(255)
DECLARE @GetTabName CURSOR
DECLARE @V_FIVE_DAYS_OLD_DATE AS VARCHAR(30)
DECLARE @V_COB_DT AS VARCHAR(30)
DECLARE @TabName_Bkp as varchar(100)
DECLARE @TabName_Bkp_old as varchar(100)
DECLARE @TabName_Split AS VARCHAR(50)
DECLARE @TabName_Split_old AS VARCHAR(50)
declare @v_cob_dt_append AS varchar(30) 
DECLARE @Schema_backup AS VARCHAR(30)
DECLARE @Schema_Table_Name AS VARCHAR(50)
--Get Today's date rounded to the mid night

SELECT @TODAY = CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
--After EOD process, there will not be an entry with current_row_ind=1
SELECT @COB_DT=MAX(COB_DT) FROM STAGE.REF_LOAD_COMPLETE
SELECT @V_COB_DT=REPLACE(convert(varchar, @COB_DT, 4) ,'.','_')
select  @FIVE_DAYS_OLD_DATE = cob_dt from 
(select cob_dt,dense_rank() over (order by cob_dt desc) as pos FROM stage.ref_load_complete ) as temp 
where POS=@In_Purge_Days
--Convert date's into Varchar
SELECT @V_FIVE_DAYS_OLD_DATE=REPLACE(convert(varchar, @FIVE_DAYS_OLD_DATE , 4) ,'.','_')
SELECT @Schema_backup='EXTRACT'

--Cursor to get the publish table name
SET @GetTabName = CURSOR FOR
SELECT 
       Tab.name 
FROM    SYS.TABLES Tab 
       JOIN SYS.SCHEMAS Sch ON (Tab.Schema_Id=Sch.Schema_Id) 
WHERE   Sch.name='EXTRACT'
       AND Tab.name NOT LIKE '%BKP%' 
OPEN @GetTabName
FETCH NEXT
FROM @GetTabName INTO @TabName
WHILE @@FETCH_STATUS = 0
BEGIN
--Get the name of the backup of the table
--print @TabName
    select @TabName_Bkp=@Schema_backup + '.'+@TabName+'_'+@v_cob_dt
    select @TabName_Bkp_old=@Schema_backup + '.'+@TabName+'_'+@V_FIVE_DAYS_OLD_DATE
    select @Schema_table_name='extract'+'.'+@TabName
    print @Schema_table_name
    print @TabName_Bkp
    --Get the tablename excluding the schema name 
    SELECT @TabName_Split= SUBSTRING(@TabName_Bkp,9,LEN(@TabName_Bkp))
    SELECT @TabName_Split_Old= SUBSTRING(@TabName_Bkp_old,9,LEN(@TabName_Bkp_old))

       --If backup of the table already exists ,then delete the current Cob_Dt data from backup and insert the data for cob_Dt from extract)
          BEGIN
          PRINT ('DROP TABLE  '+@TabName_Bkp_Old+'')
          EXECUTE ('DROP TABLE  '+@TabName_Bkp_Old+'')
          END
       --Check if the backup table already exists
       if exists (select 1 from sys.objects o where o.type ='U' and o.name = @TabName_Split)
       --If backup of the table already exists ,then delete the  data from backup and insert the data again  from extract)
          BEGIN
          PRINT ('DROP TABLE  '+@TabName_Bkp+'')
          EXECUTE ('DROP TABLE  '+@TabName_Bkp+'')

       END
       ELSE
       --If backup does not exist ,then create a new table and load data from extract table into backup table
          BEGIN
            PRINT('SELECT * INTO '+@TabName_Bkp+' FROM '+@Schema_table_name+'')
          EXECUTE('SELECT * INTO '+@TabName_Bkp+' FROM '+@Schema_table_name+' ')
          END
          --@TabName_Bkp is getting again into the cursor and backup of the --backup table is created
          print @TabName
    FETCH NEXT
    FROM @GetTabName INTO @TabName
END
CLOSE @GetTabName
DEALLOCATE @GetTabName
END
more ▼

asked Aug 20, 2011 at 06:20 AM in Default

inquisitiveuser gravatar image

inquisitiveuser
41 2 2 2

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first
GOT the answer myself.use static for:)
more ▼

answered Aug 20, 2011 at 07:05 AM

inquisitiveuser gravatar image

inquisitiveuser
41 2 2 2

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x58

asked: Aug 20, 2011 at 06:20 AM

Seen: 603 times

Last Updated: Aug 20, 2011 at 07:48 AM