IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類(lèi)|軟件專(zhuān)題|手機(jī)版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置:首頁(yè)數(shù)據(jù)庫(kù)MSSQL → 將表數(shù)據(jù)生成SQL腳本的存儲(chǔ)過(guò)程和工具

將表數(shù)據(jù)生成SQL腳本的存儲(chǔ)過(guò)程和工具

時(shí)間:2015/6/28來(lái)源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評(píng)論(0)

將表數(shù)據(jù)生成SQL腳本的存儲(chǔ)過(guò)程示例:

CREATE PROCEDURE dbo.UspOutputData   @tablename sysname

AS

declare @column varchar(1000)

declare @columndata varchar(1000)

declare @sql varchar(4000)

declare @xtype tinyint

declare @name sysname

declare @objectId int

declare @objectname sysname

declare @ident int

set nocount on

set @objectId=object_id(@tablename)

if @objectId is null -- 判斷對(duì)象是否存在

begin

print 'The object not exists'

return

end

set @objectname=rtrim(object_name(@objectId))

if @objectname is null or charindex(@objectname,@tablename)=0 --此判斷不嚴(yán)密

begin

print 'object not in current database'

return

end

if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判斷對(duì)象是否是table

begin

print 'The object is not table'

return

end

select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80

if @ident is not null

print 'SET IDENTITY_INSERT '+@TableName+' ON'

declare syscolumns_cursor cursor

for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid

open syscolumns_cursor

set @column=''

set @columndata=''

fetch next from syscolumns_cursor into @name,@xtype

while @@fetch_status < >-1

begin

if @@fetch_status < >-2

begin

if @xtype not in(189,34,35,99,98) --timestamp不需處理,image,text,ntext,sql_variant 暫時(shí)不處理

begin

set @column=@column+case when len(@column)=0 then'' else ','end+@name

set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','','

end

+case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char

when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar

when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime

when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime

when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier

else @name end

end

end

fetch next from syscolumns_cursor into @name,@xtype

end

close syscolumns_cursor

deallocate syscolumns_cursor

set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename

print '--'+@sql

exec(@sql)

if @ident is not null

print 'SET IDENTITY_INSERT '+@TableName+' OFF'

GO

exec UspOutputData 你的表名

工具介紹:

1 InstallShield自帶一個(gè)把SQL數(shù)據(jù)庫(kù)的結(jié)構(gòu)和數(shù)據(jù)導(dǎo)出為腳本的功能,很好用,如果剛好要做打包,建議用這個(gè),因?yàn)榘惭b文件比較大,如果不是做打包不建議用。

2 SqlInsert是一個(gè)專(zhuān)門(mén)導(dǎo)出數(shù)據(jù)為腳本的小工具,速度沒(méi)IS的快,但是一樣好用。

關(guān)鍵詞標(biāo)簽:SQL

相關(guān)閱讀

文章評(píng)論
發(fā)表評(píng)論

熱門(mén)文章 SqlServer2005對(duì)現(xiàn)有數(shù)據(jù)進(jìn)行分區(qū)具體步驟SqlServer2005對(duì)現(xiàn)有數(shù)據(jù)進(jìn)行分區(qū)具體步驟sql server系統(tǒng)表?yè)p壞的解決方法sql server系統(tǒng)表?yè)p壞的解決方法MS-SQL2005服務(wù)器登錄名、角色、數(shù)據(jù)庫(kù)用戶(hù)MS-SQL2005服務(wù)器登錄名、角色、數(shù)據(jù)庫(kù)用戶(hù)Access、SQL Server、Oracle常見(jiàn)應(yīng)用的區(qū)別Access、SQL Server、Oracle常見(jiàn)應(yīng)用的區(qū)別

相關(guān)下載

人氣排行 如何遠(yuǎn)程備份(還原)SQL2000數(shù)據(jù)庫(kù)SQL2000數(shù)據(jù)庫(kù)遠(yuǎn)程導(dǎo)入(導(dǎo)出)數(shù)據(jù)配置和注冊(cè)O(shè)DBC數(shù)據(jù)源-odbc數(shù)據(jù)源配置教程SQL2000和SQL2005數(shù)據(jù)庫(kù)服務(wù)端口查看或修改SQL Server 2005降級(jí)到2000的正確操作步驟修改Sql Server唯一約束教程淺談JSP JDBC來(lái)連接SQL Server 2005的方法SQL Server創(chuàng)建表語(yǔ)句介紹