弱弱的问一句:如何在sql sever2008里,
用sql语句导出sql脚本呢?
------解决方案--------------------
数据库右键-->任务-->生成脚本,里面还可以设置数据,不是挺方便的吗?
有向导还是不要用sql语句了
- SQL code
create procedure [dbo].[sp_generate_insert_script]@tablename_mask varchar(30) = NULLasbegindeclare @tablename varchar (128)declare @tablename_max varchar (128)declare @tableid intdeclare @columncount numeric (7,0)declare @columncount_max numeric (7,0)declare @columnname varchar (30)declare @columntype intdeclare @string varchar (30)declare @leftpart varchar (8000) /* 8000 is the longest string SQLSrv7 can EXECUTE */declare @rightpart varchar (8000) /* without having to resort to concatenation */declare @hasident intset nocount on-- take ALL tables when no mask is given (!)if (@tablename_mask is NULL)beginselect @tablename_mask = '%'end-- create table columninfo now, because it will be used several timescreate table #columninfo(num numeric (7,0) identity,name varchar(30),usertype smallint)select name,idinto #tablenamesfrom sysobjectswhere type in ('U' ,'S')and name like @tablename_mask-- loop through the table #tablenamesselect @tablename_max = MAX (name),@tablename = MIN (name)from #tablenameswhile @tablename <= @tablename_maxbeginselect @tableid = idfrom #tablenameswhere name = @tablenameif (@@rowcount <> 0)begin-- Find out whether the table contains an identity columnselect @hasident = max( status & 0x80 )from syscolumnswhere id = @tableidtruncate table #columninfoinsert into #columninfo (name,usertype)select name, typefrom syscolumns Cwhere id = @tableidand type <> 37 -- do not include timestamps-- Fill @leftpart with the first part of the desired insert-statement, with the fieldnamesselect @leftpart = 'select ''insert into [email protected]select @leftpart = @leftpart + '('select @columncount = MIN (num),@columncount_max = MAX (num)from #columninfowhile @columncount <= @columncount_maxbeginselect @columnname = name,@columntype = usertypefrom #columninfowhere num = @columncountif (@@rowcount <> 0)beginif (@columncount < @columncount_max)beginselect @leftpart = @leftpart + @columnname + ','endelsebeginselect @leftpart = @leftpart + @columnname + ')'endendselect @columncount = @columncount + 1endselect @leftpart = @leftpart + ' values('''-- Now fill @rightpart with the statement to retrieve the values of the fields, correctly formattedselect @columncount = MIN (num),@columncount_max = MAX (num)from #columninfoselect @rightpart = ''while @columncount <= @columncount_maxbeginselect @columnname = name,@columntype = usertypefrom #columninfowhere num = @columncountif (@@rowcount <> 0)beginif @columntype in (39,47) /* char fields need quotes (except when entering NULL);* use char(39) == ', easier readable than escaping*/beginselect @rightpart = @rightpart + '+'select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(' + @columnname + ',' + replicate( char(39), 4 ) + ',' + replicate( char(39), 6) + ')+' + replicate( char(39), 4 ) + ',''NULL'')'endelse if @columntype = 35 /* TEXT fields cannot be RTRIM-ed and need quotes *//* convert to VC 1000 to leave space for other fields */beginselect @rightpart = @rightpart + '+'select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(convert(varchar(1000),' + @columnname + ')' + ',' + replicate( char(39), 4 ) + ',' + replicate( char(39), 6 ) + ')+' + replicate( char(39), 4 ) + ',''NULL'')'endelse if @columntype in (58,61,111) /* datetime fields */beginselect @rightpart = @rightpart + '+'select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+convert(varchar(20),' + @columnname + ')+'+ replicate( char(39), 4 ) + ',''NULL'')'endelse /* numeric types */beginselect @rightpart = @rightpart + '+'select @rightpart = @rightpart + 'ISNULL(convert(varchar(99),' + @columnname + '),''NULL'')'endif ( @columncount < @columncount_max)beginselect @rightpart = @rightpart + '+'','''endendselect @columncount = @columncount + 1endendselect @rightpart = @rightpart + '+'')''' + ' from ' + @tablename-- Order the select-statements by the first column so you have the same order for-- different database (easy for comparisons between databases with different creation orders)select @rightpart = @rightpart + ' order by 1'-- For tables which contain an identity column we turn identity_insert on-- so we get exactly the same contentif @hasident > 0select 'SET IDENTITY_INSERT ' + @tablename + ' ON'exec ( @leftpart + @rightpart )if @hasident > 0select 'SET IDENTITY_INSERT ' + @tablename + ' OFF'select @tablename = MIN (name)from #tablenameswhere name > @tablenameendend