July 18, 2007 at 2:39 pm
Hi all,
I need to create a table definition script. Unfortunately, object_definition function does not work for tables. Is there any other similar function, or trick, script for this ?
Thanks
July 18, 2007 at 4:05 pm
Not sure if this would fit your needs. Not an elegant solution... But it shouldn't be too much of a stretch to have some string manipulation to turn the following into a simplified create script.
select col.column_NAME + ' ' + data_type
+ CASE WHEN data_type IN ('char', 'varchar') THEN '('+ CAST(character_maximum_length AS VARCHAR(5)) + ')' ELSE '' end
from information_schema.columns col
WHERE col.TABLE_NAME = 'targetTable'
ORDER BY col.ordinal_position
July 18, 2007 at 4:46 pm
Can't you use the scripting methods built-in the GUI!
July 19, 2007 at 12:10 am
You can use VB.NET scripting
July 19, 2007 at 2:30 am
Why can't you use the Generate Script that is readily available to you in SQL Server.
Cheers,
Sugeshkumar Rajendran
SQL Server MVP
http://sugeshkr.blogspot.com
July 19, 2007 at 8:21 am
What are you using the object_definition function for?
Also, what if you do this in two steps: 1st, script the table. 2nd, modify the table to add the object_definition component. would that work in the framework you want and in the way you want to do it? As opposed to the other means suggested to you?
July 19, 2007 at 12:52 pm
Thanks, Daryl, for your code. I modified it and it is what I want. What I needed is a simple code that scripts out all tables from database in standard form like:
create table
(column1_name type(size),
column2_name type(size))
Answering to other replies, yes while searching on internet priorly to posting this question here I also found a lot of references to VB and CMO scripting. But in my case it has to be stored procedure, so anybody who has at least dbo rights would be able to connect and run it if needed. Moreover, SQL solution looks more compact and intuitive. We don't use GUI for this because this procedure may will be scheduled as a job.
Steve, I am using object_definition function to generate object scripts. It works fine for stored procedures, functions, views, however by some reason it does not work for tables.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply