dbTalk Databases Forums  

Insert and Delete

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Insert and Delete in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Faye
 
Posts: n/a

Default Insert and Delete - 04-15-2010 , 03:11 PM






This is my data. Salesman is the key. I want to select them and then
change them to another Salesman.

From
Salesman Customer
25 Jeff
25 Kory
25 Jean

To
Salesman Customer
30 Jeff
30 Kory
30 Jean


How do I do that? Thanks.

Faye

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Insert and Delete - 04-15-2010 , 05:00 PM






Not sure what you need, but try this (make sure to try on test data not directly on production):

UPDATE Tables
SET salesman = 30
WHERE salesman = 25
AND customer IN ('Jeff', 'Kory', 'Jean');

If you need to update all customers, then this:

UPDATE Tables
SET salesman = 30
WHERE salesman = 25;

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #3  
Old   
Faye
 
Posts: n/a

Default Re: Insert and Delete - 04-16-2010 , 07:12 AM



salesman is the primary key. It can not be updated.



On Apr 15, 5:00*pm, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
Not sure what you need, but try this (make sure to try on test data not directly on production):

UPDATE Tables
SET salesman = 30
WHERE salesman = 25
* *AND customer IN ('Jeff', 'Kory', 'Jean');

If you need to update all customers, then this:

UPDATE Tables
SET salesman = 30
WHERE salesman = 25;

--
Plamen Ratchevhttp://www.SQLStudio.com

Reply With Quote
  #4  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Insert and Delete - 04-16-2010 , 08:22 AM



Then I do not understand what you are trying to do. First you describe you want to change the salesman, and then that it
cannot be changed. Using DELETE and INSERT will have the same effect as UPDATE, not sure how you really need this changed.

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #5  
Old   
Faye
 
Posts: n/a

Default Re: Insert and Delete - 04-16-2010 , 11:26 AM



I was looking for options like creating a temporary table and insert
the new rows and delete the original rows using script... Wonder what
other options are available.



On Apr 16, 8:22*am, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
Then I do not understand what you are trying to do. First you describe you want to change the salesman, and then that it
cannot be changed. Using DELETE and INSERT will have the same effect as UPDATE, not sure how you really need this changed.

--
Plamen Ratchevhttp://www.SQLStudio.com

Reply With Quote
  #6  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Insert and Delete - 04-16-2010 , 11:29 AM



How is that different than the UPDATE statement I posted? When you delete and insert you in essence simulate an update.

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #7  
Old   
Gert-Jan Strik
 
Posts: n/a

Default Re: Insert and Delete - 04-17-2010 , 04:10 PM



There is no rule that says that you cannot update the Primary Key.

Have you tried Plamen's query? What was the error message?

--
Gert-Jan


Faye wrote:
Quote:
salesman is the primary key. It can not be updated.

On Apr 15, 5:00 pm, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote:
Not sure what you need, but try this (make sure to try on test data not directly on production):

UPDATE Tables
SET salesman = 30
WHERE salesman = 25
AND customer IN ('Jeff', 'Kory', 'Jean');

If you need to update all customers, then this:

UPDATE Tables
SET salesman = 30
WHERE salesman = 25;

--
Plamen Ratchevhttp://www.SQLStudio.com

Reply With Quote
  #8  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Insert and Delete - 04-18-2010 , 04:37 PM



Plamen Ratchev (Plamen (AT) SQLStudio (DOT) com) writes:
Quote:
How is that different than the UPDATE statement I posted? When you delete
and insert you in essence simulate an update.
There could be a trigger that screams blue murder if he touches the PK
in an UPDATE statement. Or if he updates the PK for more than one row at a
time. Or it could be as simple as that there is a referencing table with a
FK constraint that does not have ON UPDATE CASCADE.

In thesse case, Faye is indeed better off with an INSERT and a DELETE. And
in the case of the referencing FKs, he needs to update these, before he
deletes.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #9  
Old   
Faye
 
Posts: n/a

Default Re: Insert and Delete - 04-20-2010 , 04:00 PM



This is exactly my situation.


On Apr 18, 4:37*pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
Plamen Ratchev (Pla... (AT) SQLStudio (DOT) com) writes:
How is that different than the UPDATE statement I posted? When you delete
and insert you in essence simulate an update.

There could be a trigger that screams blue murder if he touches the PK
in an UPDATE statement. Or if he updates the PK for more than one row at a
time. Or it could be as simple as that there is a referencing table with a
FK constraint that does not have ON UPDATE CASCADE.

In thesse case, Faye is indeed better off with an INSERT and a DELETE. And
in the case of the referencing FKs, he needs to update these, before he
deletes.

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.