Skip to content

Instantly share code, notes, and snippets.

@yoshiokatsuneo
Created January 31, 2025 09:05
Show Gist options
  • Save yoshiokatsuneo/89e007f2018dc85a04a461bd8d1fd5f5 to your computer and use it in GitHub Desktop.
Save yoshiokatsuneo/89e007f2018dc85a04a461bd8d1fd5f5 to your computer and use it in GitHub Desktop.
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