0

Is it possible to view or build create statement for selected table?

For example SSMS has feature Table > Script Table as > Create To > New Query Editor Window that can generate similar statement (without constraints though).

But how to generate create statement with constraints programmatically with T-SQL?

There are two ways how to view a definition that might be useful for code generating

exec sp_help 'Foo'

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'Foo' and TABLE_NAME = 'Bar'

But I don't want to invent a wheel. Maybe there already exist some migration script for copying the tables with constraints to another database, but I did not find it yet.

Any idea? 🤔

5
  • 1
    Have you considered dbatools?
    – vonPryz
    Commented Sep 19, 2022 at 7:55
  • 1
    This question is already answered: stackoverflow.com/questions/706664/… Commented Sep 19, 2022 at 8:13
  • @vonPryz I did not know about dbatools, it looks promising. In general I am able to use Microsoft.SqlServer.Management.Smo namespace in C# but I wanted something more lightweight.
    – Muflix
    Commented Sep 19, 2022 at 8:47
  • @MeyssamToluie this looks great, I will try it, thank you!
    – Muflix
    Commented Sep 19, 2022 at 8:47
  • I found another TSQL script that looks battle tested stormrage.com/2022/03/16/sp_getddla-and-sp_getddl
    – Muflix
    Commented Sep 24, 2022 at 8:11

1 Answer 1

1

I have been using sp_GetDDLa of Lowell Izaguirre and I use it successfully in many automation works.

I suggest you have a look at it and do your own tests.

I am very thankful for these procedures.

--#################################################################################################
-- Real World DBA Toolkit version 5.08 Lowell Izaguirre [email protected]
--#################################################################################################
-- USAGE: exec sp_GetDDLa GMACT
--   or   exec sp_GetDDLa 'bob.example'
--   or   exec sp_GetDDLa '[schemaname].[tablename]'
--   or   exec sp_GetDDLa #temp
--#################################################################################################
-- copyright 2004-2018 by Lowell Izaguirre scripts*at*stormrage.com all rights reserved.
--developer utility function added by Lowell, used in SQL Server Management Studio 
-- http://www.stormrage.com/SQLStuff/sp_GetDDL_Latest.txt
--Purpose: Script Any Table, Temp Table or Object(Procedure Function Synonym View Table Trigger)
--#################################################################################################
-- see the thread here for lots of details: http://www.sqlservercentral.com/Forums/Topic751783-566-7.aspx
-- You can use this however you like...this script is not rocket science, but it took a bit of work to create.
-- the only thing that I ask
-- is that if you adapt my procedure or make it better, to simply send me a copy of it,
-- so I can learn from the things you've enhanced.The feedback you give will be what makes
-- it worthwhile to me, and will be fed back to the SQL community.
-- add this to your toolbox of helpful scripts.
--#################################################################################################
--
1
  • Yes, this is the same script I wrote about in the comment. It would be better if it was maintained on GitHub.
    – Muflix
    Commented Oct 4, 2022 at 13:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.