Last active
February 23, 2018 11:56
-
-
Save JohnLBevan/cb720fa8920a74003b25df5346a60b3f to your computer and use it in GitHub Desktop.
Create an MS SQL Server table definition from Service Now using the ODBC driver. Inspired by discussion on this thread: https://community.servicenow.com/community?id=community_question&sys_id=50705b29dbdcdbc01dcaf3231f9619e6
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
| with sn (tableName, columnName, columnNameUI, max_length, internal_type) as | |
| ( | |
| --See https://docs.servicenow.com/bundle/kingston-application-development/page/integrate/odbc-driver/concept/c_ODBCDrvrSQL20082012.html for info on ODBC driver & setting up a linked server | |
| select name, element, column_label, max_length, internal_type | |
| from openquery(SERVICENOW , ' | |
| select name | |
| , element --database column name | |
| , column_label --UI name | |
| ,CAST(max_length as Decimal(38,0)) max_length --could convert to string here; but this makes it more reusable | |
| ,internal_type | |
| from sys_dictionary | |
| where name in (''sys_user'', ''task'') --amend to only output those tables you want; included here since queries can be slow | |
| ' ) | |
| where element > '' | |
| ) | |
| select 'create table ' + quotename(tableName) + '(' | |
| + stuff(( | |
| SELECT char(10) + char(9) + ',' + quotename(columnName) + ' ' | |
| + case internal_type | |
| when 'boolean' then 'bit' | |
| when 'choice' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'collection' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'domain_id' then 'nvarchar(32)' --same as GUID | |
| when 'email' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'float' then 'float' | |
| when 'glide_date' then 'date' | |
| when 'glide_date_time' then 'datetime' | |
| when 'GUID' then 'nvarchar(32)' --'uniqueidentifier' --could hold as uniqueidentifer, but then we'd need to convert values like so when populating: cast(stuff(stuff(stuff(stuff(COLUMN_NAME,21,0,'-'),17,0,'-'),13,0,'-'),9,0,'-') as uniqueidentifier) | |
| when 'integer' then 'int' | |
| when 'multi_two_lines' then 'nvarchar(' + cast(max_length as nvarchar) + ')' | |
| when 'numeric' then 'numeric(38,0)' --not sure if we need to do something special for precision... | |
| when 'password' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'ph_number' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'reference' then 'nvarchar(32)' --same as GUID | |
| when 'string' then 'nvarchar(' + cast(max_length as nvarchar) + ')' | |
| when 'sys_class_name' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'url' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'user_image' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'user_roles' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| else 'nvarchar(max) /* todo: define type mapping for ''' + internal_type + ''' */' | |
| end | |
| + ' /* ' + replace(columnNameUI,'*/','* /') + ' */ ' | |
| from sn c | |
| where c.tableName = t.tableName | |
| FOR XML PATH(''), TYPE | |
| ).value('.','varchar(max)'),3,1,' ') | |
| + ' | |
| )' | |
| from sn t | |
| group by t.tableName |
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
| with sn (tableName, columnName, max_length, internal_type) as | |
| ( | |
| --See https://docs.servicenow.com/bundle/kingston-application-development/page/integrate/odbc-driver/concept/c_ODBCDrvrSQL20082012.html for info on ODBC driver & setting up a linked server | |
| select name, element, max_length, internal_type | |
| from openquery(SERVICENOW , ' | |
| select name | |
| , element --database column name | |
| ,CAST(max_length as Decimal(38,0)) max_length --could convert to string here; but this makes it more reusable | |
| ,internal_type | |
| from sys_dictionary | |
| where name in (''sys_user'', ''task'') --amend to only output those tables you want; included here since queries can be slow | |
| ' ) | |
| where element > '' | |
| ) | |
| select 'insert into ' + quotename(tableName) + '(' | |
| + stuff(( | |
| SELECT char(10) + char(9) + ',' + quotename(columnName) + ' ' | |
| from sn c | |
| where c.tableName = t.tableName | |
| FOR XML PATH(''), TYPE | |
| ).value('.','varchar(max)'),3,1,' ') | |
| + char(10) + ') | |
| select * | |
| from openquery(SERVICENOW , '' | |
| select ' | |
| + stuff(( | |
| SELECT char(10) + char(9) + ', cast(' + quotename(columnName) + ' as ' | |
| + case internal_type | |
| when 'boolean' then 'bit' | |
| when 'choice' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'collection' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'domain_id' then 'nvarchar(32)' --same as GUID | |
| when 'email' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'float' then 'float' | |
| when 'glide_date' then 'date' | |
| when 'glide_date_time' then 'datetime' | |
| when 'GUID' then 'nvarchar(32)' --'uniqueidentifier' --could hold as uniqueidentifer, but then we'd need to convert values like so when populating: cast(stuff(stuff(stuff(stuff(COLUMN_NAME,21,0,'-'),17,0,'-'),13,0,'-'),9,0,'-') as uniqueidentifier) | |
| when 'integer' then 'int' | |
| when 'multi_two_lines' then 'nvarchar(' + cast(max_length as nvarchar) + ')' | |
| when 'numeric' then 'numeric(38,0)' --not sure if we need to do something special for precision... | |
| when 'password' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'ph_number' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'reference' then 'nvarchar(32)' --same as GUID | |
| when 'string' then 'nvarchar(' + cast(max_length as nvarchar) + ')' | |
| when 'sys_class_name' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'url' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'user_image' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| when 'user_roles' then 'nvarchar(' + cast(max_length as nvarchar) + ')' --just a string | |
| else 'nvarchar(max) /* todo: define type mapping for ''' + internal_type + ''' */' | |
| end | |
| + ' ) ' | |
| from sn c | |
| where c.tableName = t.tableName | |
| FOR XML PATH(''), TYPE | |
| ).value('.','varchar(max)'),1,3,'') | |
| + ' | |
| from ' + quotename(tableName) + ' | |
| '')' | |
| from sn t | |
| group by t.tableName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment