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 |
|
The_Utimate_Novice
Starting Member
7 Posts |
Posted - 2011-11-12 : 17:53:26
|
I am trying to use Stored Procedure in MS SQL Server 2008 R2 to update records.Here is a sample code from the book I am reading.CREATE PROCEDURE spUpd_MyContacts @ContactID int , @FirstName nvarchar(50) , @LastName nvarchar(50) , @Phone nvarchar(25)AS UPDATE MyContacts SET FirstName = @FirstName , LastName = @LastName , Phone = @PhonespUpd_MyContacts 5, 'Betrice', 'Rubble', '(222) 234-7654' When I run this code, it updates all the records in the table rather then only updating the specific record with the ID of 5. Is this code suppose to update all records? Or just the record with primary key of 1?My second question is, how do I update a record without using a primary key? Can I specify other attributes in a where clause rather then using a primary key?Thanks everyone!Ultimate Novice |
|
|
SIMPLicity_
Starting Member
1 Post |
Posted - 2011-11-12 : 18:07:33
|
| It really does that way!If you'd like to update only one record, you have to specify it...So, add "WHERE ID = @ContactID" clause in UPDATE statement (after SET.....). |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-11-12 : 18:14:00
|
quote: Is this code suppose to update all records?
Yes.quote: Or just the record with primary key of 1?
Where in the update statement is there anything to say update just the row with ContactID = 5? The value is passed to the procedure, but absolutely nothing is done with the value in the procedure.There is no limitation on that update, it just says update the table and set the column values.--Gail ShawSQL Server MVP |
 |
|
|
The_Utimate_Novice
Starting Member
7 Posts |
Posted - 2011-11-12 : 18:28:58
|
quote: Originally posted by SIMPLicity_ It really does that way!If you'd like to update only one record, you have to specify it...So, add "WHERE ID = @ContactID" clause in UPDATE statement (after SET.....).
Hello, I modified the code by adding the WHERE clause with the ID as follows:[codeCREATE PROCEDURE spUpd_MyContacts @ContactID int , @FirstName nvarchar(50) , @LastName nvarchar(50) , @Phone nvarchar(25)AS UPDATE MyContacts SET WHERE ContactID = @ContactID , FirstName = @FirstName , LastName = @LastName , Phone = @PhonespUpd_MyContacts 5, 'Betrice', 'Rubble', '(222) 234-7654'][/code]However, I get the following error message.Cannot update identity column 'CONTACT_ID'.Thanks for you response! |
 |
|
|
The_Utimate_Novice
Starting Member
7 Posts |
Posted - 2011-11-12 : 18:35:48
|
quote: Originally posted by GilaMonster
quote: Is this code suppose to update all records?
Yes.quote: Or just the record with primary key of 1?
Where in the update statement is there anything to say update just the row with ContactID = 5? The value is passed to the procedure, but absolutely nothing is done with the value in the procedure.There is no limitation on that update, it just says update the table and set the column values.--Gail ShawSQL Server MVP If I knew the answer I wouldn't be asking questions, I wouldn't have posted in the first place.As I mentioned I bought a book and I am new to this and following the code in the books.The text specifically says:The following example is a stored procedure to modify a record in the MyContacts table we created earlier.Executing this procedure is simple. We just pass the ContactID used to identify the record we want to update along with the values for each column as parameters defined in the procedure script:Since I am not sure this code or direction is right, I am simply posting to ask.Thanks for your help! |
 |
|
|
The_Utimate_Novice
Starting Member
7 Posts |
Posted - 2011-11-12 : 20:37:06
|
| I am in a discussion with the author and will post a link or the answers when the matter is resolved.Thanks everyone!TUN |
 |
|
|
vikki.seth
Yak Posting Veteran
66 Posts |
Posted - 2011-11-13 : 05:41:30
|
| The syntax of the update part of the SP is not correctUPDATE MyContacts SET FirstName = @FirstName, LastName = @LastName, Phone = @PhoneWHERE ContactID = @ContactID |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-13 : 12:09:27
|
quote: Originally posted by The_Utimate_Novice I am in a discussion with the author and will post a link or the answers when the matter is resolved.Thanks everyone!TUN
see this to understand how update workshttp://msdn.microsoft.com/en-us/library/ms177523.aspxsee how to apply filterscompare and understand where you had gone wrong !------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
The_Utimate_Novice
Starting Member
7 Posts |
Posted - 2011-11-13 : 14:58:21
|
Hello every,Fist I want to say thanks to the people who have devoted their time to post solutions and answers.What I was trying to do was update a record using a stored procedure and the where clause.The solution was written in a book, however, there was a missing piece as pointed by the author.Here is the entire code if you need to make an update to a record without specifying a primary keybut rather using an attribute as an identifier for the specific record you wish to update.The idea is to update the record without affecting or updating the company name of location but everything else.CREATE PROCEDURE uspUpdateRecord @Company VARCHAR(100), @CompanyLoc VARCHAR(50), @Address VARCHAR(30), @City VARCHAR(25), @State CHAR(2), @Zip CHAR(9), @Phone CHAR(10), @Contact CHAR(30), @Support CHAR(1)ASUPDATE tblSupportCallSET Address = @Address , City = @City , State = @State , Zip = @Zip , Phone = @Phone , Contact = @Contact , Support = @Support WHERE Company = @ Company AND CompanyLoc = @CompanyLoc;So if we have a company in our database or table called Office Max Located in New YorkWe can use the company name and location to identify the company and update all other attributes.uspUpdateRecord 'Office Max', 'New York','99689 Ruggs Street', 'New York', 'NY','6719','7182342345','Victor Moon','N'; In a real database, I would rather update a record with a Primary Key but this is just an example of how to do it without a Primary key.Thanks again everyone!TUN |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-14 : 00:23:49
|
| quite often it would be better to update using primary key combination. If by any chance you've multiple record per combination of values you're passing in where it will end up updating all of the records.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|