sql - Looping through different tables of different dates -
We have a legacy application, which has created several tables with the following naming convention: table_20140618, table_20140623, etc., where The date is running the program. I am now trying to clean the database, and have left some of these tables.
Each table has two fields: date start and date finished. I want to select the tables (and then skip them) where the date and date of the starred date is not zero, is not zero.
At the moment I am using the following questions to select all the tables that begin with 'Table_': such as:
from selection (TABLE_NAME) SOCEMA.TABLES WHERE TABLE_TYPE = 'base table' and 'table_%' like TABLE_NAME; I'm not sure how to find all the areas within your fields and get together. I can do this via code, but this means that there should be many hits on the database. Any ideas?
This above, after my first comment, but you should be able to change the code to fit your glasses Actually, it will use dynamic SQL to generate commands based on your filters and conditions. You can use whatever conditions you want in SELECT = SQL = ... section to check the dates, and then when the conditions are met add the name of the table . The script returns a list with the tablename and drop command, so that you can check what you are doing before you do it. But from there you can simply copy the drop command list and if you want you can execute it.
IF OBJECT_ID ('tempdb .. # Tables') is not a null drop table # Table Table # Create tabs (ROWNMBER INT identity (1,1), TableName VARCHAR (256) Combat DATABASE_DEFAULT) / * - Check out the #TABLES name from the sys.tables name where the name 'table' [_]% to get all the tables with insert the specified name to replace the old code '* / - Updated code only those tables which dateStarted and DateFinished columns #TABLES Selection TAB.name INSERT sys.tables tabs to include C1.object_id = TAB.object_id and C1.name to include sys.columns C1 = 'DateStarted' Left C2.object_id = TAB. Join 'sys.columns' in C2 at object_id and C2.name =' DateFinished 'Where' Table [_]% 'and' C1.name 'is not zero like TAB.name and C2.name is Null if OBJECIDID (' Tempbib .. # Droppable_babels') Do not have drop drop tables #DriftPabletBables table #Create DrappabelleBables (TabLand VARCHAR (256) Collated DatabaseEditBell DECLARE @ROW_NOW INT, @ROW_MAX INT, @SQL varchar (max), @TABLENAMEVARCHAR (256) Select @ROW_NOW = MIN (ROWNMBER), @ROW_MAX = MAX (ROWNMBER) #TABLES while @ROW_NOW & lt; = @ROW_MAX start selection @TABLENAME = #TABLES Where from ROWNMBER, TableName = @ROW_NOW SELECT @SQL = '(SELECT COUNT (*) FROM' + @ TABLENAME + 'WHERE is not a start date tap) & gt; 0 more (SELECT COUNT (*) FROM '+ @ TABLENAME +' WHERE DateFinished faucet) & gt; 0 Select '' '+ @ TableName +' '#DROPPABLE_TABLES EXEC (@SQL) set @ROW_NOW = @ ROW_NOW +1 end *,' Drop Table '+ TableName # DROPCOMMAND from #DROPPABLE_TABLES Edit: According to your comment, it looks like those columns are not in all those tables, you can use the following script to identify the tables mentioned And which columns are missing, so that you can check them further and you use the same idea first query To filter the results of, count only in tables with those columns. SELECT TAB.name TABLENAME, when C1.name is nulled then the 'missing' remaining 'End DateStarted_COL' is the case when C2.name is then 'missing' balance 'zero' End DateFinished_COL From the sys.tables tab, go to C1.object_id = TAB.object_id and C1.name = sys.columns at C1 'DateStarted' CEO at Ca 2. Go to Column C2. Tab. Bob_Object_ID = Tab.Object_ID and C 2.Name = 'Detfired' where tabs Name 'table [_]%' and (c 1.name is tap or c 2. the name is null)
Comments
Post a Comment