日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

如何為SQL Server表數據生成insert腳本

瀏覽:134日期:2023-11-04 12:34:47
使用SQL Server數據庫自帶的“生成SQL腳本”工具,可以生成創建表、視圖、存儲過程等的SQL腳本。那么,能否將表中的數據也生成為SQL腳本,在查詢分析器中執行這些腳本后自動將數據導入到SQL Server中呢?答案是肯定的,示例如下:

CREATE PROCEDURE dbo.OutputData

@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 -- 判斷對象是否存在

begin

print @tablename + '對象不存在'

return

end

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

if @objectname is null or charindex(@objectname,@tablename)=0

begin

print @tablename + '對象不在當前數據庫中'

return

end

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

begin

print @tablename + '對象不是表'

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'

--定義游標,循環取數據并生成Insert語句

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 暫時不處理

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'

調用時 exec OutputData 'myuser' 其中myUser中當前數據庫中存在的表。

另外方丈的:

drop proc proc_insert

go

create proc proc_insert (@tablename varchar(256))

as

begin

set nocount on

declare @sqlstr varchar(4000)

declare @sqlstr1 varchar(4000)

declare @sqlstr2 varchar(4000)

select @sqlstr='select ''insert '+@tablename

select @sqlstr1=''

select @sqlstr2=' ('

select @sqlstr1= ' values ( ''+'

select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case

-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'

when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'

when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'

when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'

-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

else '''NULL'''

end as col,a.colid,a.name

from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36

)t order by colid

select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename

-- print @sqlstr

exec( @sqlstr)

set nocount off

end

go

drop proc proc_insert

go

create proc proc_insert (@tablename varchar(256))

as

begin

set nocount on

declare @sqlstr varchar(4000)

declare @sqlstr1 varchar(4000)

declare @sqlstr2 varchar(4000)

select @sqlstr=select insert +@tablename

select @sqlstr1=

select @sqlstr2= (

select @sqlstr1= values ( +

select @sqlstr1=@sqlstr1+col++,+ ,@sqlstr2=@sqlstr2+name +, from (select case

-- when a.xtype =173 then case when +a.name+ is null then null else +convert(varchar(+convert(varchar(4),a.length*2+2)+),+a.name +)+ end

when a.xtype =104 then case when +a.name+ is null then null else +convert(varchar(1),+a.name +)+ end

when a.xtype =175 then case when +a.name+ is null then null else +++replace(+a.name+,,) + ++ end

when a.xtype =61 then case when +a.name+ is null then null else +++convert(varchar(23),+a.name +,121)+ ++ end

when a.xtype =106 then case when +a.name+ is null then null else +convert(varchar(+convert(varchar(4),a.xprec+2)+),+a.name +)+ end

when a.xtype =62 then case when +a.name+ is null then null else +convert(varchar(23),+a.name +,2)+ end

when a.xtype =56 then case when +a.name+ is null then null else +convert(varchar(11),+a.name +)+ end

when a.xtype =60 then case when +a.name+ is null then null else +convert(varchar(22),+a.name +)+ end

when a.xtype =239 then case when +a.name+ is null then null else +++replace(+a.name+,,) + ++ end

when a.xtype =108 then case when +a.name+ is null then null else +convert(varchar(+convert(varchar(4),a.xprec+2)+),+a.name +)+ end

when a.xtype =231 then case when +a.name+ is null then null else +++replace(+a.name+,,) + ++ end

when a.xtype =59 then case when +a.name+ is null then null else +convert(varchar(23),+a.name +,2)+ end

when a.xtype =58 then case when +a.name+ is null then null else +++convert(varchar(23),+a.name +,121)+ ++ end

when a.xtype =52 then case when +a.name+ is null then null else +convert(varchar(12),+a.name +)+ end

when a.xtype =122 then case when +a.name+ is null then null else +convert(varchar(22),+a.name +)+ end

when a.xtype =48 then case when +a.name+ is null then null else +convert(varchar(6),+a.name +)+ end

-- when a.xtype =165 then case when +a.name+ is null then null else +convert(varchar(+convert(varchar(4),a.length*2+2)+),+a.name +)+ end

when a.xtype =167 then case when +a.name+ is null then null else +++replace(+a.name+,,) + ++ end

else null

end as col,a.colid,a.name

from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36

)t order by colid

select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+) +left(@sqlstr1,len(@sqlstr1)-3)+) from +@tablename

-- print @sqlstr

exec( @sqlstr)

set nocount off

end

go

---------------------------------------------------------------------------------------

小干部的:

create proc spgeninsertsql

@tablename as varchar(100)

as

--declare @tablename varchar(100)

--set @tablename = orders

--set @tablename = eeducation

declare xcursor cursor for

select name,xusertype

from syscolumns

where (id = object_id(@tablename))

declare @f1 varchar(100)

declare @f2 integer

declare @sql varchar(8000)

set @sql =select insert into + @tablename + values(

open xcursor

fetch xcursor into @f1,@f2

while @@fetch_status = 0

begin

set @sql =@sql +

+ case when @f2 in (35,58,99,167,175,231,239,61) then + case when + @f1 + is null then else end + else + end

+ replace(isnull(cast( + @f1 + as varchar),null),,)

+ case when @f2 in (35,58,99,167,175,231,239,61) then + case when + @f1 + is null then else end + else + end

+ char(13) + ,

fetch next from xcursor into @f1,@f2

end

close xcursor

deallocate xcursor

set @sql = left(@sql,len(@sql) - 5) + + ) from + @tablename

print @sql

exec (@sql)

標簽: Sql Server 數據庫
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日本免费新一区视频| 一区二区亚洲精品| 天堂精品久久久久| 日本午夜精品| 国产日韩欧美中文在线| 国产乱人伦精品一区| 欧美日韩伊人| 精品国产91| 日韩大片在线观看| 精品国产亚洲一区二区三区大结局 | 精品日韩视频| 理论片午夜视频在线观看| 中文字幕成在线观看| 欧美日韩精品免费观看视完整| 日韩在线不卡| 在线综合亚洲| 日本va欧美va精品发布| 国产一区二区视频在线看| 久久久亚洲一区| 久久国产精品99国产| 久久精品99国产精品| 国产+成+人+亚洲欧洲在线| 欧美日一区二区| 日韩精选在线| 日韩不卡免费高清视频| 亚洲一区二区三区在线免费| 欧美日韩xxxx| 美女亚洲一区| 麻豆久久久久久| 午夜亚洲一区| 欧美日韩精品免费观看视欧美高清免费大片| 久久久一二三| 久久爱www.| 亚洲欧美在线综合| 久久视频一区| 国产精品极品| 欧美日韩一二三四| 国产一级久久| 欧美日本三区| 91精品在线观看国产| 综合激情网站| 国产精品原创| 国产欧美一区二区三区精品观看| 国产91久久精品一区二区| 国产精品多人| 人人精品久久| 亚洲精品美女| 视频一区二区欧美| 香蕉久久99| 成人精品久久| 精品中文字幕一区二区三区四区| 国产精品毛片| 亚洲一级特黄| 激情综合网址| 黄色不卡一区| 亚洲午夜91| 日韩精品不卡一区二区| 美女毛片一区二区三区四区最新中文字幕亚洲| 在线亚洲精品| 国产精品毛片在线| 国产毛片一区| 午夜一级在线看亚洲| 亚洲欧美日韩一区在线观看| 黑丝一区二区| 视频在线观看91| 视频一区免费在线观看| 欧美在线亚洲综合一区| 亚洲少妇诱惑| 美日韩精品视频| 水蜜桃久久夜色精品一区的特点| re久久精品视频| 999国产精品视频| 91久久久精品国产| 六月丁香综合| 日韩黄色免费网站| 欧美激情日韩| 鲁鲁在线中文| 欧美日韩精品一区二区视频| 欧美影院三区| 婷婷激情综合| 亚洲一二三区视频| 欧美亚洲国产日韩| 亚洲午夜天堂| 在线成人直播| 日韩精彩视频在线观看| 国产精品啊啊啊| 粉嫩av一区二区三区四区五区| 国产成人77亚洲精品www| 成人日韩在线| 日韩精品一二三区| 老鸭窝一区二区久久精品| 热三久草你在线| 免费观看在线综合色| 欧美日韩1区2区3区| 中文一区一区三区高中清不卡免费| 久久激情网站| 国产精品一卡| 亚洲国产一区二区三区在线播放| 在线日韩成人| 久久精品一区二区不卡| 国产精品宾馆| 久久一区二区中文字幕| 日本特黄久久久高潮| 999久久久亚洲| 国产精品99免费看| 欧美日韩 国产精品| 国产一区日韩欧美| 国产免费久久| 中文字幕免费一区二区| 国产91欧美| 欧美在线黄色| 一区二区三区四区精品视频| 色欧美自拍视频| 日韩精品免费一区二区夜夜嗨| 成人看片网站| 国产精品嫩模av在线| 国产高清久久| 中文在线а√在线8| 日韩精品亚洲专区在线观看| 欧美午夜不卡| 亚洲久久视频| 91国语精品自产拍| 日韩a一区二区| 国产日韩三级| 蜜桃视频在线观看一区二区| 欧美aa在线观看| 国产欧美欧美| 日本强好片久久久久久aaa| 亚洲欧美日韩精品一区二区| 久久九九99| 欧美a级片一区| 伊人久久av| 亚洲www啪成人一区二区| 日韩欧美精品一区| 成人在线黄色| 国产99精品| 亚洲伊人影院| 日本а中文在线天堂| 久久亚洲国产精品一区二区| 久久久久久自在自线| 欧美在线亚洲综合一区| 久久不见久久见免费视频7| 国产综合婷婷| 美女黄网久久| 国产精品亚洲二区| 一区二区三区四区在线看| 欧美亚洲激情| 91精品丝袜国产高跟在线| 欧美激情另类| 亚洲ww精品| 成人影视亚洲图片在线| 在线精品观看| 日韩在线短视频| 国产欧美亚洲精品a| 亚洲午夜黄色| 久久精品国产999大香线蕉| 日韩中文字幕一区二区三区| 日韩国产一区| 成人在线黄色| 欧美日韩夜夜| 爽好久久久欧美精品| 丁香婷婷久久| 国产精品一卡| 日韩高清不卡一区| 在线视频日韩| 蜜桃成人av| 91久久午夜| 蜜桃tv一区二区三区| 日韩伦理在线一区| 麻豆精品少妇| 老司机精品视频网| 欧美日韩一视频区二区| 日韩欧美美女在线观看| 中文久久精品| 中文字幕日本一区| 日韩一区二区三区免费视频| 婷婷激情图片久久| 伊人精品视频| 国产高清不卡| 日韩中文影院| 婷婷亚洲成人| 一区二区日韩免费看| 亚洲永久av| 成人影视亚洲图片在线| 日本不卡不码高清免费观看 | 亚洲综合激情在线| 久久精品在线| 亚洲天堂日韩在线| 欧美成人久久| 欧美激情在线精品一区二区三区| 欧洲毛片在线视频免费观看| 国产日韩高清一区二区三区在线| 色综合视频一区二区三区日韩 | 正在播放日韩精品| 国产精品久久久久久久免费观看| 国产精品一国产精品k频道56| 久久女人天堂| 国产视频一区二| 日韩和欧美的一区| 日本大胆欧美人术艺术动态|