如何在 SQL 服务器中创建 "Script Generator Options" 脚本?
How to create "Script Generator Options" scripts in SQL Server?
我有一个 SQL 服务器 数据库,每当我想备份数据库时,首先生成数据库的 Drop and Create Script
。因为通过这种方式我可以在SQL Server的每个版本中创建我的数据库。不管SQL.
的版本和维护问题
在生成和发布脚本window中SQL服务器有一个高级脚本选项,如下图:
现在,我想要来自此 window 的脚本来重现脚本生成器 高级选项。换句话说,我想要一个脚本来通过我选择的 Advanced Options.
创建我的数据库的脚本生成器
我该怎么做?
请参考这个How to generate SQL Table Script through query
这是你想要的吗?
declare @table varchar(100)
set @table = 'MyTable' -- set table name here
declare @sql table(s varchar(1000), id int identity)
-- create statement
insert into @sql(s) values ('create table [' + @table + '] (')
-- column list
insert into @sql(s)
select
' ['+column_name+'] ' +
data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') + ' ' +
case when exists (
select id from syscolumns
where object_name(id)=@table
and name=column_name
and columnproperty(id,name,'IsIdentity') = 1
) then
'IDENTITY(' +
cast(ident_seed(@table) as varchar) + ',' +
cast(ident_incr(@table) as varchar) + ')'
else ''
end + ' ' +
( case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' +
coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ','
from information_schema.columns where table_name = @table
order by ordinal_position
-- primary key
declare @pkname varchar(100)
select @pkname = constraint_name from information_schema.table_constraints
where table_name = @table and constraint_type='PRIMARY KEY'
if ( @pkname is not null ) begin
insert into @sql(s) values(' PRIMARY KEY (')
insert into @sql(s)
select ' ['+COLUMN_NAME+'],' from information_schema.key_column_usage
where constraint_name = @pkname
order by ordinal_position
-- remove trailing comma
update @sql set s=left(s,len(s)-1) where id=@@identity
insert into @sql(s) values (' )')
end
else begin
-- remove trailing comma
update @sql set s=left(s,len(s)-1) where id=@@identity
end
-- closing bracket
insert into @sql(s) values( ')' )
-- result!
select s from @sql order by id
您可以使用SQL Server Profiler 检索指定实例中正在执行的sql。
[
我有一个 SQL 服务器 数据库,每当我想备份数据库时,首先生成数据库的 Drop and Create Script
。因为通过这种方式我可以在SQL Server的每个版本中创建我的数据库。不管SQL.
在生成和发布脚本window中SQL服务器有一个高级脚本选项,如下图:
现在,我想要来自此 window 的脚本来重现脚本生成器 高级选项。换句话说,我想要一个脚本来通过我选择的 Advanced Options.
创建我的数据库的脚本生成器我该怎么做?
请参考这个How to generate SQL Table Script through query
这是你想要的吗?
declare @table varchar(100)
set @table = 'MyTable' -- set table name here
declare @sql table(s varchar(1000), id int identity)
-- create statement
insert into @sql(s) values ('create table [' + @table + '] (')
-- column list
insert into @sql(s)
select
' ['+column_name+'] ' +
data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') + ' ' +
case when exists (
select id from syscolumns
where object_name(id)=@table
and name=column_name
and columnproperty(id,name,'IsIdentity') = 1
) then
'IDENTITY(' +
cast(ident_seed(@table) as varchar) + ',' +
cast(ident_incr(@table) as varchar) + ')'
else ''
end + ' ' +
( case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' +
coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ','
from information_schema.columns where table_name = @table
order by ordinal_position
-- primary key
declare @pkname varchar(100)
select @pkname = constraint_name from information_schema.table_constraints
where table_name = @table and constraint_type='PRIMARY KEY'
if ( @pkname is not null ) begin
insert into @sql(s) values(' PRIMARY KEY (')
insert into @sql(s)
select ' ['+COLUMN_NAME+'],' from information_schema.key_column_usage
where constraint_name = @pkname
order by ordinal_position
-- remove trailing comma
update @sql set s=left(s,len(s)-1) where id=@@identity
insert into @sql(s) values (' )')
end
else begin
-- remove trailing comma
update @sql set s=left(s,len(s)-1) where id=@@identity
end
-- closing bracket
insert into @sql(s) values( ')' )
-- result!
select s from @sql order by id
您可以使用SQL Server Profiler 检索指定实例中正在执行的sql。
[