Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update in all table Expand / Collapse
Author
Message
Posted Wednesday, October 24, 2012 6:01 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:12 AM
Points: 1,953, Visits: 2,397
I have written an update statement which is updating some fields like this--

update L
set L.UpdateBy = E.UserID,
L.CreatedByID = E.UserID
from EmployeeProfile E
inner join LocationMaster L on E.EmployeeID=L.UpdateBy

As it will only updating in LocationMaster table.
I want to update all the tables that has columns(UpdateBy,CreatedByID).
Is it possible?



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1376424
Posted Wednesday, October 24, 2012 6:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:08 PM
Points: 12,927, Visits: 32,333
you could build the queries you want to execute for all tables meeting your criteria.

if the table has a value in the UpdateBy column, are you trying to set the CreatedByID tot eh same value?
what is the purpose of joining to the Employee table if the data is already in the target table anyway? if the data is blank in LocationMaster, your update will leave those rows null still.

Anyway, as far as building the query goes,
my example below below builds the same SQL statement you demoed for every table that has both a UpdateBy column and a CreatedByID column.
is that what you want?
it's a little wierd to want to blindly update like that...since the data is already there!



SELECT
'
update L
set L.UpdateBy = E.UserID,
L.CreatedByID = E.UserID
from EmployeeProfile E
inner join ' + OBJECT_NAME(T1.object_id) + ' L on E.EmployeeID=L.UpdateBy
' AS cmd
FROM sys.columns T1
INNER JOIN sys.columns T2
ON T1.object_id = T2.object_id
WHERE T1.name = 'UpdateBy'
AND T2.name = 'CreatedByID'


edit
is this what you are really trying to do?
update LocationMaster
set UpdateBy = CreatedByID
WHERE CreatedByID IS NOT NULL
--AND UpdateBy IS NULL --?? only where it's currently blank/null?



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1376429
Posted Wednesday, October 24, 2012 6:28 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 8:56 AM
Points: 115, Visits: 145
the MERGE statement might help you there but I would create separate update statements and run as a single transaction. Also use nested if statements so that if one update fails everything gets rolled back.

make sense?

Eamon
Post #1376434
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse