我正在創建一個腳本,它允許我提取有關數據庫中表的數據質量的一些基本度量。我們這樣做的方式是計算每列中存在的空白字段,空值等。我有兩個截然不同的問題。第一個處理錯誤處理,第二個處理SQL I/O。SQL錯誤處理和使用Embarcadero DBArtison 9.0輸出到csv/txt
錯誤處理: 我已經創建了一個光標,將遍歷所有提供的表和列。對於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表(表名VARCHAR(100),columnNameToUpdate VARCHAR(50))
感謝您的幫助,
RP
--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(' [email protected] + ') as Max_Value_' [email protected] +', ' +
'
min(' + @columnName + ') as Min_Value_' [email protected] + ', ' +
'
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(' [email protected] + ') as Max_Value``
你用什麼來運行腳本? –
Embarcadero DBArtison 9.0 – newToProgramming