sql2005數(shù)據(jù)導(dǎo)出方法(使用存儲過程導(dǎo)出數(shù)據(jù)為腳本)
執(zhí)行語句:exec KeleyiOutputSqlData thetablename
以下是創(chuàng)建存儲過程腳本:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[KeleyiOutputSqlData]
@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 '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 -- 判斷對象是否是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'
相關(guān)文章
SqlServer 2005 中字符函數(shù)的應(yīng)用
SqlServer 2005 中字符函數(shù)的應(yīng)用,需要的朋友可以參考下。2010-07-07Win2008中安裝的MSSQL2005后無法訪問的解決方法
最近筆者一直在使用Win2008系統(tǒng),不過發(fā)現(xiàn)一個(gè)很奇怪的問題,那就是在該系統(tǒng)上安裝了SQL2005后,再在其他計(jì)算機(jī)訪問該主機(jī)顯示不能訪問2014-07-07SqlServer 2005 T-SQL Query 學(xué)習(xí)筆記(3)
利用ROW_NUMBER()進(jìn)行高效率的分頁。2010-02-02SQLServer ntile獲取每組前10%的數(shù)據(jù)
sqlserver2005有關(guān)鍵字ntile(x)和over(partition by.. order by..)子句配合.2009-08-08sql server 2005用戶權(quán)限設(shè)置深入分析
關(guān)于什么是用戶權(quán)限,最簡單的定義可能是,用戶能做什么和不能做什么,本文將詳細(xì)介紹sql server 2005用戶權(quán)限設(shè)置,需要了解的朋友可以參考下2012-11-11Sql Server 2005的1433端口打開局域網(wǎng)訪問和進(jìn)行遠(yuǎn)程連接
在實(shí)際項(xiàng)目中,我們經(jīng)常會遇到需要局域網(wǎng)訪問或者需要外網(wǎng)訪問甚至是兩者都需要的數(shù)據(jù)庫搭建,那么應(yīng)該如何來處理呢,我們來一一探討下2014-08-08