Selecting an updating the same record in one query - sql

There are identical questions here, but I don't understand how I would be able to apply the suggested solutions to my problem, so here goes...
I have the following code where I want to change the StudentID from 7 to 8
UPDATE Student
SET StudentID = 8, Forename = "Adam", Surname = "Sai", YearID = 1,
WHERE StudentID = 7
This is in the context of a Tkinter treeview where I want to be able to update any value which is selected.
Tkinter Treeview
I get the following error:
Execution finished with errors.
Result: near "WHERE": syntax error
At line 1:
UPDATE Student
SET StudentID = 7, Forename = "Adam", Surname = "Sai", YearID = 1,
WHERE
I am wondering whether this is because I am trying to update and identify the record both using StudentID.

The syntax error in your query is the comma before the where clause. Eliminate it and try again.

Related

EntityDataSource Where Clause in VB.NET

I have a entitydatasource bound to a gridview on my webpage.
In the code behind, I am able to filter and display what I want to the gridview using the WHERE statements of the entitydatasource. One of my where statements filters by using a large OR statement, but the much more logical IN statement does not work.
For example I have employees 1-9 and I want to select employees 1, 4, and 7. In SQL I can get this two ways:
1.
SELECT * FROM table WHERE EmployeeID = 1 OR EmployeeID = 4 OR EmployeeID = 7
2.
SELECT * FROM table WHERE EmployeeID IN (1,4,7)
Both of these statements work perfectly fine for me in SQL
When using the EF, only option 1 works for selecting those particular employees. In order to create this statement, I have a for loop that creates a whereString that end up looking like this:
whereString = "it.EmployeeID = 1 OR it.EmployeeID = 4 OR it.EmployeeID = 7"
EntityDataSource.Where = whereString
If I set my whereString equal to this
whereString = "it.EmployeeID IN (1,4,7)"
it does not work. Why does this where clause that mimics the format used in the SQL code not work?
The "IN clause" for EntityDataSource.Where requires curly braces, rather than parentheses:
whereString = "it.EmployeeID IN {1,4,7}"

Invalid Column Name in SQL Server Management Studio

When I execute this query in SQL Server Management Studio, this error appears:
'Msg 207, Level 16, State 1, Line 1
Invalid column name 'ACCOUNT_NO'.'
This is the code for the query:
DECLARE #largeaccnumber INT = ACCOUNT_NO
DECLARE #smallaccnumber INT
SET #smallaccnumber = (SELECT LEFT(#largeaccnumber, 6))
SELECT DNADRX.CODE,
DNADDR.NAME,
DNADDR.TYPE,
DNADDR.MAIL_NAME,
ADDRESS_LINE1,
ADDRESS_LINE2,
ADDRESS_LINE3,
TOWN_CITY,
COUNTY_STATE,
COUNTY_STATE_CODE,
COUNTRY,
POST_ZIP,
LAST_STAT_DATE,
ACCOUNT_NO
FROM DNADRX,
DNADDR,
BACCNT
WHERE DNADDR.CODE = DNADRX.ADDRESS_CODE
AND DNADDR.CODE = #smallaccnumber
ORDER BY DNADRX.CODE
I want the query to display the data from the columns of the different tables (the columns are listed in the SELECT bit of the query) from 3 different tables (DNADRX, DNADDR, BACCNT), and the factor linking all 3 tables together is the 6 digit code (ACCOUNT_NO in the BACCNT table, ADDRESS_CODE in the DNADRX table and CODE in the DNADDR table). Originally, ACCOUNT_NO from table BACCNT was 8 digits long, but I reduced it to the first 6 digits using SELECT LEFT and assigned this 6 digit value to the variable #smallaccnumber.
Whenever I try to execute the query, it keeps telling me that 'ACCOUNT_NO' is an invalid code name. I have checked the spelling, refreshed using IntelliSense and tried 'BACCNT.ACCOUNT_NO' instead of just 'ACCOUNT_NO' on the first line of the query but it still won't work (instead it says that the multi-part identifier could not be bound when I try 'BACCNT.ACCOUNT_NO').
I am really new to SQL coding so sorry if the answer to my problem is really simple.
Thank you for your assistance :)
You can try something like this.
This assumes you know the 6 character code. This query will only find results IF there is a record matching in EVERY table. If one table doesn't find a matching record this query will return NOTHING. If you want to find a row even if a recrod is missing from a table, replace the "INNER JOIN" with "LEFT OUTER JOIN"
SELECT Dnadrx.Code,
Dnaddr.Name,
Dnaddr.Type,
Dnaddr.Mail_Name,
Address_Line1,
Address_Line2,
Address_Line3,
Town_City,
County_State,
County_State_Code,
Country,
Post_Zip,
Last_Stat_Date,
Account_No
FROM Dnaddr
INNER JOIN BACCNT ON DNAADDR.CODE = BACCNT.ACCOUNT_NO
INNER JOIN Dnadrx ON Dnaaddr.Code=Dnaadrx.Address_Code
WHERE Dnaddr.Code='YOUR 6 CHARACTER CODE GOES HERE'
ORDER BY Dnadrx.Code;

ORA-00904 Invalid Identifier - Update Statement With Two Tables

I'm working with PeopleSoft Campus Solutions, and we need to update about 22,000 rows of data. This is data between the tables of ACAD_PLAN_VW and ACAD_PROG. Students are listed on both, so their IDs match between the two.
Basically what we are trying to do is say that when the ID, academic career, student career number, effective sequence, and effective date match, AND where the academic plan (their degree, as stored on the ACAD_PLAN_VW) is a specific value, update the ACAD_PROG on the ACAD_PROG table to X value.
I tried to do some very interesting combinations of FROM statements, constantly getting errors. After some researching, I found out that SQLTools doesn't really like FROM statements within UPDATE statements, so I rewrote it to just make the connections manually. I'm assuming I'm doing this right, unless I need to reword it.
The statement I have is:
UPDATE PS_ACAD_PROG SET PS_ACAD_PROG.ACAD_PROG = 'UGDS'
WHERE PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
AND PS_ACAD_PLAN_VW.ACAD_PLAN = 'DSTDS'
Theoretically, I would assume that this would update any student who has those connections. However, the error that I'm currently getting is as follows:
ORA-00904: "PS_ACAD_PLAN_VW"."ACAD_PLAN": invalid identifier
I have, as of yet, been unable to figure out the issue. I do have the correct access to view and update those fields, and the field does indeed exist.
Oracle doesn't know it should use the PS_ACAD_PLAN_VW table. Somehow you should reference it.
For example can you try this way?
UPDATE (
select
PS_ACAD_PROG.ACAD_PROG,
PS_ACAD_PLAN_VW.ACAD_PLAN
from
PS_ACAD_PROG,
PS_ACAD_PLAN_VW
where
PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
)
SET
ACAD_PROG = 'UGDS'
WHERE
ACAD_PLAN = 'DSTDS'
Try to use the table which is not getting identified,by keeping in where clause so that you can use the select statement.That will help identifying the tables.
The following implementation should work:
UPDATE PS_ACAD_PROG
SET ACAD_PROG = 'UGDS'
WHERE ROWID IN(SELECT PROG.ROWID
FROM PS_ACAD_PROG PROG
, PS_ACAD_PLAN_VW PLAN
WHERE PLAN.EMPLID = PROG.EMPLID
AND PLAN.ACAD_CAREER = PROG.ACAD_CAREER
AND PLAN.STDNT_CAR_NBR = PROG.STDNT_CAR_NBR
AND PLAN.EFFSEQ = PROG.EFFSEQ
AND PLAN.EFFDT = PROG.EFFDT
AND PLAN.ACAD_PLAN = 'DSTDS');

SQL displaying wrong records in OR condition

I am struggling to write query for the below scenario, can any one help me please?
I have two column error and priority both are numeric fields. need to fetch based on the condition. If i select the value 3 for example from UI screen, it has to look for record in the table.
if the value match in any of the column i have to return the record i have written like this
Select *
FROM WorkItems
WHERE [System.Title] like '%defect%'
AND [System.WorkItemType] in ('Incident','bug')
AND [ErrorClass] =3
OR [Microsoft.VSTS.Common.Priority] =3
AND [Customer] = 'XYZ'
order by [System.WorkItemType]
Its giving false record and fetching all the records with priority or errorclass = 3, i need the records only workitem type = incident or bug for error class=3 or priority = 3
its returning all the records from the table.
If i put and condition like this [ErrorClass] = 3 and [Microsoft.VSTS.Common.Priority] = 3 its returning only if both the value are 3. I need all the records which match 3 along with above condition
try
Select *
FROM WorkItems
WHERE System.Title like '%defect%'
AND System.WorkItemType in ('Incident', 'bug')
AND (ErrorClass = 3 OR Microsoft.VSTS.Common.Priority = 3)
AND Customer = 'XYZ'
order by System.WorkItemType

updating one table to another sql

ok..i'm sure i'm having a brain fart moment..but for some reason i cant get this update to work. I have 2 tables, i'm pretty much trying to copy info from 8 columns to 8 columns in another table. ..
this is what i have so far
update a
set a.denialcharge_cost = b.denial_cost
, a.epd_cost = b.early_default_payment
, a.expeditecharge_cost = b.expeditecharge
, a.duediligence_cost = b.duediligence
, a.deskreview_cost = b.deskreview
, a.servicing_cost = b.servicingcharge
, a.mers_cost = b.merscharge
, a.qcplans_cost = b.qcplans
from orderheader a
inner join clients b
on a.prnt_id = b.id
i get the error
Msg 512, Level 16, State 1, Procedure UpdateOrderHeader, Line 13
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
i have x number of clients in clients..and y number of orders in orderheader..every order can have 1 client..every client can have multiple orders...prnt_id in orderheader has the id in the clients table....any help would be appreciated... i'm trying to copy denial_cost, early_default_payment, expeditecharge, duediligence, deskreview, servicingcharge, merscharge, qcplans
from clients to orderheader
Based on this answer to a previous question, and all of the other troubleshooting that we did, it appears as though you have a trigger getting in the way somewhere. Try disabling it and running the update.
SOMETHING like that should work (with formatting)
update orderheader
set denialcharge_cost = b.denial_cost,
epd_cost = b.early_default_payment,
expeditecharge_cost = b.expeditecharge,
duediligence_cost = b.duediligence,
deskreview_cost = b.deskreview,
servicing_cost = b.servicingcharge,
mers_cost = b.merscharge,
qcplans_cost = b.qcplans
from clients b
where orderheader.prnt_id = clients.id
from what i understand of the error, you are trying do fetch only one result, somewhere and the subquery returns more than a field.
like
select (select * from bb) as count from cc gives an error
because the subquery returns more than a field from the nested query