|
-
Jun 30th, 2010, 06:24 PM
#1
Thread Starter
Frenzied Member
MSSQL update multiple tables
Hi,
I would be more than happy, if someone can provide me a simple example of stored procedure to update 2 tables in one stored procedure.
thanks
-
Jun 30th, 2010, 07:21 PM
#2
Re: MSSQL update multiple tables
You simply place two different update statements (1 for each table ) in the SP
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jun 30th, 2010, 07:49 PM
#3
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
i never used a SP, i used directly the commands... from my sqldatasource.... select * .... , how will it look like in the SP, because there are some addtional code in it.
Code:
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
-- Add the parameters for the stored procedure here
<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO
-
Jun 30th, 2010, 08:39 PM
#4
Re: MSSQL update multiple tables
You could just google it.
Here is the first link i get.
http://webforumz.com/databases/45639...-procedure.htm
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 1st, 2010, 01:14 AM
#5
Re: MSSQL update multiple tables
Code:
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
-- Add the parameters for the stored procedure here
<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
INSERT INTO Table1 (column1,column2) VALUES(value1,Value2)
INSERT INTO Table2 (column1,column2) VALUES(value1,Value2)
END
GO
To insert records in multiple tables you can just place the t-sql there.
-
Jul 1st, 2010, 04:36 PM
#6
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
Hi,
I tried to make one, well it updates the first table bot not the second one.
My code is as follow:
Code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE updatereservation2( @PassengerId bigint, @FirstName varchar(50),@LastName varchar(50),@E_mail varchar(50),@Mobile varchar(50),
@VehicleInfoId bigint, @VehicleType varchar(100),@ServiceDateTime datetime,@PickupLocation text,@DropLocation text, @ExtraInfo text
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE dbo.TblPassangerInformation
SET FirstName = @FirstName, LastName = @LastName, E_mail = @E_mail, Mobile = @Mobile
FROM dbo.TblPassangerInformation
WHERE (dbo.TblPassangerInformation.PassengerId = @PassengerId)
UPDATE dbo.TblVehicleInformation
SET VehicleType = @VehicleType, ServiceDateTime = @ServiceDateTime, PickupLocation = @PickupLocation, DropLocation = @DropLocation, ExtraInfo = @ExtraInfo
FROM dbo.TblVehicleInformation
WHERE (dbo.TblVehicleInformation.VehicleInfoId = @VehicleInfoId)
END
GO
-
Jul 1st, 2010, 07:26 PM
#7
Re: MSSQL update multiple tables
Are you sure you got lines on the table?
You must have a row in order to update it.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 1st, 2010, 07:29 PM
#8
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
yes i do, i can see it in my grid view
-
Jul 1st, 2010, 07:57 PM
#9
Re: MSSQL update multiple tables
Hi.
Better forget gridview and such for a moment.
Just do this on sql server.
Ok so you pass VehicleInfoId=@VehicleInfoId. What is the @VehicleInfoId value?
Don't answer that.Just do a
select * from TblVehicleInformation
Where TblVehicleInformation.VehicleInfoId=
and provide the @VehicleInfoId
Do you get a row?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 1st, 2010, 08:17 PM
#10
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
Hi,
I just test it and yes i got a row. Maybe in the gridview in the select command i joined 3 tables, shud i do the same in the update?
vb Code:
SELECT dbo.TblPassangerInformation.PassengerId, dbo.TblPassangerInformation.FirstName, dbo.TblPassangerInformation.LastName, dbo.TblPassangerInformation.E_mail, dbo.TblReservationInvoice.Approved, dbo.TblVehicleInformation.VehicleType, dbo.TblVehicleInformation.ServiceDateTime, dbo.TblPassangerInformation.ReservationId, dbo.TblVehicleInformation.PickupLocation, dbo.TblVehicleInformation.DropLocation, dbo.TblVehicleInformation.ExtraInfo, dbo.TblPassangerInformation.Mobile, dbo.TblVehicleInformation.VehicleInfoId FROM dbo.TblReservationInvoice INNER JOIN dbo.TblPassangerInformation ON dbo.TblReservationInvoice.ReservationId = dbo.TblPassangerInformation.ReservationId INNER JOIN dbo.TblVehicleInformation ON dbo.TblPassangerInformation.ReservationId = dbo.TblVehicleInformation.ReservationId ORDER BY dbo.TblPassangerInformation.ReservationId DESC
thx
-
Jul 1st, 2010, 08:36 PM
#11
Re: MSSQL update multiple tables
Well in the sql you posted if you put before order by,
Where dbo.TblVehicleInformation.VehicleInfoId="your value" do you get a row?
If so then do so in the update.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 1st, 2010, 09:06 PM
#12
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
sorry i didn't get what you mean, do i have to use order by in the update command too?
-
Jul 1st, 2010, 09:10 PM
#13
Re: MSSQL update multiple tables
No not in the update.
I meant if this
Code:
SELECT dbo.TblPassangerInformation.PassengerId, dbo.TblPassangerInformation.FirstName, dbo.TblPassangerInformation.LastName, dbo.TblPassangerInformation.E_mail,
dbo.TblReservationInvoice.Approved, dbo.TblVehicleInformation.VehicleType, dbo.TblVehicleInformation.ServiceDateTime,
dbo.TblPassangerInformation.ReservationId, dbo.TblVehicleInformation.PickupLocation, dbo.TblVehicleInformation.DropLocation, dbo.TblVehicleInformation.ExtraInfo,
dbo.TblPassangerInformation.Mobile, dbo.TblVehicleInformation.VehicleInfoId
FROM dbo.TblReservationInvoice INNER JOIN
dbo.TblPassangerInformation ON dbo.TblReservationInvoice.ReservationId = dbo.TblPassangerInformation.ReservationId INNER JOIN
dbo.TblVehicleInformation ON dbo.TblPassangerInformation.ReservationId = dbo.TblVehicleInformation.ReservationId
Where dbo.TblVehicleInformation.VehicleInfoId="your value"
ORDER BY dbo.TblPassangerInformation.ReservationId DESC
will give you a row ("your value of course being the value you need, u use for update later).
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 1st, 2010, 09:59 PM
#14
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
that's what i 've been doing, but the grid view is not updating it, will just show back previous value, now it don't even update the first table.
-
Jul 1st, 2010, 10:13 PM
#15
Re: MSSQL update multiple tables
Please do EVERYTHING in sql and then we will see what is the gridview problem.
I have to sleep in a while...
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 2nd, 2010, 12:13 AM
#16
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
ok, i executed the commands in MSSQL
when i run the select command in SQL i get rows....
when i run the update query in sql ... the table get updated
vb Code:
UPDATE dbo.TblVehicleInformation SET VehicleType = 'test' WHERE (VehicleInfoId = 75)
when i execute the SP nothing happens just says
Code:
Command(s) completed successfully.
thanks
-
Jul 2nd, 2010, 07:14 PM
#17
Re: MSSQL update multiple tables
I did not understand.
You get rows and data...And then?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 2nd, 2010, 07:18 PM
#18
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
i used the id i got from the result in the update commands
-
Jul 2nd, 2010, 07:24 PM
#19
Re: MSSQL update multiple tables
Sorry.What id?The pk id?
And for what u used it?
Can you be a little more specific?
If your update works then what is the problem?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 2nd, 2010, 07:26 PM
#20
Thread Starter
Frenzied Member
Re: MSSQL update multiple tables
When i use my SP in my grid view it's not working, the results do not get updated.
-
Jul 2nd, 2010, 07:31 PM
#21
Re: MSSQL update multiple tables
Hmm.
If the sp works on sql then clearly the problem is on how you pass your data on the gridview.
Are you using standard ado or DAL?
Maybe a new post on the on vb.net or here would be better because i think the problem is on how you integrate your sp to the grid.
If you are using datasets try to set the auto increment property to "-1".
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|