SELECT INTO Statements
You can use the SELECT INTO statement to export formatted data to a file.
Data Export with an SQL Query
The following query exports data into a file formatted in comma-separated values (CSV):
SELECT sys_id, priority INTO [csv://incident.txt] FROM [incident] WHERE category = 'request'You can specify other formats in the file URI. The possible delimiters are tab, semicolon, and comma with the default being a comma. The following example exports tab-separated values:
SELECT sys_id, priority INTO [csv://incident.txt;delimiter=tab] FROM [incident] WHERE category = 'request'
C#
String connectionString = "OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;"; using (ServiceNowConnection connection = new ServiceNowConnection(connectionString)) { ServiceNowCommand cmd = new ServiceNowCommand("SELECT sys_id, priority INTO [csv://incident.txt] FROM [incident] WHERE category = 'request' ", connection); int rowsAffected = cmd.ExecuteNonQuery(); }
VB.NET
Dim connectionString As [String] = "OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;" Using connection As New ServiceNowConnection(connectionString) Dim cmd As New ServiceNowCommand("SELECT sys_id, priority INTO [csv://incident.txt] FROM [incident] WHERE category = 'request'", connection) Dim rowsAffected As Integer = cmd.ExecuteNonQuery() End UsingYou can specify other file formats in the URI. The following example exports tab-separated values:
C#
String connectionString = "OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;"; using (ServiceNowConnection connection = new ServiceNowConnection(connectionString)) { ServiceNowCommand cmd = new ServiceNowCommand("SELECT * INTO [incident] IN [csv://filename=c:/incident.csv;delimiter=tab] FROM [incident] WHERE category = 'request'", connection); int rowsAffected = cmd.ExecuteNonQuery(); }
VB.NET
Dim connectionString As [String] = "OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;" Using connection As New ServiceNowConnection(connectionString) Dim cmd As New ServiceNowCommand("SELECT * INTO [incident] IN [csv://filename=c:/incident.csv;delimiter=tab] FROM [incident] WHERE category = 'request'", connection) Dim rowsAffected As Integer = cmd.ExecuteNonQuery() End Using