r/SQL • u/makadulla • May 29 '26
SQL Server How can I generate "create script" for ALL databases in a server?
Hey!
I know there are options to generate a create script for one database in SSMS, but if I want to recreate a whole db server as close as possible with all databases, tables etc including permissions and so on, how can I do that? I would appreciate if someone could point me in the right direction as I am no expert in SQL.
Thanks in advance!
13
Upvotes
11
u/VladDBA SQL Server DBA May 29 '26
As with most "bulk operations across one or more instances of SQL Server" questions, the answer is dbatools: https://dbatools.io/Export-DbaScript/
4
u/alinroc SQL Server DBA May 29 '26 edited May 29 '26
What is the problem you're trying to solve here? Clone the server for testing or DR purposes?
Are you copying the data or just the database schemas & permissions?
Like /u/VladDBA said, the answer is usually
dbatools. If you need to copy or move everything to a new instance, look atStart-DbaMigration.Export-DbaScriptworks great for individual objects inside a database, butExport-DbaInstancewill get you everything at the instance level.Edit: If you're just copying database schemas, check this out.
2
u/ifatree May 29 '26
redgate sqlcompare has also always been a good tool for anything i've needed.
1
u/benchwrmr22 May 29 '26
Visual Studio/VS code, SQL Database Projects/SSDT, Schema Compare, SQLPackage
Those are Microsoft tools that can get you started.
1
u/CanProfessional766 Jun 03 '26
If you want to recreate the entire server, use full database backups and restores rather than generating scripts. That will preserve the schema, data, and most database settings. For server level objects like logins, SQL Agent jobs, and linked servers, script those separately (or use a tool like dbatools). Generating scripts for every database is possible, but it's usually more work and less complete than a backup/restore approach.
1
u/pitifulchaity Jun 18 '26
If your goal is to recreate the environment as closely as possible, tools that handle schema comparison and deployment automation can save a lot of manual work. We ended up using dbForge Studio for SQL Server for that kind of task because it was easier to review and synchronize changes across databases than generating massive scripts by hand.
-2
u/AuburnKodiak May 30 '26
From copilot:
✅ SQL Server (SSMS) — Generate Complete CREATE Script
This is the most widely used approach.
Step‑by‑step:
Open SQL Server Management Studio (SSMS)
Connect to your SQL Server instance.
Locate your database
Expand Databases in Object Explorer
Right‑click your target database
Start the script wizard
Click:
Tasks → Generate Scripts
Welcome screen
Click Next
Choose objects
Select:
✅ Script entire database and all database objects
(or choose specific tables, views, etc.)
Click Next
Set scripting options
Choose:
Save to file, or
Save to new query window
Click Advanced…
Set advanced options (IMPORTANT) In the Advanced dialog, configure:
Types of data to script:
Schema only → (CREATE statements only)
Data only → (INSERT statements)
Schema and data → ✅ recommended if needed
Other useful settings:
✅ Script indexes
✅ Script primary keys
✅ Script foreign keys
✅ Script triggers
✅ Script defaults & constraints
Set:
Script for Server Version → your target server version
Click OK
Review summary
Click Next
Generate script
Wait for completion
Click Finish
✅ Result:
You now have a full script that recreates your database objects (and optionally data).
✅ Script Using T-SQL (Alternative Method)
If you prefer a programmatic approach:
For a specific object (example: table)
sp_helptext 'YourTableName'
Or generate via SSMS “Script As”:
Right‑click a table →
Script Table As → CREATE To → New Query Editor
✅ Use SQL Server Tools (Automation)
You can use tools like:
SQL Server Data Tools (SSDT)
DACPAC export:
Right‑click DB → Tasks → Export Data-tier Application
This produces a deployment package that includes schema.
✅ MySQL (if applicable)
Use the mysqldump utility:
mysqldump -u username -p --no-data db_name > schema.sql
--no-data → schema only (CREATE statements)
Remove it to include data
✅ PostgreSQL
Use pg_dump:
pg_dump -U username -s db_name > schema.sql
-s = schema only
⚠️ Common Pitfalls to Avoid
Forgetting to include:
indexes
constraints
triggers
Not matching target server version
Missing dependencies (e.g., users, roles, linked objects)
Not checking for:
filegroups
partitioning
CLR objects (if used)
👍 Best Practice
After generating the script:
Open it in a query window
Run it on a test database first
Fix dependency/order issues if any