Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
nhaas
Yak Posting Veteran
90 Posts |
Posted - 2007-05-01 : 23:13:45
|
I want to create a query to perform in a Procedure.Database1 to Database2 both Tables are the same. I am not sure how to do it.Table is simple for now Userid, FirstName, LastName. I would like if it exists to do an Update.Would the Insert look something like this?INSERT Database2.NEWTABLE (au_id, au_lname, au_fname)SELECT au_id, au_lname, au_fnameFROM Database1.OldtableThank you |
|
nhaas
Yak Posting Veteran
90 Posts |
Posted - 2007-05-01 : 23:59:36
|
OK got this to work, GOOGLE! but can I do an IF Exists, if it does then do a Update? LIKE SOif Exists (INSERT TSPeople (OwnerID, FirstName, LastName) Select OwnerID, FirstName, LastName From IFW.dbo.People)ELSEUPDATE (TSPeople (OwnerID, FirstName, LastName) VALUE Select OwnerID, FirstName, LastName From IFW.dbo.People)) |
|
|
nhaas
Yak Posting Veteran
90 Posts |
Posted - 2007-05-02 : 00:51:39
|
Updated code, still no joy; This does nothing as it stands.IF Exists(Select OwnerID, FirstName, LastName From IFW.dbo.People)UPDATE TSPeople SET FirstName = 'FirstName'ELSEINSERT TSPeople (OwnerID, FirstName, LastName) Select OwnerID, FirstName, LastName From IFW.dbo.PeopleIF I Do ThisIF Exists(Select OwnerID, FirstName, LastName From IFW.dbo.People)BEGINUPDATE TSPeople SET FirstName = 'FirstName'ENDELSEBeginINSERT TSPeople (OwnerID, FirstName, LastName) Select OwnerID, FirstName, LastName From IFW.dbo.PeopleENDIt only updates. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-02 : 00:54:35
|
[code]INSERT Database2.dbo.NEWTABLE (au_id, au_lname, au_fname)SELECT au_id, au_lname, au_fnameFROM Database1.dbo.Oldtable[/code] KH |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-02 : 00:57:26
|
quote: Originally posted by nhaas Updated code, still no joy; This does nothing as it stands.IF Exists(Select OwnerID, FirstName, LastName From IFW.dbo.People)UPDATE TSPeople SET FirstName = 'FirstName'ELSEINSERT TSPeople (OwnerID, FirstName, LastName) Select OwnerID, FirstName, LastName From IFW.dbo.PeopleIF I Do ThisIF Exists(Select OwnerID, FirstName, LastName From IFW.dbo.People)BEGINUPDATE TSPeople SET FirstName = 'FirstName'ENDELSEBeginINSERT TSPeople (OwnerID, FirstName, LastName) Select OwnerID, FirstName, LastName From IFW.dbo.PeopleENDIt only updates.
-- INSERT where as not existsINSERT TSPeople (OwnerID, FirstName, LastName) Select OwnerID, FirstName, LastName From IFW.dbo.People pwhere not exists (select * from TSPeople x where x.OwnerID = p.OwnerID)-- UPDATE where as differenceUPDATE dSET FirstName = s.FirstName, LastName = s.LastNameFROM TSPeople d inner join IFW.dbo.People s ON d.OwnerID = p.OwnerIDWHERE d.FirstName <> s.FirstNameor d.LastName <> s.LastName KH |
|
|
nhaas
Yak Posting Veteran
90 Posts |
Posted - 2007-05-03 : 00:21:09
|
I put the query in as like it shows and I get an error on line 6 on "where"UPDATE where as differencethank you for the helpSQL 2000 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-03 : 07:32:11
|
quote: Originally posted by nhaas I put the query in as like it shows and I get an error on line 6 on "where"UPDATE where as differencethank you for the helpSQL 2000
That suppose to be a comment line on the following codes KH |
|
|
nhaas
Yak Posting Veteran
90 Posts |
Posted - 2007-05-11 : 13:01:19
|
khtan;they work great! thank you! |
|
|
|
|
|
|
|