I'm an intermediate with MS SQL so i'm looking for some feedback on how to optimize my current script.
The Objective is to check if customers of 3rd-party software maintain certain fields in their database or if those fields are always holding their default values.
So, this is a sample of what i came up with: (I like the temporary table because it allows me to do some further inqueries)
create table #TableUsage
(
TableName varchar(100) NOT NULL,
FieldName varchar(100) NOT NULL,
Used bit NOT NULL
)
insert into #TableUsage (TableName, FieldName, Used)
values
('Value Entry', 'Description', iif(exists (select top 1 [Entry No_] from [CRONUS CH$Value Entry$437dbf0e-84ff-417a-965d-ed2bb9650972] r where r.[Description] <> ''),1,0)),
('Value Entry', 'Item Ledger Entry Type', iif(exists (select top 1 [Entry No_] from [CRONUS CH$Value Entry$437dbf0e-84ff-417a-965d-ed2bb9650972] r where r.[Item Ledger Entry Type] <> 0),1,0)),
('Value Entry', 'Valued Quantity', iif(exists (select top 1 [Entry No_] from [CRONUS CH$Value Entry$437dbf0e-84ff-417a-965d-ed2bb9650972] r where r.[Valued Quantity] <> 0),1,0)),
('Value Entry', 'Type', iif(exists (select top 1 [Entry No_] from [CRONUS CH$Value Entry$437dbf0e-84ff-417a-965d-ed2bb9650972] r where r.[Type] <> 0),1,0))
select * from #TableUsage where Used = 0
I'm generating those SQL Queries from other datasets. So, it doesn't have to be generic... But I'm not really confident that the iif(exists (select top 1 [Entry No_] from [CRONUS CH$Value Entry$437dbf0e-84ff-417a-965d-ed2bb9650972] r where r.[Type] <> 0),1,0) element for each field in a table is the most efficient way to do this.
(And it has to analyze more than 200 Tables, 3000 Fields on databases up to 1.5TB in size)
Thanks for your input in advance.