0

I need to insert data into 3 tables in a single query

Table SkillData

UserIndex SkillPoint TotalSkill
1 5202 99

Table Emblems

UserIndex CountEmblem Status
1 1265 1

Table Rewards

UserIndex Level Code
1 99 1005
2
  • So what code do you have so far? What is wrong with three INSERT statements? Commented Mar 12, 2023 at 2:37
  • save code, since I'm using PHP, and I think doing each query separately is slower the process Commented Mar 12, 2023 at 2:41

1 Answer 1

2

If your reason is to save space in your PHP, you might can use a Stored Procedure for this. Example below:

CREATE PROCEDURE dbo.InsertUserStuff
   @UserIndex INT,
   @SkillPoint INT,
   @TotalSkill INT,
   @CountEmblem INT,
   @Status INT,
   @Level INT,
   @Code INT
AS
BEGIN

   SET XACT_ABORT ON;

   BEGIN TRAN;

   INSERT INTO dbo.SkillData
   (
      UserIndex,
      SkillPoint,
      TotalSkill
   )
   VALUES
   (
      @UserIndex,
      @SkillPoint,
      @TotalSkill
   );

   INSERT INTO dbo.Emblems
   (
      UserIndex,
      CountEmblem,
      Status
   )
   VALUES
   (
      @UserIndex,
      @CountEmblem,
      @Status
   );

   INSERT INTO dbo.Rewards
   (
      UserIndex,
      Level,
      Code
   )
   VALUES
   (
      @UserIndex,
      @Level,
      @Code
   );

   COMMIT TRAN;

END;

You can execute this query with:

EXEC dbo.InsertUserStuff
   @UserIndex = 1,   
   @SkillPoint = 5202,  
   @TotalSkill = 99,  
   @CountEmblem = 1265, 
   @Status = 1,      
   @Level = 99,       
   @Code = 1005         

Obviously you need to change the datatypes to whatever you are using.

1
  • 1
    this works for me, thanks Commented Mar 12, 2023 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.