SQL Server 2005 update query with left join not working - sql

I am trying to do an Update query with a left join in SQL Server 2005 but for some reason it's not working.
[EDIT: "not working" - sorry, this means that the update isn't running. Records aren't updated. I'm running this Update query in a SRSS report, because the CRM I'm using doesn't allow direct access to the database - it runs Selects, Updates, Inserts, Deletes fine but gives you no useful error messages if things don't work as expected. Operating in the dark unfortunately!]
My SQL statement is this:
UPDATE [tblSlots]
SET [tblSlots].[PublishedStartTime] = '10:00'
FROM tblSlots
LEFT JOIN tblDays ON tblSlots.SlotDayID = tblDays.DayID
WHERE tblDays.Published = 1
If I take out the LEFT JOIN line and just filter on e.g. a tblSlots.SlotID, the update works fine.
But I'd like to be able to update the slots on ALL published days at once.
(I tried it as just a JOIN, but that didn't work either...)
I'm sure it's something terribly obvious...

Your query syntax looks fine to me, and as you said it runs fine until you try to do an update. This is a shot in the dark, but if the issue is with updating while selecting from multiple tables, then perhaps simply changing your query to not join to the tblDays might work.
UPDATE [tblSlots]
--SET [tblSlots].[PublishedStartTime] = '10:00'
SET [tblSlots].[PublishedStartTime] = (select SomeValue from tblDays where DayID = SlotDayID) --If a value is needed from Day table
FROM tblSlots
Where SlotDayID in (select DayID from tblDays WHERE Published = 1 and DayID is not null)

you can try:
UPDATE [tblSlots]
SET [tblSlots].[PublishedStartTime] = '10:00'
FROM [tblSlots] ,
(
SELECT DayID FROM
[tblSlots]
LEFT JOIN tblDays
ON tblSlots.SlotDayID = tblDays.DayID
WHERE tblDays.Published = 1
) AS [Data_Days]
WHERE
[Data_Days].DayId = [tblSlots].SlotDayID

Related

Query runs forever ORACLE

Am trying to update the isdeleted column in one table when a record is not in the other user table . My problem is the query l have written runs forever. how best can l write the query below.
update TBLG2O_REGISTER a set a."isDeleted" = '1'
where a."UserID" not in (select k."UserID" from TBLG2O_USER k)
The answer is going to be database engine-specific. Performance characteristic differ wildly, across different database engines, and you failed to specify which DB server you are using.
However, subqueries are frequently MySQL's Achilles heel; I wouldn't be surprised that if this was MySQL. If so, the following approach should have better performance characteristics with MySQL:
update TBLG2O_REGISTER a left join TBLG20_USER k using(UserID)
set a.isDeleted = '1' where k.UserID is null;
Finally got it to work Thank you for your help
Update TBLG2O_REGISTER a set a."isDeleted" = '1' where a."UserID" in (select p."UserID"
from TBLG2O_REGISTER p left join TBLG2O_USER k on p."UserID" =k."UserID"
where k."UserID" is null)

Query for finding what are the reports has not been generated today but were generated yesterday?

I am trying to write a query for
finding what are the reports has not been generated today but were generated yesterday.
I have tried this but this is not working.
select * from records where name not in
(Select * from records where rdate to_date('26-03-2014','DD-MM-YYYY')) and
and rdate=to_date('27-03-2014','DD-MM-YYYY')
SqlFiddle link
I should get the output with the records aa and dd.
Please try:
select
*
from
records
where
rdate=to_date('26-03-2014','DD-MM-YYYY') and
name not in (Select name from records where rdate=to_date('27-03-2014','DD-MM-YYYY'))
A left outer join is preferable to not in in cases like this:
select * from records r1
left join records r2 on
r1.name = r2.name and
r2.rdate = to_date('27-03-2014','DD-MM-YYYY')
where r1.rdate = to_date('26-03-2014','DD-MM-YYYY') and r2.rdate is null
This finds all of the reports that were run yesterday, then joins reports that were run today. Using where r2.rdate is null then excludes the rows that matched to a report today, so you are left with the ones that were run yesterday but not today.
This is much more efficient, because not in executes the subquery repeatedly, while this only executes one query.
SQLFiddle

Oracle Update using a join in the select statement

I am attempting to update a field based upon data in a joined table. I've read that the Update command will not work with a table joins in the where clause. However, I cannot use the Exists command workaround as my condition is not the existence of a linked record, but rather a value in that linked record.
update stock S
set stm_auto_key=186086
From
STOCK Left Join
STOCK_RESERVATIONS On STOCK.STR_AUTO_KEY = STOCK_RESERVATIONS.STR_AUTO_KEY
Where
S.QTY_OH > 0 And
S.STM_LOT = 128729 And
STOCK_RESERVATIONS.IND_AUTO_KEY Is Null
The select statement works fine stand alone. However using it in an update command yields "SQL command not properly ended."
Thanks in advance...
I guess, you need something like this, because there might be no FROM clause in UPDATE statement:
update stock S
set stm_auto_key=186086
Where
S.QTY_OH > 0 And
S.STM_LOT = 128729 And
(SELECT STOCK_RESERVATIONS.IND_AUTO_KEY FROM STOCK_RESERVATIONS
WHERE S.STR_AUTO_KEY = STOCK_RESERVATIONS.STR_AUTO_KEY) Is Null

Updated SQL Field based on Conditions in a Related Table

I am new to SQL and despite hours of searching, cannot figure out the SQL query to update records in my members table, based on conditions in my payments table. I'm very confused whether I use a JOIN (and if so what kind) or a Subquery?
Here's what I have so far:
UPDATE wp_mcra_members
SET wp_mcra_members.dues_paid = 1
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
WHERE wp_mcra_payments.year_paid = '2013' and wp_mcra_payments.reason = 'Dues';
I want the databased to search for any records in the Payments table that meet my conditions of being year 2013 and labeled Dues. Then I want the Members table to update the field dues_paid based on any found records matching those conditions, where the Member ID = Payments Member
The syntax for an update with a join varies by database. Here is generic syntax using a subquery in the where clause:
UPDATE wp_mcra_members
SET dues_paid = 1
where wp_mcra_members.id in (select wp_mcra_payments.member
from wp_mcra_payments
WHERE wp_mcra_payments.year_paid = '2013' and
wp_mcra_payments.reason = 'Dues'
) ;
The syntax you supplied looks like it would work if you are using SQL Server. Oracle does not support JOINs with UPDATE, instead look into using MERGE or Gordon's answer would also work there.
Assuming however you are using MySQL, a JOIN in the UPDATE statement would probably have a better performance than using IN:
UPDATE wp_mcra_members
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
SET wp_mcra_members.dues_paid = 1
WHERE wp_mcra_payments.year_paid = '2013'
and wp_mcra_payments.reason = 'Dues';

long running sp

The following sp:
I have a stored procedure which runs anywhere from 1/2 minute to 4 hours (during nightly processing):
update tableA
set tableA.Other_Flag_50 = isnull(Staging.other_flag_50, 0)
from tableA
inner join (
select acct_nbr,
appl_code,
Other_Flag_50
from tableB
) Staging on tableA.lnhist_acct_nbr = Staging.acct_nbr
and tableA.lnhist_appl_code = Staging.appl_code
I ran Blocking reports in Profiler for 2 nights in a row, first at 10 minutes interval then at 5 minutes. The stored procedure never shows up as being blocked (but it blocks other queries).
Any ideas on optimizing this? Would creating a view with the join help? (acct_nbr, appl_code, Other_Flag_50 from tableB) Thanks!!
Have you tried doing the INNER JOIN directly to tableB?
UPDATE tableA
SET tableA.Other_Flag_50=isnull(tableB.other_flag_50,0)
FROM tableA
INNER JOIN tableB
ON tableA.lnhist_acct_nbr = tableB.acct_nbr
AND tableA.lnhist_appl_code = tableB.appl_code
Try using rowlock to prevent locking a whole table.
update tableA with (rowlock)
...
EDIT
Not sure about other RDBMS but the answer I provided works for SQL Server