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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to update/delete rows from different tables?

Author  Topic 

marksquall
Starting Member

17 Posts

Posted - 2012-05-03 : 01:05:51
Hello Sirs/Mams,

A pleasant day to every one.

I have the following tables and a sample dummy data (once again, we will assume that this is already stored):
--------------------------------------------------------

PERSON table:

person_id first_name last_name
1 PETER PARKER

--------------------------------------------------------

STUDENT table:

person_id student_number
1 STUD-001

--------------------------------------------------------

ADDRESS table:

person_id address
1 Daily Bugle, New York City


--------------------------------------------------------

PHONE table:

person_id landline
1 123-45-67


The person_id in PERSON table is a primary key and the person_id in STUDENT,ADDRESS and PHONE are foreign keys respectively.

I want to update/delete this record but instead of using person_id, my query will be student_number. I tried the following SQL but the query complains about something that is not update-able because the modification affects multiple base tables:


ALTER PROCEDURE [dbo].[SP_UPDATE_STUDENT_RECORD]
-- Add the parameters for the stored procedure here
@idstudent AS NVARCHAR(20),
@studfname AS NVARCHAR(50),
@studmname AS NVARCHAR(50),
@studlname AS NVARCHAR(50),
@studaddress1 AS NVARCHAR(50),
@studphone1 AS NVARCHAR(50)

AS
BEGIN
SET NOCOUNT ON;
WITH STUDENT_RECORD AS (
SELECT s.STUDENT_NUMBER AS ID, p.FIRST_NAME, p.MIDDLE_NAME,
p.LAST_NAME, a.ADDRESS, c.LANDLINE

FROM PERSON AS p, STUDENT AS s, ADDRESS AS a, PHONE AS c

WHERE (p.PERSON_ID = s.PERSON_ID) AND (p.PERSON_ID = a.PERSON_ID) AND (p.PERSON_ID = c.PERSON_ID)


) UPDATE [STUDENT_RECORD] SET [FIRST_NAME] =@studfname, [MIDDLE_NAME] = @studmname, [LAST_NAME] = @studlname, [ADDRESS] = @studaddress1, [LANDLINE] =@studphone1
WHERE [ID] = @idstudent

END



I hope someone could help me to revise my query. I know if this query will be change into something that is update-able, this will be the same stuff as deleting it.


Thank you and more power.

Warm regards,

Mark Squall

"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-03 : 15:55:59
[code]
ALTER PROCEDURE [dbo].[SP_UPDATE_STUDENT_RECORD]
-- Add the parameters for the stored procedure here
@idstudent AS NVARCHAR(20),
@studfname AS NVARCHAR(50),
@studmname AS NVARCHAR(50),
@studlname AS NVARCHAR(50),
@studaddress1 AS NVARCHAR(50),
@studphone1 AS NVARCHAR(50)

AS
BEGIN
BEGIN TRAN studupdate
BEGIN TRY
DECLARE @Updated_Person table
(
Person_id int
)

UPDATE t
SET t.first_name= @studfname,
t.middle_name = @studmname,
t.last_name = @studlname
OUTPUT inserted.person_id
INTO @Updated_Person
FROM PERSON t
INNER JOIN STUDENT s
ON s.person_id = t.person_id
WHERE s.student_number = @idstudent


UPDATE t
SET t.address = @studaddress1
FROM ADDRESS t
INNER JOIN @Updated_Person p
ON t.person_id = p.person_id


UPDATE t
SET t.landline = @studphone1
FROM PHONE t
INNER JOIN @Updated_Person p
ON t.person_id = p.person_id


COMMIT TRAN studupdate

END TRY
BEGIN CATCH

IF @@TRANCOUNT > 0
ROLLBACK TRAN studupdate

END CATCH

END
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

marksquall
Starting Member

17 Posts

Posted - 2012-05-05 : 01:04:17
To visakh16:

That's was totally awesome... ......

Thanks a lot, it works.


Warm regards,

Mark Squall

"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-05 : 01:06:24
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -