This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
mysql_insert_id alternative for postgresql
Is there a Postgresql equivalent of mysql_insert_id() to get the ID generated in the last query ??
If you are using PostgreSQL 8.2 or newer then the simplest way is to use RETURNING:
$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");
$insert_row = pg_fetch_row($result);
$insert_id = $insert_row[0];
For other alternatives see this duplicate.
Related
This question already has an answer here:
Replacement for Left command in SQLite SQL
(1 answer)
Closed 7 months ago.
How to remove all characters from / to &.
UPDATE bal SET Perevod=substr(Perevod, INSTR(Perevod, '/.+&')-1)
WHERE INSTR(Perevod, '/.+&')>0;
You can use this because I believe there is no LEFT function in SQLite:
UPDATE bal
SET Perevod = substr(Perevod, 1, INSTR(Perevod, '/')-1)
WHERE INSTR(Perevod, '/')>0;
DEMO
This question already has answers here:
SQL How to Select the most recent date item
(6 answers)
Closed 2 years ago.
I would like to select last inserted data in my table.
I tried :
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=MAX(DateSpread)"
But this doesn't work -> returns : Error Automation..
MAX(DateSpread) should return 04/11/2020
DateSpread is a date format in my AccessDB
And when I hard code:
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=04/11/2020"
It does work, what am I missing?
Please note that I execute this SQL request in Excel and my database is an Access database (.accdb)
Then answer was that I had to do an other select :
"SELECT * FROM HistoSpreadLiq WHERE DateSpread=(SELECT Max(DateSpread) FROM HistoSpreadLiq)"
This question already has an answer here:
MS Access Query with CASE statement
(1 answer)
Closed 3 years ago.
Getting Syntax Error( missing operator) in query expression, What am I getting wrong?
SELECT
ExportUF_NEW.Position,
ExportUF_NEW.[User Defined Field 03]
(CASE
WHEN ExportUF_NEW.[User Defined Field 03] = OP THEN "Production"
WHEN ExportUF_NEW.[User Defined Field 03] = STM THEN "Thermal"
ELSE NULL
END) AS OperationGroup
FROM ExportUF_NEW
WHERE (((ExportUF_NEW.[User Defined Field 03]) Is Not Null))
Expect an outcome to new column "OperationGroup" based on ExportUF_NEW[User Defined Field 03].
MS Access did not support CASE WHEN, use switch instead.
Similar link: What is the equivalent of Select Case in Access SQL?
This question already has an answer here:
Query Data from XML
(1 answer)
Closed 5 years ago.
I have a table in an Oracle 12c Database that contains a XML Type Column and i need to extract a value from this Column.
Here an example of this Column's content:
<AbcMsg xmlns="http://example.org/SCL/CommonTypes">
<Msg>
<Pmnt>
<swif:SWIFT MT="202" xmlns:swif="urn:abc/scl/SWIFT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<swif:Header>
<swif:Input_Output_Identifier>O</swif:Input_Output_Identifier>
<swif:Sender_BIC>XXXXXXXXXXX</swif:Sender_BIC>
<swif:Receiver_BIC>XXXXXXXXXXX</swif:Receiver_BIC>
<swif:Message_Priority>NORM</swif:Message_Priority>
<swif:User_Header>
<swif:Message_User_Reference>XXXXXXXXXXXXXXXXX</swif:Message_User_Reference>
</swif:User_Header>
<swif:Original_Swift>
<swif:Message>DATA TO BE EXTRACTED</swif:Message>
</swif:Original_Swift>
<swif:Create_Date_Time>2016-07-28T15:45:00</swif:Create_Date_Time>
<swif:Msg_ID>POFu7yCXHoN</swif:Msg_ID>
<swif:Sequence_Number>248600</swif:Sequence_Number>
<swif:Session_Number>6184</swif:Session_Number>
</swif:Header>
<swif:Data xsi:type="swif:Data_202">
<swif:F20>
<swif:Sender_s_Reference>POFu7yCXHoN</swif:Sender_s_Reference>
</swif:F20>
<swif:F21>
<swif:Related_Reference>XXXXXXXXXXX</swif:Related_Reference>
</swif:F21>
<swif:F32A>
<swif:Currency>USD</swif:Currency>
<swif:Amount>156020000</swif:Amount>
<swif:Date>2016-07-28</swif:Date>
</swif:F32A>
<swif:Cdrt_F58a_F59a>
<swif:Account>174208531</swif:Account>
<swif:Identifier_Code>XXXXXXXXX</swif:Identifier_Code>
</swif:Cdrt_F58a_F59a>
</swif:Data>
</swif:SWIFT>
</Pmnt>
<Extn/>
</Msg>
</AbcMsg>
I need to retrieve the Value of <swif:Original_Swift>. I've tried the function EXTRACT(xml_column, '/AbcMsg/Msg/Pmnt') but it keeps returning null.
Any clue on how to do that???
Thanks in Advance.
Try xmltable or xmlquery. You have to declare defult and swif namesapce;
select * from xmltable(XMLNAMESPACES('urn:abc/scl/SWIFT' as "swif", default 'http://example.org/SCL/CommonTypes') , '/AbcMsg/Msg/Pmnt/swif:SWIFT/swif:Header/swif:Original_Swift' passing xmltype(your_xml_here));
This question already has answers here:
Syntax error at end of input in PostgreSQL
(4 answers)
Closed 2 years ago.
I am trying to use ? in the following way (I use it in Golang to generate query, but it seems like it is not Go dependent):
WITH Tmp(name, enabled) AS (
VALUES(?, ?),(?, ?)
)
UPDATE table_groups
SET enabled = (SELECT enabled
FROM Tmp
WHERE table_groups.name=Tmp.name)
WHERE table_groups.name IN (SELECT name FROM Tmp)
getting:
syntax error at or near ","
If I substitute ? in the above statement by concrete values, everything works fine. Is there a problem using ? with WITH and how would I get around it? Thanks.
Go doesn't support this at of the box.
You can use jmoiron/sqlx if you want this functional
example using sqlx (from docs):
var levels = []int{4, 6, 7}
query, args, err := sqlx.In("SELECT * FROM users WHERE level IN (?);", levels)
// sqlx.In returns queries with the `?` bindvar, we can rebind it for our backend
query = db.Rebind(query)
rows, err := db.Query(query, args...)