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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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

3 回答

?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

這是我想出來(lái)的劇本。它處理標(biāo)識(shí)列、默認(rèn)值和主鍵。它不處理外鍵、索引、觸發(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


查看完整回答
反對(duì) 回復(fù) 2019-07-09
?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

中隱藏了一個(gè)Powershell腳本。msdb為所有表和相關(guān)對(duì)象編寫(xiě)腳本的論壇:

# 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"


查看完整回答
反對(duì) 回復(fù) 2019-07-09
  • 3 回答
  • 0 關(guān)注
  • 1501 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)