Now, I think it would help if any intruders infiltrate your fortress to have dwarves hide in their rooms, but there isn't a command like that for orders... I mean, it would make sense, since the military would have less to patrol to keep dwarves safe (depending on how your fortress works). However, still necessary at some point.
I haven't read if someone already recommended it, but still, I can imagine it wouldn't be difficult.
At my school, we have a "Lockdown" drill where we all hide in the nearest classroom if there is some sort of killer on the loose inside the school or something.
For dwarves that don't have rooms, they would hide in the barracks or nearest bedroom.
Anyway, I have a story to go with this.
It all began spring year 3 or 4, of my favorite fortress of "Stronghold".
I finished building an arch for style out of chalk.
Within minutes, a fire consumed everything around my fortress (luckily my chalk walls prevented it from getting in). And two kittens died in the fire. KITTENS DIED!
Well, in the mean time, I had all the dwarves in the command of "dwarves stay indoors"
I forgot to shut my bridge to get into the fortress, and somehow goblins got into my fortress (via the bridge I guessed).
There happened to be an elf caravan at the trading post, and they all were killed in a bloody mess. I brough my guards in as fast as I could. The trading post was on my production layer (i know, i shouldn't have it organized like that, it is not efficient), but there was 1 dwarf killed in that incident (along with all the elves).
Anyway, a dwarf broke out in insanity and killed 3 dwarves (yeah 1 dwarf can kill 3, but goblins could kill only one, proves they are inferior, but they were also on my production layer as well).
Well, two dwarves went on tantrums, and now they destroyed some chairs, tables, and the trade post (still with elf supplies on it, lol).
Aftermath, dwarf bodies everywhere, dwarves thirsty and hungry, and everyone is traumatized.
Now many are going insane because of the chaos, and more dwarves die.
Now, I think more died, actually, since I see dozens of dead bodies on the ground, but I am too sad to see Stronghold destroyed to count their heads...
Anyway, if this command was in place, I think almost no dwarves would be dead, or traumatized.