Index Rebuild Operation
Ola Hallengren has already done the work for you here, no need to roll your own code.  You can even cherry pick specific indexes or tables.

https://ola.hallengren.com/

I'd also recommend going ONLINE with those rebuilds if you're on Enterprise Edition, that'll save you locking of the tables for writes.