I am trying to connect to a PostgreSQL database table from Excel via the PostgreSQL ODBC 32-bit driver.
In Excel, I go to Data>Get Data> From Other Sources> From ODBC. I navigate to the ODBC data source I set up, enter the credentials, and it clearly connects as the available tables appear. The preview fails and the query fails when I hit "Load" giving the error:
DataSource.Error: ODBC: ERROR [HY000] Error while executing the query
Details:
DataSourceKind=Odbc
DataSourcePath=dsn=PostgreSQL
OdbcErrors=Table
When I test the connection in ODBC admin it is successful. I have tried both the ANSI and Unicode drivers. TIBCO Spotfire connects to the ODBC datasource and pulls the data in just fine.
Any help you can provide would be greatly appreciated.
This appears to be a bug with the latest psqlODBC driver, which is psqlodbc_09_06_0500 at the time I'm writing this. I have access to my PostgreSQL server logs. Here's the error message and the offending query:
ERROR: syntax error at or near "ta" at character 553
STATEMENT: select ta.attname, ia.attnum, ic.relname, n.nspname, tc.relname from pg_catalog.pg_attribute ta, pg_catalog.pg_attribute ia, pg_catalog.pg_class tc, pg_catalog.pg_index i, pg_catalog.pg_namespace n, pg_catalog.pg_class ic where tc.relname = 'rates' AND n.nspname = 'public' AND tc.oid = i.indrelid AND n.oid = tc.relnamespace AND i.indisprimary = 't' AND ia.attrelid = i.indexrelid AND ta.attrelid = i.indrelid AND ta.attnum = i.indkey[ia.attnum-1] AND (NOT ta.attisdropped) AND (NOT ia.attisdropped) AND ic.oid = i.indexrelid order by ia.attnumselect ta.attname, ia.attnum, ic.relname, n.nspname, NULL from pg_catalog.pg_attribute ta, pg_catalog.pg_attribute ia, pg_catalog.pg_class ic, pg_catalog.pg_index i, pg_catalog.pg_namespace n where ic.relname = 'rates_pkey' AND n.nspname = 'public' ANDic.oid = i.indexrelid AND n.oid = ic.relnamespace AND ia.attrelid = i.indexrelid AND ta.attrelid = i.indrelid AND ta.attnum = i.indkey[ia.attnum-1] AND (NOT ta.attisdropped) AND (NOT ia.attisdropped) order by ia.attnum
Here's the context around character 553: order by ia.attnumselect ta.attname, ia.attnum. Note that it's missing a comma between two field names.
I was able to get it working with psqlodbc_09_06_0200, which is about a year old. Since it sounds like you use 32-bit Office, you can download psqlodbc_09_06_0200-x86.zip from https://www.postgresql.org/ftp/odbc/versions/msi/. (Use x64 if you have 64-bit Office installed.)
You might be able to experiment with driver versions between psqlodbc_09_06_0200-x86.zip and psqlodbc_09_06_0500-x86.zip as the bug was presumably introduced somewhere between those two versions.
You can skip this error by removing the navigate step and querying the information you need throw an SQL statement.
For example if you want to retry some information from LineItemExport the default query is something like this:
let
Source = Odbc.DataSource("dsn=name", [HierarchicalNavigation=true]),
quickbase_Database = Source{[Name="quickbase",Kind="Database"]}[Data],
public_Schema = quickbase_Database{[Name="public",Kind="Schema"]}[Data],
LineItemExport = public_Schema{[Name="LineItemExport",Kind="View"]}[Data]
in
LineItemExport
You have to change the source step in the query editor or change the code to something like this:
let
Source = Odbc.Query("dsn=name", "select * from public.LineItemExport")
in
Source
This is the way you can came over the error, otherwise you should try to change your drivers version.
Related
I know that this is a really long post but I'm not sure of what part of my process is making my file crash, so I tried to detail everything about what I did to get to the error messages.
So, first of all, I created a query on Kusto, which looks something similar to this but in reality is 160 lines of code, this is just a summarized version of what my code might do just to show my working process.
First, what I do in Session_Id_List is create a list of all distinct Session Id's from the past day.
Then on treatment_alarms1 I count the amount of alarms for each type of alarm that was active during each session.
Then, on treatment_alarms2 I create a list which might look something like this
1x Alarm_Type_Number1
30x Alarm_Type_Number2
7x Alarm_Type_Number3
and like that for each treatment, so I have a list of all alarms that were active for that treatment.
Lastly, I create a left outer join with Session_Id_List and treatment_alarms2. This means that I will get shown all of the treatment ID's, even the ones that did not have any active alarms.
let _StartTime = ago(1d);
let _EndTime = ago(0d);
let Session_Id_List = Database1
| where StartTime >= _StartTime and StartTime <= _EndTime
| summarize by SessionId, SerialNumber, StartTime
| distinct SessionId, StartTime, SerialNumber;
let treatment_alarms1 = Database1
| where StartTime >= _StartTime and StartTime <= _EndTime and TranslatedData_Status == "ALARM_ACTIVE"
| summarize number_alarms = count() by TranslatedData_Value, SessionId
| project final_Value = strcat(number_alarms, "x ", TranslatedData_Value), SessionId;
let treatment_alarms2 = Database1
| where StartTime >= _StartTime and StartTime <= _EndTime and TranslatedData_Status == "ALARM_ACTIVE"
| join kind=inner treatment_alarms1 on SessionId
| summarize list_of_alarms_all = make_set(final_Value) by SessionId
| project SessionId, list_of_alarms_all;
let final_join = Session_Id_List
| join kind=leftouter treatment_alarms2 on SessionId;
final_join
| project SessionId, list_of_alarms_all
Then I put this query into Excel, by using the following method
I go to Tools -> Query to Power BI on Kusto Explorer
I go to Data -> Get Data -> From Other Sources -> Blank Query
I go to advanced editor
I copy and paste my query and press "Done" at the bottom
If you see now, the preview of my data will show "List" on the list_of_alarms_all column, rather than showing me the actual values of the list.
To fix this issue I first press the arrows on the header of the column
I press on "Extract Values"
I select Custom -> Concatenate using special characters -> Line Feed -> Press OK
That works fine for all of the ID's that do have alarms on them, it shows them as a list and tells me how many there are, the issue is with the ID's that did not have any treatments where I get "Error" on the Excel preview. Once I press "Close & Load" the data is put on the worksheet and it looks fine, the "Error" are all gone and instead I get empty cells where the "Error" would be at.
The problem now starts when I close the file and try to open it again.
First I get this message. So I press yes to try and enter the file.
Then I get this other message. The problem with this message is that it says that I have the file open when that is not true. I even tried to restart my laptop and open the file again and I would still get the message when in reality I don't have that file open.
Then I get this message, telling me that the connection to the query was removed.
So my problem here is that 1) I can't edit the file anymore unless I make a copy because I keep getting the message saying that I already have the file opened and it is locked for editing and 2) I would like to refresh this query with VBA maybe once a week from now on but I can't because when I save the file the connection to the query is deleted by excel itself.
I'm not sure of why this is happening, I'm guessing it's because of the "Error" I get on the empty cells when I try to extract the values from the lists. If anybody has any information on how I can fix this so I don't get these error messages please let me know.
I was not able to reproduce your issue, however there are some things you might want to try.
Within ADX, you could wrap you query with a function, so you won't have to copy a large piece of code into your Excel.
You could deal with null values (this is what gives you the Error values) already in your query. Note the use of coalesce.
// I used datatable to mimic your query results
.create-or-alter function SessionAlarms()
{
datatable (SessionId:int,list_of_alarms_all:dynamic)
[
1, dynamic([10,20,30])
,2, dynamic([])
,3, dynamic(null)
]
| extend list_of_alarms_all = coalesce(list_of_alarms_all, dynamic([]))
}
You can use Power Query ADX connector and copy your query/function As Is
If you haven't dealt with null values in you KQL you can take care of the error in Excel by using Replace Errors
This is my first try at using the Power Query... I've build a "dynamic" query in which I can change the retrieved fields as well as the filtering fields and values to be used by the query.
It's working perfectly on my computer but as soon as I try to execute it on another computer, I get the "Please rebuild this data combination" error. I saw some post saying I'll have to kind of split my query but I have not been able to figure it out.
Here is what my 2 tables look like:
Condition and fields selection
and here is my Query with the error:
Query
This might not be very elegant, but it allow me, thru a VBA script, to generate the list of fields to be retrieved and to generate the condition to be used by the SQL.
Any idea why it's not working on the other computers or how to improved the solution I'm using?
Thank you!
Notes:
Hi, all my Privacy Level are already set to 'None'.
I've tried to parametrize my code but I can't figure how. The Where condition is dynamic: it could be Where Number = "1234" but in other condition, the where might be like: 'Where Assignee = "xyz"'.
Here is a simplified example of my code:
let
Source = Sql.Database("xxxx", "yyyy", [Query=
"Select network, testid
from CM3T1M1 "
& paramConditions[Conditions]{0} &
" "])
in
Source
rebuild query, Formula.Firewall
That's a feature to prevent prevent accidentally leaking data. You can change the privacy level to ignore it
See also: docs.microsoft/dataprivacyfirewall
Is the dynamic query inserting those cells into the SQL query ? Report Parameters are nice for letting the user change variables without having to re-edit the query.
Parameterized native SQL queries
from: https://blog.crossjoin.co.uk/2016/12/11/passing-parameters-to-sql-queries-with-value-nativequery-in-power-query-and-power-bi/
let
Source = Sql.Database("localhost", "Adventure Works DW"),
Test = Value.NativeQuery(
Source,
"SELECT * FROM DimDate
WHERE EnglishMonthName=#MonthName AND
EnglishDayNameOfWeek=#DayName",
[
MonthName = "March",
DayName = "Tuesday"
]
)
in
Test
Dynamic Power Query version of SQL Query
To dynamically generate this SQL Query
select NUMBER, REQUESTED_BY from SourceTable
where NUMBER = 404115
Table.SelectRows is your Where.
SelectColumns is your select
let
Source = ...,
filterByNum = 404115,
columnNames = {"NUMBER", "REQUESTED_BY"},
removedColumns = Table.SelectColumns(
Source, columnNames, MissingField.Error
),
// I used 'MissingField.Error' so you know right away
// if there's a typo or bug
// assuming you are comparing Source[NUMBER]
filteredTable = Table.SelectRows(
Source, each [NUMBER] = filterByNum
)
in
filteredTable
I'm trying the standard SELECT ... AS call to rename a column in query output with the Python Salesforce API and it's throwing following error:
... unexpected token: 'AS'", 'errorCode': 'MALFORMED_QUERY'}
So far most native language calls from SOQL have been working in the API and it seems, from here, that SELECT ... AS is valid SOQL.
Query outline:
from simple_salesforce import Salesforce
sf = Salesforce(username=myusername, password=mypassword, security_token=mytoken)
query = "select closedate as Date from opportunity"
query_list = sf.query_all(query)['records']
edit
error remains even after putting the new column name within quotes as advised in above link:
query = "select closedate as \"Date\" from Opportunity"
Thanks
As Terminus mentioned, SOQL field aliasing is not possible in SOQL in most contexts, including yours. The only case in which I have seen aliasing working in SOQL is in aggregate queries. For example, in apex you could write:
AggregateResult myResult = [SELECT count(Id) SpecialName FROM Contact];
system.debug(myResult);
and receive the result:
DEBUG|AggregateResult:{SpecialName=1630}
In python via simple-salesforce it would look like this:
sf.query_all('SELECT count(Id) SpecialName FROM Contact')
with the result:
OrderedDict([('totalSize', 1),
('done', True),
('records',
[OrderedDict([('attributes',
OrderedDict([('type', 'AggregateResult')])),
('SpecialName', 6587)])])])
Please mark as answered if this answer your question.
The syntax seems to be incorrect.
Try query = "select closedate <insert alias> from Opportunity" according to the alias notation documented here : https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_alias.htm
Hi there so bit of an odd one, the sql query im running is
SELECT [idPerson] FROM tblPersons WHERE [Email] LIKE 'Joe.Pike%'
If i run this in the server i get my correct personID, however when i run it in my code i get a random personID that doesn't exist, now what is even weirder is that my second bit of code queries based on the personID and that returns a value
UserName = row[1]+'.'+row[2]
Password = 'XXXXXXXX'
Email = UserName + '#XXXXXXX.org.uk'
emaillike = [str(row[1]+'.'+row[2]).replace("'", '')+"%"]
sql = ("""SELECT [idPerson] FROM tblPersons WHERE [Email] LIKE {}""").format(str(emaillike).replace("[","").replace("]",""))
PersonId = cursor.execute(sql).fetchone()
PersonPin = \
str(cursor.execute('SELECT [Pin] FROM tblPersonPins WHERE [PersonId] = ?'
, [PersonId[0]]).fetchone()).replace("'","").replace("(","").replace(")","").replace(",","")
print(sql)
print(emaillike)
print(str(PersonId))
print(PersonPin)
I have even tried to fetchall rows with the Email LIKE query but that still only returned one ID and that is the non existent 105524 value
The sql server is MSSQL server 2017 and im running Python 3.7.2
It also appears to be an issue with pyodbc as well as pypyodbc
My print statements return :
SELECT [idPerson] FROM tblPersons WHERE [Email] LIKE 'Joe.Pike%'
['Joe.Pike%']
(105524,)
7271
But the numbers should both be different and once again to confirm when I run the printed sql statement in sql server developer it returns the correct data.
thanks in Advance
P.S this is unfinished code so please ignore the terrible formatting (I also know i should not be using positional parameters for sql queries but I can't see any other way to achieve the end result)
OMG please shoot me!
Incorrect database, Man I wish I could Delete questions
I am new to C# and was experimenting a bit trying to insert stuff into a SQL Server CE database. I don't understand why this code is not working. I am getting a
System.Data.SqlServerCE.SqlCeException
error pointing to the com1.ExecuteNonQuery(); line when the program is run.
I am attaching my code that i am using to insert into the table.
//Connecting to SQL Server
SqlCeConnection conn1 = new SqlCeConnection();
conn1.ConnectionString = connection; //connection is a string variable which has the connection details
conn1.Open();
SqlCeCommand com1 = new SqlCeCommand();
com1.Connection = conn1;
com1.CommandType = CommandType.Text;
com1.CommandText = "INSERT into data(pname, budget, dcommision, advance, phone, cdetails, mail) values(#pname , #budget, #dcommision, #advance, #phone, #cdetails, #mail)";
com1.Parameters.AddWithValue("#pname", textBox8.Text.Trim());
com1.Parameters.AddWithValue("#budget", budget);
com1.Parameters.AddWithValue("#dcommision", textBox7.Text.Trim());
com1.Parameters.AddWithValue("#advance", advance);
com1.Parameters.AddWithValue("#phone", phone);
com1.Parameters.AddWithValue("#cdetails", richTextBox1.Text.Trim());
com1.Parameters.AddWithValue("#mail", textBox3.Text.Trim());
com1.ExecuteNonQuery(); //Executing the SQL query
com1.Dispose(); //Closing SQL Server connection
conn1.Close();
Is something wrong with my query? I am really a newbie so some help would be really appreciated. Thanks
Your table data have also column cname which you don't include in INSERT list, and column i marked as NOT NULL. Include the column too into INSERT list or provide DEFAULT value for this column in DB.