Syntax error on DB2 XMLELEMENT - db2

I get this error when trying out this command in the BIRT Classic Models sample database in Data Studio
select xmlelement(name "custno", customers.customernumber) from customers
Syntax error: Encountered "\"custno\"" at line 1, column 24.
I do not know how to correct it.
Thanks.

I'm not familiar with db2, but according to this your statement looks quite alrigth (although I'd place an alias to name this field...)
But this
Syntax error: Encountered "\"custno\"" at line 1, column 24.
seems to be a quite clear hint, that your error is connected to the NAME of the element.
I'm pretty sure, that this statement was created on string level.
Did you try to escape the "-characters with \"?
The SQL reaching the engine might look like
select xmlelement(name \"custno\", customers.customernumber) from customers
or
select xmlelement(name "\"custno"\", customers.customernumber) from customers
... which is wrong of course...
But to be honest: just guessing...

Related

Why do I get a "unterminated quoted string at or near" error using python postgresql, and not in pgadmin, whith the same request?

I have this query :
INSERT INTO lytnobjects.devices (id,idedge,uniqueref,constructeur,ipaddress,macaddress,
hostname,devicetype,isfirewall,isvisible,iscorporate,
ishub,osname,osversion,datecreation,lasttrafic,
hourtrafic,daytrafic,monthtrafic)
VALUES ('e1e455e98b6ed0037a58d0c1f5dc245a',3183,'TODO','TODO','192.168.143.49',
'b0:0c:d1:bb:36:1c','HPBB361C','Other',False,False,False,False,'','',
'2021-10-29T00:58:53.709','2021-01-01T00:00:00','0/0','0/0','0/0')
When I execute the query using python 3.9 and psycopg2_binary (PostgreSQL), I get an error :
unterminated quoted string at or near "'HPBB361C"
conn is the opened connection to the database (AWS RDS PostgreSQL)
sql is a string with the query above
def SQLExec(conn,sql):
try: cur = conn.cursor()
cur.execute(sql)
except (Exception, psycopg2.DatabaseError) as error:
print("***** ERROR:",error)
cur.close()
If I execute the same request from pgAdmin, I get no error !
There is no missing quote as you can see in the query, and no reason to point an error at this place!
So, I have a string (sql) with the query ("INSERT ...")
I call execute from psycopg2, and get an error: unterminated quoted string at or near "'HPBB361C"
I copy/paste the same string into pgAdmin, and the query is executed with no error
The same string (query)
Any idea why I get an error from my python app?
I am looking for an answer since many hours, but find no explanation, and I don't know how to fix the problem (which doesn't exist for me)
Your help is very appreciated
Thank you
I finaly found the answer!
I build the sql query (string) using some variables coming from various sources, like Amazon S3 for instance.
I assumed that the variable was really a string, with nothing "bizarre" in it... But in fact, sometimes, the "string" was ended with a "\x00" char, that is not displayed, so the string looks just normal :-/
When I execute my query (string) with psycopg2, it receives the extra \x00 char, which ends the string at this place! This is why it says there is a missing quote
I put a trace in the code to display the .encode() version of my string, and it revealed the \x00 at the end. So now I "clean" all string variables used in my queries, just with myvariable.replace("\x00","")
And it works now. There is probably a more conventional way to fix this...
I hope it may help somebody sometime! ;-)

Grafana (V7) adding variable in table name

I need to be able to use variables in table names - I basically have the same set of tables used for different types of data, so I would like to just have one dashboard and swapping between all types instead of always having to set up multiple identical dashboards.
My query is something like:
select * from table_$variable_name;
Where my list of possible variable is something like cat, dog, bird
I can seem to make this work, if I only put the variable as shown above I get the following error
Error 1146: Table 'table_$variable_name' doesn't exist
If I enclose it in curly brackets, I get this error instead.
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{bird}' at line 1
(i.e. with the selected variable actually being visible this time)
I'm not sure if the issue is having underscores in the table names, I tried putting underscores around my variables too to check and I had no luck with that.
Another thing I tried was gradually adding on to the table name, so e.g.
select * from table_$variable;
Still returns an error, but I can see the table name starting to form correctly
Error 1146: Table 'table_bird_' doesn't exist
However, as soon as I add another underscore, the variable is not picked up abymore
```Error 1146: Table 'table_$variable_' doesn't exist``
I'm sure it's something silly I am missing in the syntax of the query - anyone has any suggestions?
Using this https://grafana.com/docs/grafana/latest/variables/templates-and-variables/ for reference
As #arturomp suggests, use
${var:raw}
At least in my case, this was the solution that worked.
I found double square brackets work. e.g.
Rather than
select * from table_$variable_name;
use
select * from table_[[variable_name]];

PyCharm PostgreSQL dialect detection

Having a problem with PostgreSQL dialect in PyCharm. I have the below SELECT query:
"SELECT * FROM table WHERE ST_DWithin(table.geog_column,
ST_GeographyFromText(\'SRID=4326;POINT(%s %s)\'), %s)"
The query performs as expected in a query editor but Pycharm complains <expression> expected, got '%'. I have set the dialect detection to PostgreSQL.
I believe there is an issue with the parameter binding but not able to figure out what the issue is. Any help would be appreciated.
EDIT: I somehow missed the clear warnings on psycopg2 documentation about using python string interpolation and concatenation.
The right way of doing it is to use SQLAlchemy to construct raw SQL queries:
from sqlalchemy import text
sql = text("SELECT * FROM table WHERE ST_DWithin(table.geog_column,
ST_GeographyFromText(\'SRID=4326;POINT(:long :lat)\'), :distance)")
data = {'long': longitude, 'lat': latitude, 'distance': distance}
result = conn.execute(sql, data)
The below approach is WRONG and is susceptible to SQL injections. I have left it here for reference only.
I just found the mistake and for anyone else who is caffeine starved, you need to add the %s within single quotes. Elementary but can easily be missed.
"SELECT * FROM table WHERE ST_DWithin(table.geog_column,
ST_GeographyFromText(\'SRID=4326;POINT('%s' '%s')\'), *'%s'*)"
The quotes fixed the issue for me but I am not entirely sure if this is the right approach and hence leaving it here to get some input.

Progress DB charindex syntax error

I'm trying to create a query in progress DB that gets the name portion off the email address. This is what I have so far:
substring("PUB"."NAME"."INTERNET-ADDRESS", 1, CHARINDEX('#', "PUB"."NAME"."INTERNET-ADDRESS") ) as Name
However, I'm getting a syntax error around the CHARINDEX(...
I'm not a progress guy and knowing SQL is just enough to get me into trouble...
Do you see the error in my ways?
Try this:
substring("PUB"."NAME"."INTERNET-ADDRESS", 1, INSTR("PUB"."NAME"."INTERNET-ADDRESS",'#') ).
Here you have the entire suite of documentation:
https://community.progress.com/community_groups/openedge_general/w/openedgegeneral/1329.openedge-product-documentation-overview

Subsonic help, how to list only the first record

Hi I'm doing a little report using subsonic I'm pretty noob And I can't figure how to list only the first record in my report I'm doing something like:
new Select("id,Name,place,group").From(User.Schema)
.InnerJoin(Profile.Schema)
.InnerJoin(userGroup.Schema)
.Where("place")
.IsEqualTo("insomeplace")
.ExecuteReader();
result:
093007 Joe doe insomeplace S2A
093007 Joe doe insomeplace S2A
093007 Joe doe insomeplace S2A
093007 Joe doe insomeplace S2A
I have try to new Select("bla bla").Distinct() new Select("bla bla").Top("1") but none of those appear to work... so what I can do??? any ideas???
When used Top("1") I got a error saying "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1user.id, user.Name, Place, Place
FROM user
' at line 1"
I'm using subsonic 2.x
thanks in advance
I'm assuming from the reference in your question that you are using MySql. I'm afraid that I don't know much about MySql, but the following may be helpful:
I can assure you that .Top("1") certainly works in SubSonic 2.x against SQL Server.
Can you find an equivalent to SQL Server's Profiler for MySql to see what SQL code is being sent to the database?
TOP 1 isn't valid SQL in MySql, instead you need to use the LIMIT clause - http://dev.mysql.com/doc/refman/5.0/en/select.html
quit!!!! I proffered use of
string sqlString = "Select * ...";
new InlineQuery().ExecuteReader(sqlString);
You can use the Paged(...) method:
new Select("id,Name,place,group").From(User.Schema)
.InnerJoin(Profile.Schema)
.InnerJoin(userGroup.Schema)
.Where("place")
.Paged(1, 1)
.IsEqualTo("insomeplace")
.ExecuteReader();
I'm not 100% percent sure if you have to use Paged(1,1) or Paged(0,1), try both.
Another way to do this would be to use .ExecuteSingle(), which would return only the first result. The downside of this is that the database would actually return all rows of the query (SubSonic simply discards anything but the first record).