Results 1 to 21 of 21

Thread: MSSQL update multiple tables

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  3. #3

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: MSSQL update multiple tables

    You could just google it.
    Here is the first link i get.
    http://webforumz.com/databases/45639...-procedure.htm
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: MSSQL update multiple tables

    Are you sure you got lines on the table?
    You must have a row in order to update it.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  8. #8

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: MSSQL update multiple tables

    yes i do, i can see it in my grid view

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  10. #10

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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:
    1. SELECT     dbo.TblPassangerInformation.PassengerId, dbo.TblPassangerInformation.FirstName, dbo.TblPassangerInformation.LastName, dbo.TblPassangerInformation.E_mail,
    2.                       dbo.TblReservationInvoice.Approved, dbo.TblVehicleInformation.VehicleType, dbo.TblVehicleInformation.ServiceDateTime,
    3.                       dbo.TblPassangerInformation.ReservationId, dbo.TblVehicleInformation.PickupLocation, dbo.TblVehicleInformation.DropLocation, dbo.TblVehicleInformation.ExtraInfo,
    4.                       dbo.TblPassangerInformation.Mobile, dbo.TblVehicleInformation.VehicleInfoId
    5. FROM         dbo.TblReservationInvoice INNER JOIN
    6.                       dbo.TblPassangerInformation ON dbo.TblReservationInvoice.ReservationId = dbo.TblPassangerInformation.ReservationId INNER JOIN
    7.                       dbo.TblVehicleInformation ON dbo.TblPassangerInformation.ReservationId = dbo.TblVehicleInformation.ReservationId
    8. ORDER BY dbo.TblPassangerInformation.ReservationId DESC

    thx

  11. #11
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  12. #12

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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?

  13. #13
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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).
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  14. #14

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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.

  15. #15
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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...
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  16. #16

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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:
    1. UPDATE    dbo.TblVehicleInformation
    2. SET              VehicleType = 'test'
    3. WHERE     (VehicleInfoId = 75)
    when i execute the SP nothing happens just says
    Code:
    Command(s) completed successfully.
    thanks

  17. #17
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: MSSQL update multiple tables

    I did not understand.
    You get rows and data...And then?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  18. #18

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: MSSQL update multiple tables

    i used the id i got from the result in the update commands

  19. #19
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  20. #20

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: MSSQL update multiple tables

    When i use my SP in my grid view it's not working, the results do not get updated.

  21. #21
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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
  •  



Click Here to Expand Forum to Full Width