{"id":1656,"date":"2024-09-01T13:23:46","date_gmt":"2024-09-01T07:53:46","guid":{"rendered":"https:\/\/adilfahim.com\/myblog\/?p=1656"},"modified":"2024-09-01T13:23:46","modified_gmt":"2024-09-01T07:53:46","slug":"sap-hana-sql","status":"publish","type":"post","link":"https:\/\/adilfahim.com\/myblog\/sap-hana-sql\/","title":{"rendered":"SAP HANA SQL Commands for Auditing\/SOX"},"content":{"rendered":"<h2>By using SAP HANA SQL Commands, we can perform following job<\/h2>\n<p><b>Finding index and primary keys of tables<\/b><\/p>\n<p>SELECT IFNULL(CONSTRAINT,&#8217;NUNIQUE&#8217;),<br \/>\nINDEX_NAME,COLUMN_NAME<br \/>\nFROM INDEX_COLUMNS WHERE SCHEMA_NAME = &#8216;%s&#8217;<br \/>\nAND TABLE_NAME = &#8216;%s&#8217;<br \/>\nORDER BY INDEX_OID,POSITION<\/p>\n<p><b>Show details of users that have been logged-in<\/b><\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;USERS&#8221; where &#8220;LAST_SUCCESSFUL_CONNECT&#8221; is not null order by 9 desc;<\/p>\n<p><b>To get all Libraries<\/b><\/p>\n<p>select to_char(definition) from public.libraries where schema_name=&#8217;EXT&#8217;;<\/p>\n<p><b>Check invalid custom DB views<\/b><\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;VIEWS&#8221; where schema_name not like &#8216;SAP%&#8217; and is_valid = &#8216;FALSE&#8217;;<\/p>\n<p><b>Check inactive custom DB objects<\/b><\/p>\n<p>select * from &#8220;_SYS_REPO&#8221;.&#8221;INACTIVE_OBJECT&#8221; where &#8220;PACKAGE_ID&#8221; not like &#8216;sap%&#8217;;<\/p>\n<p><b>Check which SAP language settings are being used by current user<\/b><\/p>\n<p>select session_context(&#8216;LOCALE_SAP&#8217;), session_context(&#8216;LOCALE&#8217;) from dummy;<\/p>\n<h2>Search executed SAP HANA SQL statements &#8211;<\/h2>\n<p><b>To find out who deleted a table<\/b><\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;M_EXECUTED_STATEMENTS&#8221; where &#8220;STATEMENT_STRING&#8221; LIKE &#8216;DROP TABLE%&#8217;;<\/p>\n<p><b>Show custom settings within global.ini and indexserver.ini<\/b><\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;M_INIFILE_CONTENTS&#8221; where (&#8220;LAYER_NAME&#8221; = &#8216;SYSTEM&#8217; or &#8220;HOST&#8221; <> &#8220;)<br \/>\nand (&#8220;FILE_NAME&#8221; = &#8216;global.ini&#8217; or &#8220;FILE_NAME&#8221; = &#8216;indexserver.ini&#8217;);<\/p>\n<p><b>Show assigned user roles<\/b><\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;GRANTED_ROLES&#8221; where &#8220;GRANTEE_TYPE&#8221; = &#8216;USER&#8217;;<\/p>\n<p><b>Show assigned repository privileges<\/b><\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;GRANTED_PRIVILEGES&#8221; where object_type = &#8216;REPO&#8217;;<\/p>\n<p><b>Show objects owned by non-system users<\/b><\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;OWNERSHIP&#8221; where owner_name not like &#8216;SAP%&#8217; and owner_name not like &#8216;%SYS%&#8217;<br \/>\norder by 1,2;<\/p>\n<p><b>Show Corresponding Tables referring to which Schema<\/b><\/p>\n<p>SELECT BASE_SCHEMA_NAME, BASE_OBJECT_NAME,DEPENDENT_SCHEMA_NAME,<br \/>\nDEPENDENT_OBJECT_NAME,DEPENDENT_OBJECT_TYPE<br \/>\nFROM &#8220;SYS&#8221;.&#8221;OBJECT_DEPENDENCIES&#8221;<br \/>\nWHERE BASE_SCHEMA_NAME = &#8216;TCMP&#8217; \/* <== Schema Name *\/\nAND BASE_OBJECT_NAME = 'CS_SALESORDER' \/* <== Table Name *\/\n\n<b>Analyze expensive statement trace<\/b><\/p>\n<p>select<br \/>\nto_varchar(&#8220;STATEMENT_START_TIME&#8221;,&#8217;DD.MM.YYYY&#8217;) &#8220;EXEC_DATE&#8221;,<br \/>\nto_varchar(&#8220;STATEMENT_START_TIME&#8221;,&#8217;HH24:MI:SS&#8217;) &#8220;EXEC_TIME&#8221;,<br \/>\nto_int(&#8220;DURATION_MICROSEC&#8221;\/1000000) &#8220;DURATION_S&#8221;,<br \/>\nto_decimal(&#8220;MEMORY_SIZE&#8221;\/1073741824,10,1) &#8220;MEM_GB&#8221;,<br \/>\n&#8220;RECORDS&#8221;,<br \/>\n&#8220;DB_USER&#8221;,<br \/>\n&#8220;APP_USER&#8221;,<br \/>\n&#8220;APPLICATION_NAME&#8221;,<br \/>\n&#8220;STATEMENT_STRING&#8221;,<br \/>\nlength(&#8220;STATEMENT_STRING&#8221;) &#8220;SQL_LENGTH&#8221;,<br \/>\nOCCURRENCES_REGEXPR(&#8216;JOIN&#8217; FLAG &#8216;i&#8217; IN &#8220;STATEMENT_STRING&#8221;) &#8220;JOIN&#8221;,<br \/>\nOCCURRENCES_REGEXPR(&#8216;CASE&#8217; FLAG &#8216;i&#8217; IN &#8220;STATEMENT_STRING&#8221;) &#8220;DISTINCT&#8221;,<br \/>\n&#8220;ERROR_TEXT&#8221;,<br \/>\n&#8220;PARAMETERS&#8221;<br \/>\nfrom &#8220;SYS&#8221;.&#8221;M_EXPENSIVE_STATEMENTS&#8221;<br \/>\nwhere &#8220;OPERATION&#8221; in<br \/>\n(&#8216;INSERT&#8217;,&#8217;SELECT&#8217;,&#8217;AGGREGATED_EXECUTION&#8217;) \u2013exclude background activity<br \/>\nand &#8220;RECORDS&#8221; > 0<br \/>\nand to_varchar(&#8220;STATEMENT_START_TIME&#8221;, &#8216;YYYYMMDD&#8217;) = current_date<br \/>\nand to_int(to_varchar(&#8220;STATEMENT_START_TIME&#8221;,&#8217;HH24\u2032))<br \/>\nbetween 8 and 17 \u2013business hours<br \/>\norder by 3 desc;<\/p>\n<p>Benefits of HANA <a href=\"https:\/\/adilfahim.com\/myblog\/sap-hana-audit-trail-benefits-and-use-cases\/\" target=\"_blank\" rel=\"noopener\">Auditing<\/a><\/p>\n<p><b>Most Common Functions used in Implementation<\/b><\/p>\n<p>SELECT SESSION_USER &#8220;session user&#8221; FROM DUMMY;<\/p>\n<p>SELECT TO_DATE(&#8216;2010-01-12&#8217;, &#8216;YYYY-MM-DD&#8217;) &#8220;to date&#8221; FROM DUMMY;<\/p>\n<p>SELECT TRIM (&#8216;a&#8217; FROM &#8216;aaa123456789aa&#8217;) &#8220;trim both&#8221; FROM DUMMY;<\/p>\n<p>SELECT CURRENT_DATE &#8220;current date&#8221; FROM DUMMY;<\/p>\n<p>SELECT<br \/>\nDAYS_BETWEEN (TO_DATE (&#8216;2009-12-05&#8217;, &#8216;YYYY-MM-DD&#8217;),<br \/>\nTO_DATE(&#8216;2010-01-05&#8217;, &#8216;YYYY-MM-DD&#8217;)) &#8220;days between&#8221;<br \/>\nFROM DUMMY;<\/p>\n<p>SELECT UPPER (&#8216;Ant&#8217;) &#8220;uppercase&#8221; FROM DUMMY;<\/p>\n<p>SELECT CONCAT (&#8216;C&#8217;, &#8216;at&#8217;) &#8220;concat&#8221; FROM DUMMY;<\/p>\n<p>SELECT FLOOR (14.5) &#8220;floor&#8221; FROM DUMMY;<\/p>\n<p>SELECT TO_DECIMAL(7654321.888888, 10, 3) &#8220;to decimal&#8221; FROM DUMMY;<\/p>\n<p>SELECT REPLACE (&#8216;DOWNGRADE DOWNWARD&#8217;,&#8217;DOWN&#8217;, &#8216;UP&#8217;) &#8220;replace&#8221; FROM DUMMY;<\/p>\n<p>SELECT RTRIM (&#8216;endabAabbabab&#8217;,&#8217;ab&#8217;) &#8220;rtrim&#8221; FROM DUMMY;<\/p>\n<p>SELECT RIGHT(&#8216;HI0123456789&#8217;, 20) &#8220;right&#8221; FROM DUMMY;<\/p>\n<p>SELECT  WEEK (&#8216;2017-01-02&#8217;) FROM DUMMY;<\/p>\n<p>SELECT LENGTH (&#8216;length in char&#8217;) &#8220;length&#8221; FROM DUMMY;<\/p>\n<p>SELECT SUBSTRING (&#8216;1234567890&#8217;,4,2) &#8220;substring&#8221; FROM DUMMY;<\/p>\n<p>SELECT WEEKDAY (TO_DATE (&#8216;2010-12-31&#8217;, &#8216;YYYY-MM-DD&#8217;)) &#8220;week day&#8221; FROM DUMMY;<\/p>\n<p>SELECT TO_VARCHAR (TO_DATE(&#8216;2009-12-31&#8217;), &#8216;YYYY\/MM\/DD&#8217;) &#8220;to varchar&#8221; FROM DUMMY;<\/p>\n<p>SELECT<br \/>\nYEARS_BETWEEN(TO_DATE(&#8216;2001-01-01&#8217;),<br \/>\nTO_DATE(&#8216;2003-03-14&#8217;)) &#8220;years_between&#8221;<br \/>\nFROM DUMMY;<\/p>\n<p>SELECT YEAR (TO_DATE (&#8216;2011-05-30&#8217;, &#8216;YYYY-MM-DD&#8217;)) &#8220;year&#8221; FROM DUMMY;<\/p>\n<p>SELECT MONTH (&#8216;2011-05-30&#8217;) &#8220;month&#8221; FROM DUMMY;<\/p>\n<p>SELECT NOW () &#8220;now&#8221; FROM DUMMY;<\/p>\n<p>SELECT LAST_DAY (TO_DATE(&#8216;2010-01-04&#8217;, &#8216;YYYY-MM-DD&#8217;)) &#8220;last day&#8221; FROM DUMMY;<\/p>\n<p>SELECT DAYOFYEAR (&#8216;2021-05-30&#8217;) &#8220;dayofyear&#8221; FROM DUMMY;<\/p>\n<h2>SAP HANA SQL Commands for SOX\/AUDIT Requirements<\/h2>\n<p>(Make sure auditing is enabled)<\/p>\n<p>select * from audit_log where user_name=&#8217;USER&#8217; and client_host LIKE &#8216;HOST%&#8217; and (timestamp)>&#8217;2022-07-31 00:00:00&#8242; order by 1;<\/p>\n<p>select * from &#8220;SYS&#8221;.&#8221;M_CONNECTIONS&#8221; where client_host NOT LIKE &#8216;HOST%&#8217; and USER_NAME LIKE &#8216;USER%&#8217; and AUTHENTICATION_METHOD NOT LIKE &#8216;INTERNAL&#8217;;<\/p>\n<p>select * from all_audit_log where user_name=&#8217;USER&#8217; and EVENT_ACTION NOT LIKE &#8216;CONN%&#8221; and (timestamp)&#8217;2022-03-29 07:00:00&#8242; order by 1;<\/p>\n<p>select * from &#8220;PUBLIC&#8221;.&#8221;EFFECTIVE_ROLES&#8221; where USER_NAME=&#8217;USER&#8217;;<\/p>\n<p>select * from audit_log where AUDIT_POLICY_NAME=&#8217;Grant&#8217;;<\/p>\n<p>select * from audit_log where AUDIT_POLICY_NAME=&#8217;USER&#8217; and EVENT_ACTION like &#8216;DROP%&#8217; and GRANTEE=&#8217;USER&#8217;;<\/p>\n<p>Read here Auditing in <a href=\"https:\/\/learning.sap.com\/learning-journeys\/provision-and-administer-databases-in-sap-hana-cloud\/auditing-in-sap-hana-cloud_a6522675-08c8-45ac-801d-a2f7afe00550\" target=\"_blank\" rel=\"noopener\">SAP HANA Cloud<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By using SAP HANA SQL Commands, we can perform following job Finding index and primary keys of tables SELECT IFNULL(CONSTRAINT,&#8217;NUNIQUE&#8217;), INDEX_NAME,COLUMN_NAME FROM INDEX_COLUMNS WHERE SCHEMA_NAME = &#8216;%s&#8217; AND TABLE_NAME = &#8216;%s&#8217; ORDER BY INDEX_OID,POSITION Show details of users that have been logged-in select * from &#8220;SYS&#8221;.&#8221;USERS&#8221; where &#8220;LAST_SUCCESSFUL_CONNECT&#8221; is not null order by 9 desc; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[1137,1140,1138,1139],"class_list":["post-1656","post","type-post","status-publish","format-standard","hentry","category-sap-updates","tag-sap-hana-auditing","tag-sap-hana-security","tag-sap-hana-sox","tag-sap-hana-sql-commands"],"_links":{"self":[{"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/posts\/1656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/comments?post=1656"}],"version-history":[{"count":1,"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/posts\/1656\/revisions"}],"predecessor-version":[{"id":1657,"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/posts\/1656\/revisions\/1657"}],"wp:attachment":[{"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/media?parent=1656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/categories?post=1656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adilfahim.com\/myblog\/wp-json\/wp\/v2\/tags?post=1656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}