Update statement is not working - sql

Can any one suggest the reason why update statement is not working in SQL-SERVER-2008 for the code mentioned below
UPDATE [dbo].[CTRL_SECURITY]
SET BI_FIN_FLG = ISNULL(CAF.BI_FIN_FLG,'N'),
BI_HR_FLG = ISNULL(CAF.BI_HR_FLG,'N')
FROM dbo.CTRL_APP_FLG AS CAF
LEFT JOIN [dbo].[CTRL_SECURITY] AS CS
ON CAF.TABLE_COLUMN_VALUE = CAST(CS.ACCOUNT_ID AS VARCHAR(180))
WHERE ACCOUNT_ID IN(SELECT TABLE_COLUMN_VALUE FROM dbo.CTRL_APP_FLG WHERE TABLE_NAME='DIM_ACCOUNT')

Change UPDATE [dbo].[CTRL_SECURITY] to UPDATE CS

Related

Is this questionable SQL Update syntax with aliased table correct?

I saw this SQL Update statement in a trigger and am unsure if the update works accurately - based on looking at where the table alias is and the update table syntax.
The syntax doesn't give any error on execution, and updates the record correctly when executing on random samples on my test DB.
However, on a larger PROD DB with more records, is there a possibility that the update fails or skips altogether? There were reports that random records did not have the SAMPLE.ISCOMPOSITESAMPLE field set.
Questionable syntax
UPDATE SAMPLE SET
SAMPLE.SAMPLETYPE = (SELECT DESCRIPTION FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO),
SAMPLE.ISCOMPOSITESAMPLE = (SELECT COMPOSITESAMPLE FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO)
FROM SAMPLE C
INNER JOIN INSERTED T ON C.SAMPLENO = T.SAMPLENO
Syntax I am familiar with (similar to above but intentionally not optimised for comparison)
UPDATE C SET
SAMPLETYPE = (SELECT DESCRIPTION FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO),
ISCOMPOSITESAMPLE = (SELECT COMPOSITESAMPLE FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO)
FROM SAMPLE C
INNER JOIN INSERTED T ON C.SAMPLENO = T.SAMPLENO
The query is correct and will work. However, there are two things that I would fix:
TheUPDATE SAMPLE does update the table whose alias is C. This is documented as correct and something that really irks me, because aliases should be respected. You should use the alias for the update.
The correlated subqueries are not using fully qualified column names.
So, using correlated subqueries, I would recommend:
UPDATE S
SET SAMPLETYPE = (SELECT ST.DESCRIPTION FROM SAMPLETYPE ST WHERE ST.SAMPLETYPENO = S.SAMPLETYPENO),
ISCOMPOSITESAMPLE = (SELECT ST.COMPOSITESAMPLE FROM SAMPLETYPE ST WHERE ST.SAMPLETYPENO = S.SAMPLETYPENO)
FROM SAMPLE S INNER JOIN
INSERTED I
ON S.SAMPLENO = I.SAMPLENO ;
You can also write this -- probably more efficiently -- using a LEFT JOIN:
UPDATE S
SET SAMPLETYPE = ST.DESCRIPTION,
ISCOMPOSITESAMPLE = ST.COMPOSITESAMPLE
FROM SAMPLE S INNER JOIN
INSERTED I
ON S.SAMPLENO = I.SAMPLENO LEFT JOIN
SAMPLETYPE ST
ON ST.SAMPLETYPENO = S.SAMPLETYPENO;

Update query whilst joining two tables?

I am trying to update a table whilst using a join. The task is:
Modify the database to show that Helen Partou has now learned to play the tambourine adequately.
Here is my attempt:
update MusicianInstrument
set instrumentName = 'Tambourine',levelOfExpertise = 'Adequate'
from MusicianInstrument join Musician
on MusicianInstrument.musicianNo = Musician.musicianNo
where musicianName = 'Helen Partou';
However I keep getting an error with the FROM statement.....any help?!
Thanks
You want to use WHERE EXISTS:
UPDATE MusicianInstrument mi
SET mi.instrumentName = 'Tambourine'
, mi.levelOfExpertise = 'Adequate'
WHERE EXISTS ( SELECT 1 FROM Musician m
WHERE m.musicianNo = mi.musicianNo
AND m.musicianName = 'Helen Partou');
On a side note since you're using Oracle I would recommend not using CamelCase or headlessCamelCase for object names.
Your task is to modify database, so probably not only update table but also insert values if they are not already there.
You can do this using merge:
merge into MusicianInstrument i
using (select * from Musician where musicianName = 'Helen Partou') m
on (m.musicianNo = i.musicianNo
and i.instrumentName = 'Tambourine')
when matched then update set levelOfExpertise = 'Adequate'
when not matched then insert (i.musicianNo, i.instrumentName, i.levelOfExpertise)
values (m.musicianNo, 'Tambourine', 'Adequate')

Update database from another using joins?

I am trying to update a table from another database using joins and having a hard time. This is what I am trying to do in pseudo:
UPDATE [Database1].[dbo].[Sessions]
SET [SpeakerID] = ?STATEMENT1?
WHERE ?STATEMENT2?
For "Statement1", this would be coming from another database and table that has columns: SessionID and SpeakerID. How can this be achieved?
UPDATE a
SET a.SpeakerID = b.colName -- SET valoue here
FROM Database1.dbo.Sessions a
INNER JOIN Database2.dbo.Sessions b
ON a.SessionID = b.SessionID -- assumes that their
-- relationship column is SessionID,
-- change it in your original columnName
WHERE ....
a and b are called alias. They are useful when you have longer source name.
UPDATE L
SET SpeakerID = R.SpeakerID
FROM dbo.LocalTable AS L
INNER JOIN RemoteDatabase.dbo.RemoteTable AS R
ON L.SomeValue = R.SomeValue;
This really is no different from this problem except you have to add a database prefix to one of the tables in the join.
try
UPDATE [Database1].[dbo].[Sessions]
SET
Sessions.col1 = other_table.col1
FROM
[Database1].[dbo].[Sessions] Sessions
INNER JOIN
[Database2].[dbo].other_table AS other_table
ON
Sessions.id = other_table.id
WHERE Sessions.id = ??
Note if the database is on another server you will need to create a linked server first
http://msdn.microsoft.com/en-us/library/ff772782.aspx

Update statement involving two different catalogs

Trying to execute an update of a single field where the current db wants to pull values from last night's backup. Should be something close to:
update myTable
set status = (select status
from .lastBackup.dbo.myTable as t
where t.idField = idField)
Help?
Try this:
update t
set status = b.status
from myTable t
inner join lastBackup.dbo.myTable b
on t.idField = b.idField

sql table update problem

table 1(ob): name,address
table 2(address): dname,addr
I need to update ob.address with address.addr when ob.name=address.dname. Can anyone help to get better results because I'm using following command which leads system halt.
UPDATE ob LEFT JOIN address ON ob.name =address.dname SET ob.address=address.addr;
This should do it:
update ob
set address = address_table.addr
where ob.name = address_table.dname
EDIT: Advice: use a better name for Table 2 than address. Maybe TBL_ADDRESS? In my above example I used address_table.
UPDATE ob
SET ob.address = address.addr
WHERE ob.name = address.dname