I'm using SQL Server 2008 R2 and am trying to query it using Google Apps Script. I'm using one of the suggested scripts that's supposed to populate a spreadsheet but i'm having problems connect with SQL.
I've checked the port i've specified is correct so I doubt the issue is that, i'd really appreciate confirmation that my connection string is right.
Here's the code -
server/instance:1433;databaseName=xxxx;user=xxxx;password=xxxx");
function foo() {
var conn = Jdbc.getConnection ("jdbc:sqlserver/instance:1433;databaseName=xxxx;user=xxxx;password=xxxx");
var stmt = conn.createStatement();
stmt.setMaxRows(100);
var start = new Date();
var rs = stmt.executeQuery("select top 1000 from SOP10100");
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange('a1');
var row = 0;
while(rs.next()) {
cell.offset(row, 0).setValue(rs.getString(1));
cell.offset(row, 1).setValue(rs.getString(2));
cell.offset(row, 2).setValue(rs.getString(3));
cell.offset(row, 3).setValue(rs.getString(4));
row++;
}
rs.close();
stmt.close();
conn.close();
var end = new Date();
Logger.log("time took: " + (end.getTime() - start.getTime()));
Thanks for your help!
I think your Connection string is slightly malformed. I believe you need a
sqlserver://
before your servername/instance.I don't have a SQL Server instance handy but I tested the following code against SQL Azure and it worked well. I was able to select data from a table called helloworld and write it to a spreadsheet.
I've replaced my credentials with placeholders.
I have an entire project on GitHub that is a Mysql & SQL Server JDBC connection app. You can find it here: GitHub Project . I have posted the related code below. The the SQL Server case is where you instantiate the parameters for your connection and then below is the rest of the connection. To get an entire tool use the GitHub Project.
I've cracked it :).... For our SQL Server and google spreadsheets at least!
Thanks for all the help. I've been reading some of the issues here and now I'd like to share what you've helped me build. I've build a function that can take an SQL query and output the results to the spreadsheet its in.
To get the data on the spreadsheet you'll need to open a google spreadsheet and go 'Tool' > '<> script editor'. Then past the code bellow stating with and including "/****.....". Next you will have to update these bits in the code
Then you need to save it and return to the spread sheet and enter
Into the spreadsheet cell