Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL with variable amounts of fields

SQL with variable amounts of fields

Scheduled Pinned Locked Moved Database
helpdatabasegame-devxml
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Expert Coming
    wrote on last edited by
    #1

    I am working on a simple game where you take creatures into battle and they fight against each other. Everything is working fine minus figuring out the best way to store the results. I want something similar to this data structure. int ID (identity) int AttackerID int DefenderID int AttackerCreatureID1 int AttackerCreatureID2 int AttackerCreatureID3 int DefenderCreatureID1 int DefenderCreatureID2 int DefenderCreatureID3 and then more for the creatures that died, ect The problem is, I want the characters to be able to buy more battle slots and bring more into battle at a time. So, say they know can bring 5 creatures in, I don't want to have to make a bunch of new columns. Using XML this exercise would be pretty easy since you can just add a couple extra tags here and there whenever you want. Like:

    12
    <Creature>
    <ID>12</ID>
    </Creature>
    <Creature>
    <ID>12</ID>
    </Creature>

    I've done lots of work with SQL before but never come across doing something like this with variable amounts of data column wise... Any help would be awesome! Thanks ahead of time.

    The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

    R D 2 Replies Last reply
    0
    • E Expert Coming

      I am working on a simple game where you take creatures into battle and they fight against each other. Everything is working fine minus figuring out the best way to store the results. I want something similar to this data structure. int ID (identity) int AttackerID int DefenderID int AttackerCreatureID1 int AttackerCreatureID2 int AttackerCreatureID3 int DefenderCreatureID1 int DefenderCreatureID2 int DefenderCreatureID3 and then more for the creatures that died, ect The problem is, I want the characters to be able to buy more battle slots and bring more into battle at a time. So, say they know can bring 5 creatures in, I don't want to have to make a bunch of new columns. Using XML this exercise would be pretty easy since you can just add a couple extra tags here and there whenever you want. Like:

      12
      <Creature>
      <ID>12</ID>
      </Creature>
      <Creature>
      <ID>12</ID>
      </Creature>

      I've done lots of work with SQL before but never come across doing something like this with variable amounts of data column wise... Any help would be awesome! Thanks ahead of time.

      The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

      R Offline
      R Offline
      Roman_wolf
      wrote on last edited by
      #2

      You could try this: Procedure with following params int ID (identity) int AttackerID int DefenderID NVARCHAR AttackerCreatureIDs (this would be comma separated ids) NVARCHAR DefenderCreatureIDs (this would be comma separated ids) then in your proc, split the Ids string the below function could be used

      CREATE FUNCTION [dbo].[Split]
      (
      @List nvarchar(2000),
      @SplitOn nvarchar(5)
      )
      RETURNS @RtnValue table
      (

      Id int identity(1,1),
      Value nvarchar(100)
      

      )
      AS
      BEGIN
      While (Charindex(@SplitOn,@List)>0)
      Begin
      Insert Into @RtnValue (value)
      Select
      Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
      Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
      End
      Insert Into @RtnValue (Value)
      Select Value = ltrim(rtrim(@List))

      Return
      

      END
      GO

      E 1 Reply Last reply
      0
      • R Roman_wolf

        You could try this: Procedure with following params int ID (identity) int AttackerID int DefenderID NVARCHAR AttackerCreatureIDs (this would be comma separated ids) NVARCHAR DefenderCreatureIDs (this would be comma separated ids) then in your proc, split the Ids string the below function could be used

        CREATE FUNCTION [dbo].[Split]
        (
        @List nvarchar(2000),
        @SplitOn nvarchar(5)
        )
        RETURNS @RtnValue table
        (

        Id int identity(1,1),
        Value nvarchar(100)
        

        )
        AS
        BEGIN
        While (Charindex(@SplitOn,@List)>0)
        Begin
        Insert Into @RtnValue (value)
        Select
        Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
        Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
        End
        Insert Into @RtnValue (Value)
        Select Value = ltrim(rtrim(@List))

        Return
        

        END
        GO

        E Offline
        E Offline
        Expert Coming
        wrote on last edited by
        #3

        I thought about doing that but it feels dirty, doesn't feel very relational databaseish...

        The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

        1 Reply Last reply
        0
        • E Expert Coming

          I am working on a simple game where you take creatures into battle and they fight against each other. Everything is working fine minus figuring out the best way to store the results. I want something similar to this data structure. int ID (identity) int AttackerID int DefenderID int AttackerCreatureID1 int AttackerCreatureID2 int AttackerCreatureID3 int DefenderCreatureID1 int DefenderCreatureID2 int DefenderCreatureID3 and then more for the creatures that died, ect The problem is, I want the characters to be able to buy more battle slots and bring more into battle at a time. So, say they know can bring 5 creatures in, I don't want to have to make a bunch of new columns. Using XML this exercise would be pretty easy since you can just add a couple extra tags here and there whenever you want. Like:

          12
          <Creature>
          <ID>12</ID>
          </Creature>
          <Creature>
          <ID>12</ID>
          </Creature>

          I've done lots of work with SQL before but never come across doing something like this with variable amounts of data column wise... Any help would be awesome! Thanks ahead of time.

          The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          It looks to me as if you've got a many to many relationship there. A battle joins many attackers to many defenders. So you would have a table Battles which has a unique BattleId, plus AttackerId and DefenderId plus whatever other information you want to hold against a battle (date, location, whatever). Then you have a number of options. One option would be to have an Attackers table which holds BattleId and CreatureId for all the attackers, and another table Defenders which holds BattleId and CreatureId for all the defenders. Or another option would be to have just one table BattleCreatures which holds BattleId, ArmyId and CreatureId where ArmyId is either the AttackerId or the DefenderId depending which side the creature is on. Whether you separate out Attackers from Defenders into two separate tables or whether you have them all together in one table is a bit of a judgement call. There are arguments for and against both options.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups