第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在SQLServer中,如何為給定表生成CREATETABLE語句?

在SQLServer中,如何為給定表生成CREATETABLE語句?

森林海 2019-07-09 10:38:28
在SQLServer中,如何為給定表生成CREATETABLE語句?我花了很多時間想出解決這個問題的辦法,所以本著這個職位,我把它張貼在這里,因為我認為它可能對其他人有用。如果有人有一個更好的腳本,或任何添加,請張貼它。編輯:是的,伙計們,我知道如何在ManagementStudio中這樣做-但我需要能夠在另一個應用程序中做到這一點。
查看完整描述

3 回答

?
動漫人物

TA貢獻1815條經(jīng)驗 獲得超10個贊

這是我想出來的劇本。它處理標識列、默認值和主鍵。它不處理外鍵、索引、觸發(fā)器或任何其他聰明的東西。它在SQLServer 2000、2005和2008上工作。


declare @schema varchar(100), @table varchar(100)

set @schema = 'dbo' -- set schema name here

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 AND table_schema = @schema

 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


查看完整回答
反對 回復 2019-07-09
?
胡子哥哥

TA貢獻1825條經(jīng)驗 獲得超6個贊

中隱藏了一個Powershell腳本。msdb為所有表和相關對象編寫腳本的論壇:

# Script all tables in a database

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") 

    | out-null


$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') '<Servername>'

$db = $s.Databases['<Database>']


$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($s)

$scrp.Options.AppendToFile = $True

$scrp.Options.ClusteredIndexes = $True

$scrp.Options.DriAll = $True

$scrp.Options.ScriptDrops = $False

$scrp.Options.IncludeHeaders = $False

$scrp.Options.ToFileOnly = $True

$scrp.Options.Indexes = $True

$scrp.Options.WithDependencies = $True

$scrp.Options.FileName = 'C:\Temp\<Database>.SQL'


foreach($item in $db.Tables) { $tablearray+=@($item) }

$scrp.Script($tablearray)


Write-Host "Scripting complete"


查看完整回答
反對 回復 2019-07-09
  • 3 回答
  • 0 關注
  • 1494 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號