我正在创建一个脚本,它允许我提取一些关于数据库中表的数据质量的基本指标。我们这样做的方法是计算每列中存在的空白字段、空值等。我有两个明显的问题。第一个处理错误处理,第二个处理 SQL I/O。
错误处理: 我创建了一个光标,它将遍历提供的所有表和列。对于 90% 的列,我需要将其转换为 LONG,但是其他一些列有字母,所以我得到一个无效的转换错误。如何设置一个 if 语句,在转换之前将列的数据类型与 Varchar/Text 进行比较,以免出现错误?
SQL 输出:运行光标后,我需要将数据移动到 csv 或 txt 文件。我只有对数据库的读取权限,所以我不能编写程序或做 BCP。有没有办法解决这个问题?
--Declare a table variable to hold your table names (and column names in case needed)
声明 @listOfTablesToUpdate 表 (tableName varchar(100), columnNameToUpdate varchar(50))
谢谢你的帮助,
反相
--insert the tables that you want to work with.
--insert into @listOfTablesToUpdate values ('customer_ref', 'aml_rec_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'customer_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'bank_num')
insert into @listOfTablesToUpdate values ('customer_ref', 'peer_grp_cd')
insert into @listOfTablesToUpdate values ('customer_ref', 'cust_crd_rating')
insert into @listOfTablesToUpdate values ('customer_ref', 'cust_income')
insert into @listOfTablesToUpdate values ('customer_ref', 'cust_net_worth')
insert into @listOfTablesToUpdate values ('customer_ref', 'cust_invest_obj')
insert into @listOfTablesToUpdate values ('customer_ref', 'last_ofac_scan')
insert into @listOfTablesToUpdate values ('customer_ref', 'relationship_mgr_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'relationship_typ_cd')
insert into @listOfTablesToUpdate values ('customer_ref', 'tss_kyc_risk_rating')
insert into @listOfTablesToUpdate values ('customer_ref', 'cust_addr_link_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'customer_parent_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'status_cd')
insert into @listOfTablesToUpdate values ('customer_ref', 'cust_description_cd')
insert into @listOfTablesToUpdate values ('customer_ref', 'fin_entity')
insert into @listOfTablesToUpdate values ('customer_ref', 'ultimate_parent_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'cust_create_date')
insert into @listOfTablesToUpdate values ('customer_ref', 'si_lob_cd')
insert into @listOfTablesToUpdate values ('customer_ref', 'ges_ucn_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'employee_ind')
insert into @listOfTablesToUpdate values ('customer_ref', 'nds_lob_cd')
insert into @listOfTablesToUpdate values ('customer_ref', 'lcl_fin_entity')
insert into @listOfTablesToUpdate values ('customer_ref', 'business_line')
insert into @listOfTablesToUpdate values ('customer_ref', 'system_feeder_id')
insert into @listOfTablesToUpdate values ('customer_ref', 'last_update_date')
--Cursor for iterating
declare @tableCursor cursor,
@tableName varchar(100),
@tableName_ret varchar(100),
@columnName varchar(50)
set @tableCursor = cursor for select tableName,columnNameToUpdate from @listOfTablesToUpdate
open @tableCursor
fetch next from @tableCursor into @tableName, @columnName
while(@@fetch_status = 0)
begin
--dynamic sql
declare @sqlCommand varchar(1000)
declare @sqlCommand2 varchar(1000)
--Your logic here...this is just an example
Set @sqlCommand = 'Select
max(' +@columnName + ') as Max_Value_' +@columnName +', ' +
'
min(' + @columnName + ') as Min_Value_' +@columnName + ', ' +
'
Cast(sum (case when Cast(' + @columnName + ' as FLOAT) =0 then 1.0 else 0 end) as FLOAT) as ZeroCount, ' +
'
Count(Distinct(' + @columnName + ')) as DistinctCount
from AML.dbo.' + @tableName
Exec(@sqlCommand)
Set @sqlCommand2 = 'Select
count(*) as Null_SpaceCount
from AML.dbo.' + @tableName + '
where (' + @columnName + ' is null) or (len(ltrim(' + @columnName + '))=0 or len(rtrim(' + @columnName + '))=0)'
Exec(@sqlCommand2)
fetch next from @tableCursor into @tableName, @columnName
end
close @tableCursor
deallocate @tableCursor
-- max(' +@columnName + ') as Max_Value``