# How to Access Entity Tablespace

**URL:** <https://forums.toadworld.com/t/how-to-access-entity-tablespace/1483>\
**Category:** Toad Data Modeler\
**Created:** [February 4, 2008, 11:17am UTC](https://forums.toadworld.com/t/how-to-access-entity-tablespace/1483 "2008-02-04T11:17:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![moakes](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@moakes](https://forums.toadworld.com/u/moakes)\
**Post date:** [February 4, 2008, 11:17am UTC](https://forums.toadworld.com/t/how-to-access-entity-tablespace/1483/1 "2008-02-04T11:17:31Z")

</div>

Hi,  
How can I access Entity, Index and Key tablespace attributes in a script? I can’t seem to find them in the Reference.  
I want to write a script to loop all entities and check the Tablespace is set to the correct values.  
Thanks  
Malcolm

---

<div class="post-metadata">

**Author:** ![MichalBielamowicz](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@MichalBielamowicz](https://forums.toadworld.com/u/MichalBielamowicz)\
**Post date:** [February 5, 2008, 3:00am UTC](https://forums.toadworld.com/t/how-to-access-entity-tablespace/1483/2 "2008-02-05T03:00:24Z")

</div>

As far as I see, TDM’s attributes have some generic names, for example Entities have “Comments” property, even in MSSQL where MS named it “Description”. So in order to change Description you have to change Comments property from a script.  
So maybe the tablespace property has other name, maybe “Schema”?

---

<div class="post-metadata">

**Author:** ![Vaclav\_Frolik](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/vaclav_frolik/32/12_2.png) [@Vaclav\_Frolik](https://forums.toadworld.com/u/Vaclav_Frolik)\
**Post date:** [February 5, 2008, 4:20am UTC](https://forums.toadworld.com/t/how-to-access-entity-tablespace/1483/3 "2008-02-05T04:20:51Z")

</div>

Hi Malcolm,

please see PEREntityOR and PERTablespaceOR classes.

Here you are an example:

* * *

function main(){

// itearate entities  
Log.Information("\*\*\* E N T I T I E S \*\*\* ");  
for (i=0; i\<Model.Entities.Count; i++)  
{  
Ent = Model.Entities.GetObject(i);  
Log.Information("Entity: " +Ent.Name);  
if(Ent.Tablespace != null)  
{  
Log.Information(" --- Entity '" +Ent.Name+ "' has tablespace '"+Ent.Tablespace.Name+"'");  
}  
}

Log.Information("");

// iterate tablespaces  
Log.Information("\*\*\* T A B L E S P A C E S \*\*\* ");  
for (i=0; i\<Model.Tablespaces.Count; i++)  
{  
TS = Model.Tablespaces.GetObject(i);  
Log.Information("Tablespace: "+TS.Name);  
for (j = 0; j\<TS.Entities.Count; j++)  
{  
EntityWithTS = TS.Entities.GetObject(j)  
Log.Information(" --- Tablespace '"+TS.Name+"' is assigned to entity '"+EntityWithTS.Name+"'");  
}

}  
}

* * *

When you iterate entities, you can access tablespace via Ent.Tablespace.Name. You can also iterate tablespaces and access entity name as it is mentioned in the second example.

See the screen shot, please.

Regards,

Vaclav

 ![tablespaces.png](https://global.discourse-cdn.com/quest/original/2X/1/1c77ff90f2cb3afe03a293c361077f6f0f87b65c.png)

---

<div class="post-metadata">

**Author:** ![moakes](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@moakes](https://forums.toadworld.com/u/moakes)\
**Post date:** [February 5, 2008, 6:20am UTC](https://forums.toadworld.com/t/how-to-access-entity-tablespace/1483/4 "2008-02-05T06:20:35Z")

</div>

Thanks Vaclav,

That was helpful. I’ve figured out how to access the Key tablespace.

Another question if I may:  
I’d like to extend my script to assign tablespace to a key;

if ( key.Tablespace.Name is not set )  
set tablespace to required value (eg that of the table)  
else if ( key.Tablespace.Name is the wrong name )  
set tablespace to required value (eg that of the table)

How do I create/replace a tablespace for a Key?

Thanks  
Malcolm

---

<div class="post-metadata">

**Author:** ![Vaclav\_Frolik](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/vaclav_frolik/32/12_2.png) [@Vaclav\_Frolik](https://forums.toadworld.com/u/Vaclav_Frolik)\
**Post date:** [February 6, 2008, 10:45am UTC](https://forums.toadworld.com/t/how-to-access-entity-tablespace/1483/5 "2008-02-06T10:45:40Z")

</div>

Hi Malcolm,

here is the script. Don’t forget to select your model in and rename it to Model in the “Name in script” field. See previous screen shot.

function main(){  
// itearate entities  
Log.Information("\*\*\* E N T I T I E S \*\*\* ");  
for (i=0; i\<Model.Entities.Count; i++)  
{  
Ent = Model.Entities.GetObject(i);  
Log.Information("Entity: " +Ent.Name);  
if(Ent.Tablespace != null)  
{  
// create new EntityTableSpaceName variable and put Entity tablespace name into it  
EntityTableSpaceName = Ent.Tablespace.Name;

```
         Log.Information(" --- Entity '" +Ent.Name+ "' has tablespace '"+Ent.Tablespace.Name+"'");
        
         // iterate keys
         for(j =0; j<Ent.Keys.Count; j++)
               {
                   Key = Ent.Keys.GetObject(j); 
                  
                   if(Key.Tablespace == null)
                       {
                            Key.SetLinkedObject("Tablespace", Ent.Tablespace);  
                            // Note: we cannot use simple 'Key.Tablespace = Ent.Tablespace', because if you
                            // deleted the tablespace, it wouldn't be automatically deleted from the Entity.
                            // That's why it is necessary to use the SetLinkedObject procedure.
                       }
                  
                   else
                       {         
                       // create new KeyTableSpaceName variable and put Entity tablespace name into it
                       KeyTableSpaceName = Key.Tablespace.Name;
                       Log.Information(" ----- Key '" +Key.Name+ "' has tablespace '"+Key.Tablespace.Name+"'");
                      
                       // compare values
                       if(KeyTableSpaceName != EntityTableSpaceName)
                           { 
                           // replace tablespace
                           Key.SetLinkedObject("Tablespace", Ent.Tablespace);
                           Log.Information(" ----- Table space of key '" +Key.Name+ "' has been changed to '"+Key.Tablespace.Name+"'");
                           }
                       }
                  
               }

```

}  
}

}

Regards,

Vaclav
