Добро пожаловать в форум, Guest >> Войти | Регистрация | Поиск | Правила | | В избранное | Подписаться | ||
Все форумы / Microsoft SQL Server |
![]() ![]() |
Денис А
Guest |
Добрый день! как программно сгенерировать скрипт создание таблицы |
27 июл 12, 12:14 [12924546] Ответить | Цитировать Сообщить модератору |
alexeyvg Member Откуда: Moscow Сообщений: 31783 |
Конкретнее задавайте вопрос, что у вас не получается? |
||
27 июл 12, 12:34 [12924688] Ответить | Цитировать Сообщить модератору |
trew Member Откуда: Москва Сообщений: 2646 |
Денис А,DECLARE @SQL varchar(max) SET @SQL =' CREATE TABLE [dbo].[tab03]( [id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY, [dd] [int] NULL ) ' EXEC (@SQL) |
27 июл 12, 13:11 [12924973] Ответить | Цитировать Сообщить модератору |
Денис А
Guest |
Необходимо получить ddl создания таблицы |
27 июл 12, 13:39 [12925198] Ответить | Цитировать Сообщить модератору |
Ken@t Member Откуда: 大地 Сообщений: 3264 |
Учу пользоваться поиском, дорого |
||
27 июл 12, 13:43 [12925236] Ответить | Цитировать Сообщить модератору |
Yasha123 Member Откуда: Сообщений: 1955 |
можно взять исходный текст от sp_help, его часть для таблиц. получится примерно такое: declare @objname nvarchar(776) = 'dbo.table_for_script' declare @dbname sysname ,@no varchar(35), @yes varchar(35), @none varchar(35) select @no = 'no', @yes = 'yes', @none = 'none' -- Make sure the @objname is local to the current database. select @dbname = parsename(@objname,3) declare @objid int declare @sysobj_type char(2) select @objid = object_id, @sysobj_type = type from sys.all_objects where object_id = object_id(@objname) -- DISPLAY COLUMN IF TABLE / VIEW if exists (select * from sys.all_columns where object_id = @objid) begin -- SET UP NUMERIC TYPES: THESE WILL HAVE NON-BLANK PREC/SCALE -- There must be a ',' immediately after each type name (including last one), -- because that's what we'll search for in charindex later. declare @precscaletypes nvarchar(150) select @precscaletypes = N'tinyint,smallint,decimal,int,bigint,real,money,float,numeric,smallmoney,date,time,datetime2,datetimeoffset,' -- INFO FOR EACH COLUMN print ' ' select 'Column_name' = name, 'Type' = type_name(user_type_id), 'Computed' = case when ColumnProperty(object_id, name, 'IsComputed') = 0 then @no else @yes end, 'Length' = convert(int, max_length), -- for prec/scale, only show for those types that have valid precision/scale -- Search for type name + ',', because 'datetime' is actually a substring of 'datetime2' and 'datetimeoffset' 'Prec' = case when charindex(type_name(system_type_id) + ',', @precscaletypes) > 0 then convert(char(5),ColumnProperty(object_id, name, 'precision')) else ' ' end, 'Scale' = case when charindex(type_name(system_type_id) + ',', @precscaletypes) > 0 then convert(char(5),OdbcScale(system_type_id,scale)) else ' ' end, 'Nullable' = case when is_nullable = 0 then @no else @yes end, 'TrimTrailingBlanks' = case ColumnProperty(object_id, name, 'UsesAnsiTrim') when 1 then @no when 0 then @yes else '(n/a)' end, 'FixedLenNullInSource' = case when type_name(system_type_id) not in ('varbinary','varchar','binary','char') then '(n/a)' when is_nullable = 0 then @no else @yes end, 'Collation' = collation_name from sys.all_columns where object_id = @objid -- IDENTITY COLUMN? if @sysobj_type in ('S ','U ','V ','TF') and @objid > 0 begin print ' ' declare @colname sysname select @colname = col_name(@objid, column_id) from sys.identity_columns where object_id = @objid select 'Identity' = isnull(@colname,'No identity column defined.'), 'Seed' = ident_seed(@objname), 'Increment' = ident_incr(@objname), 'Not For Replication' = ColumnProperty(@objid, @colname, 'IsIDNotForRepl') -- ROWGUIDCOL? print ' ' select @colname = null select @colname = name from sys.columns where object_id = @objid and is_rowguidcol = 1 select 'RowGuidCol' = isnull(@colname,'No rowguidcol column defined.') end end -- DISPLAY TABLE INDEXES & CONSTRAINTS if @sysobj_type in ('S ','U ') begin print ' ' EXEC sys.sp_objectfilegroup @objid print ' ' EXEC sys.sp_helpindex @objname print ' ' EXEC sys.sp_helpconstraint @objname,'nomsg' if (select count(*) from sysdepends where depid = @objid and deptype = 1) = 0 begin raiserror(15647,-1,-1,@objname) -- No views with schemabinding reference table '%ls'. end else begin select distinct 'Table is referenced by views' = obj.name from sys.objects obj, sysdepends deps where obj.type ='V' and obj.object_id = deps.id and deps.depid = @objid and deps.deptype = 1 group by obj.name end end из полученного добра собрать скрипт создания |
27 июл 12, 13:44 [12925246] Ответить | Цитировать Сообщить модератору |
Все форумы / Microsoft SQL Server | ![]() |