Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Generate DDL script to create table and all constraints


Generate DDL script to create table and all constraints

Author
Message
VPSD
VPSD
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 136
Obviously the UDTs will not work with this script for now. I have tried to fix the issue that you highlihted and uploaded the script again. You may have to wait for a day before the script is published once again and visible to you.
Richard Norris
Richard Norris
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 190
Here's my take on it the column type generation, which includes computed columns, UDTs and the numeric & decimal types:


SELECT QUOTENAME(c.name) + ' '
+ CASE WHEN c.is_computed = 0 THEN
CASE t.is_user_defined WHEN 1 THEN QUOTENAME(s.name) + '.' ELSE '' END +
QUOTENAME(t.name) +
CASE
WHEN t.name IN ('binary','varbinary','char','varchar')
THEN '(' + CASE c.max_length WHEN -1 THEN 'max' ELSE CAST(c.max_length AS VARCHAR(10)) END + ')'
WHEN t.name IN ('nchar','nvarchar')
THEN '(' + CASE c.max_length WHEN -1 THEN 'max' ELSE CAST(c.max_length/2 AS VARCHAR(10)) END + ')'
WHEN t.name IN('numeric','decimal')
THEN '(' + CAST(c.[precision] AS VARCHAR(10)) + ', ' + CAST(c.[scale] AS VARCHAR(10))+ ')'
ELSE ''
END + CASE c.is_nullable WHEN 0 THEN ' NOT NULL' ELSE ' NULL' END
ELSE ' AS ' + cc.[definition]
END AS dataType
FROM sys.columns c
INNER JOIN sys.objects o
ON c.[object_id] = o.[object_id]
INNER JOIN sys.types t
ON c.system_type_id = t.system_type_id
AND c.user_type_id = t.user_type_id
LEFT JOIN sys.schemas s
ON t.[schema_id] = s.[schema_id]
LEFT JOIN sys.computed_columns cc
ON c.[object_id] = cc.[object_id]
AND c.column_id = cc.column_id
WHERE o.name = 'Table_1'
ORDER BY c.column_id




VPSD
VPSD
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 136
Thanks, this helps. I will update the script accordingly.
Sailor
Sailor
SSC Veteran
SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)

Group: General Forum Members
Points: 253 Visits: 837
I'm on SQL 2008:

When I exec I get:

Msg 102, Level 15, State 1, Line 35
Incorrect syntax near 'Schema_Name'.
Msg 102, Level 15, State 1, Line 37
Incorrect syntax near 'obje'.



VPSD
VPSD
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 136
I uploaded a new version of the script, should be there in a day.
Sailor
Sailor
SSC Veteran
SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)

Group: General Forum Members
Points: 253 Visits: 837
Seems like it's working better as I'm getting output now.

But this error is coming up:

Msg 4145, Level 15, State 1, Line 35
An expression of non-boolean type specified in a context where a condition is expected, near 'name'.
Msg 102, Level 15, State 1, Line 37
Incorrect syntax near 'o'.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search