Created
January 31, 2025 09:05
-
-
Save yoshiokatsuneo/89e007f2018dc85a04a461bd8d1fd5f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace procedure f.assert_table_primary_key(table_name string, key_name string) | |
begin | |
execute immediate ''' | |
with | |
results as ( | |
select | |
(select count(*) from ''' || table_name || ''') as cnt, | |
(select count(distinct ''' || key_name || ''') from ''' || table_name || ''') as cnt_distinct, | |
) | |
select * from results | |
where | |
if(cnt = cnt_distinct, true, error(' ''' || table_name || '''.''' || key_name || ''' is not unique key. count: ' || cnt || ' . unique count: ' || cnt_distinct || '.')) | |
'''; | |
end; | |
-- call f.assert_table_primary_key('yoshioka_test1.tbl', 'id'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment