Hello All,
I have to delete the unwanted records from all the 200 tables considering two columns of dbo4.id_trans_map table as my reference. Below is the code I have written for this, I have not added delete to this, as first I want to ensure that the code is right. There will around 200X1000 delete statements, what would be good approach to run this.
Below is the sample code I have started with, can you help me with correcting this or validating
DECLARE @bt_id NVARCHAR(100)
DECLARE @vtrans_id NVARCHAR(100)
DECLARE @vTabName NVARCHAR(300)
DECLARE @vSQL NVARCHAR(1000)
--list down all the batches and trans_ids(around 10K rec here) where ref_no is null and delete these recs from 200+ tables
DECLARE curEmpiRfmNull CURSOR
FOR SELECT bt_id,trans_id FROM dbo4.id_trans_map WHERE ISNULL(rf_no,'')=''
--list of all the 200+ tables from where I need to delete the rec pertaining to above cursor condition
DECLARE dboTabList CURSOR
FOR SELECT 'dbo.' + T.TABLE_NAME
FROM dbo4.INFORMATION_SCHEMA.TABLES T
WHERE T.TABLE_NAME IN (SELECT TABLE_NAME
FROM dbo4.INFORMATION_SCHEMA.COLUMNS C
WHERE C.COLUMN_NAME IN ('bt_id') -- only tables which has bt_id to be picked
)
OPEN curEmpiRfmNull;
FETCH NEXT FROM curEmpiRfmNull INTO @bt_id,@vtrans_id
WHILE @@FETCH_STATUS = 0
BEGIN
OPEN dboTabList;
FETCH NEXT FROM dboTabList INTO @vTabName
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @vSQL= 'DELETE '+@vTabName+' WHERE bt_id='''+@bt_id+''' AND trans_id ='''+@vtrans_id +''''-- building sql for this purpose
PRINT @vSQL
FETCH NEXT FROM dboTabList INTO @vTabName
END
CLOSE dboTabList;
FETCH NEXT FROM curEmpiRfmNull INTO @bt_id,@vtrans_id
END
CLOSE curEmpiRfmNull;
CLOSE dboTabList; Neil