How to execute MySQL 'CREATE VIEW' in Zend Framework 1? - zend-db

I tried following, is it the way to do?
$sql_group = $db->query("CREATE VIEW `branchfromgroup` AS (SELECT `tbl_group`.`Sno` FROM `tbl_group` LEFT JOIN `tbl_branch` ON `tbl_group`.`branchId` = `tbl_branch`.`Sno` WHERE (`tbl_branch`.`Sno` = '4'))");

Related

SQL Update statement: OperationalError near "FROM"

Replacing box = with tb.box = shifts the error to the '.':
query = (f'UPDATE tb '
f"SET box = '{box_update}' "
f'FROM {table} tb '
'INNER JOIN question qu ON qu.id = tb.question_id '
f'WHERE qu.number_a = {num_a} AND qu.number_b = {num_b};')
Error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "FROM": syntax error [SQL: "UPDATE tb SET box = '0' FROM addition tb INNER JOIN question qu ON qu.id = tb.question_id WHERE qu.number_a = 1 AND qu.number_b = 9;"]
Actual implementation may not be fully adhered even with latest SQLite to support UPDATE-FROM. Specifically, docs do not indicate:
JOIN in outer query is supported in UPDATE.
FROM table should not repeat table in UPDATE.
Table alias alone in UPDATE may not be allowed. Possibly no alias for updated table should be used.
Consider below adjustment aligned to example in docs. Below demonstrates parameterization with sqlite3 raw cursor. Adjust to however you run with sqlalchemy.
q = f'''UPDATE {table}
SET box = ?
FROM question qu
WHERE qu.id = {table}.question_id
AND qu.number_a = ?
AND qu.number_b = ?;
'''
cursor.execute(q, (box_update, num_a, num_b))
conn.commit()
It looks like the update syntax you are using either is not supported at all, or at least is not supported on your version of SQLite. However, you may rewrite your update to use exists logic:
query = (f'UPDATE {table} tb '
f"SET box = '{box_update}' "
f'WHERE EXISTS (SELECT 1 FROM question qu '
f' WHERE qu.id = tb.question_id AND '
f' qu.number_a = {num_a} AND qu.number_b = {num_b});')
The raw SQL query would look something like this:
UPDATE yourTable tb
SET box = ?
WHERE EXISTS (SELECT 1 FROM question qu
WHERE qu.id = tb.question_id AND
qu.number_a = ? AND qu.number_b = ?);

How to sanitize join params inside a controller in Rails 4?

Is this, the best way to sanitize 'join' params inside a Controller in Rails 4?
assume:
user_name = params[:user_name]
.
# That's the only way that I can figure this out:
#result = Agenda.joins("LEFT JOIN meetings AS me ON meetings.agenda_id = agendas.id WHERE me.name = #{Agenda.sanitize(user_name})"
I have tried this but don't works because 'joins' expect tables after each ',':
#result = Agenda.joins("LEFT JOIN meetings AS me ON meetings.agenda_id = agendas.id WHERE me.name = ?", user_name)
Note:
This is just a bit of the code to explain the problem, in the full code I really have to use the LEFT JOIN.
I found a better solution using
Model.send(:sanitize_sql_array, < query >)
eg:
user_name = params[:user_name]
join = "LEFT JOIN meetings AS me
ON meetings.agenda_id = agendas.id
WHERE me.name = ?", user_name)"
join = Agenda.send(:sanitize_sql_array,join)
#result = Agenda.joins(join)
In this format, you can use as many parameters as you need with any type of query.

How can i user join on this SQL?

okay i have got this sql statement which someone helped me on here to make this statement the only problem im facing that i want catDesc from te_category table to be appear on my 'Category' table on main page but instead of that i'm getting catID from te_events
This is the code i got from stackoverflow.
$sql = "SELECT * FROM te_events
JOIN te_venue
ON te_venue.venueID = te_events.venueID
WHERE te_events.eventID = $eventID";
this my te_event table screenshot http://prnt.sc/d8e7i1
this is my te_category screenshot http://prntscr.com/d8e87e
I have tried anything but couldn't get what i want please HELP !
use this query:
$sql = "SELECT * FROM te_events
JOIN te_venue
ON te_venue.venueID = te_events.venueID
JOIN te_category ON te_category.catID = te_events.catID
WHERE te_events.eventID = $eventID";

Updating a Microsoft Access 2013 table with data from a pass-through query

I am trying, unsuccessfully so far, to update records in a Microsoft Access 2013 table (called tbl_Data) with data from an AS400 table (LIBRARY.TABLE).
As you can see in my Access 2013 pass-through query below, I am trying to join the access table with the AS400 table using the Prefix & Number fields, and from there, update the access table with Name & Address information from the AS400 table.
Here is my latest attempt:
UPDATE
tbl_Data
SET
tbl_Data.FirstName = a.NINMFR,
tbl_Data.MiddleName = a.NINMMD,
tbl_Data.LastName = a.NINAML,
tbl_Data.BuildingNumber = a.NIBLNR,
tbl_Data.StreetName = a.NISTNM,
tbl_Data.AptSuite = a."NIAPT#",
tbl_Data.Address2 = a.NIADR2,
tbl_Data.City = a.NICITY,
tbl_Data.State = a.NISTAT,
tbl_Data.ZipCode = a.NIZIPC
INNER JOIN
LIBRARY.TABLE a
ON
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR;
When I run this query, I get an error that says:
OBDC--call failed.
[IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0199 - Keyword INNER not expected. Valid tokens: USE SKIP WAIT WITH WHERE. (#-199)
I would really appreciate any assistance, as I'm out of ideas.
Thanks!
That is Microsoft specific syntax for an update, it does not work on DB2. Try this:
UPDATE
tbl_Data
SET
(tbl_Data.FirstName,
tbl_Data.MiddleName,
tbl_Data.LastName,
tbl_Data.BuildingNumber,
tbl_Data.StreetName,
tbl_Data.AptSuite,
tbl_Data.Address2,
tbl_Data.City,
tbl_Data.State,
tbl_Data.ZipCode)
=
(SELECT
a.NINMFR,
a.NINMMD,
a.NINAML,
a.NIBLNR,
a.NISTNM,
a."NIAPT#",
a.NIADR2,
a.NICITY,
a.NISTAT,
a.NIZIPC
FROM
library.table a
WHERE
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR)
WHERE
EXISTS (
SELECT *
FROM
library.table a
WHERE
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR);

SQL update syntax with subquery

I have this update statement:
UPDATE quotedetailextensionbase qd
SET qd.new_capvehicleid = (SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype)
I get an error back but I'm not sure why
Incorrect syntax near 'qd'
Seems like it would be easier to achieve this result with an update-join statement:
UPDATE qd
SET new_capvehicleid = cv.new_capvehicleid
FROM quotedetailextensionbase qd
JOIN (SELECT new_capvehicleid
FROM vwCapidLookup) cv ON cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype
SQL Server lets you do joined updates:
UPDATE
qd
SET
new_capvehicleid = cv.new_capvehicleid
FROM
quotedetailextensionbase qd
join vwCapidLookup cv on
cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype
;
Does that make sense?
Please try the below SQL:
UPDATE quotedetailextensionbase
SET new_capvehicleid =
(SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype)
Use MERGE: https://msdn.microsoft.com/en-us/library/bb510625.aspx. That is correct when you would update from another table.