Create Table DDL via TSQL

  • Comments posted to this topic are about the item Create Table DDL via TSQL

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hi, thanks for the good article.

    But i tried downloading the compressed file, but it did not download.

     

    Regards.

     

  • San_kan1gb;

    I believe this article was reposted from the original~12 years ago, i'll see if i can recover the links. the proc has gone through a slew of changes since then..matching the created case of all objects, and a ton of improvements.

    grab the code snippets form page 8 of this thread: it houses the most updated versions of this code.

    https://www.sqlservercentral.com/forums/topic/get-ddl-for-any-sql-2005-table/page/8

    there are things this code does not do, temporal tables , in memory optimized, partitioned tables, tables with custom data type names over 16 chars, and a few other exotics, like xml indexes.

    let us know if you have any questions in that other thread.

    • This reply was modified 6 years, 8 months ago by Lowell.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I've downloaded those files and added them to the article in a zip file.

  • I have tried to change the line delimiter to '<br>' but the output is not changing.

    Is this possible to achieve?

  • you'll want to change the definition of @VbCrLf from VARCHAR(2) to something larger, and like this, maybe?

    and make sure it is not being re-assigned to CHAR(13) + CHAR(10) later.

    you'll end up needing to cleanup theoutput with html entities code for greater than and less than, as well, but it should work.

    DECLARE @VbCrLf VARCHAR(10) ='<br />' + CHAR(13) + CHAR(10)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I know this proc and this thread are quite old, but I made a couple of fixes to what I hope is the latest version of this proc.

    https://gist.github.com/tcartwright/87828c11c33dfc529859db2ea752b3b3

  • Thank you space ghost!. I've merged your changes with my current changes. my newer version grabs partitioning information, and had a similar fix for foreign keys that happen to reference a different schema, and a few other items, like better aliasing, and a parameter for the whitespace instead of hard coding spaces at 16 or 18. i will also use this to fix the similar proc sp_GetDDLa.

    edit: *.sql file attachments not allowed? added a txt version instead

    Thank you very much for the feedback!

    --V320 Fixes Identified 08/14/2022 by Lowell and dprutan

    -- Fix For temp tables not rendering due to new temporal table logic.

    -- Adding new logic for partition tables and indexes.

    -- rename of a variable or two plus cleanup of alias, spelling, etc

    -- datetime spacing is off for column alignment

    --V321 Fixes identified 2022-11-20 by Ryan H for Foreign Keys needing to join schema_id also to avoid bad script duplication,ie if dbo.ServerInfo and adm.ServerInfo had foreign keys, previous script grabbed extra FKS due to bad join.

    --V322 Tim Cartwright: fixed logic around foreign keys

    -- - made it so that multiple foreign keys of the same name in different schemas would not script out for the table

    -- - when the FK referenced another table in another schema, it was improperly using the wrong schema for the referenced table.

     

     

    • This reply was modified 1 days, 1 hours ago by Lowell.
    Attachments:
    You must be logged in to view attached files.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply