有之前的.sql文件,在新的数据库中打开后。 想生成新的脚本文件。 基于现在的数据库。 该如何操作, 菜鸟一个,求大家指点下。
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'
是不同数据库脚本的切换吗?看看这个
http://wenku.baidu.com/link?url=XxEUuhfs-cfe-2zVKPyBihnNRCV19nuI03kAf1zvuLHzI-UqA4oPCbGU97S07WUG0TkUBnB3xLsfbqX6yV8YLCPahD-htPTjC_VMRbIPBwG
sql语句有一种特别的用法,相信很多人还不知道,我也是后来学到的,因此拿来跟大家分享下:生成sql脚本
举个例子:
select 1 from student;
如果表中有数据的话,你就会发现查询的结果会是:
根据这个原理,我们可以用来生成sql脚本,以下就是实例:
实例1:一个简单的例子
select * from Popedom
select 'insert intoPopedom(id,popedom_name,popedom_value) values
('+cast(id as varchar(10))+',''' +popedom_name + ''',
''' + popedom_value+ ''')' FROM Popedom
insert intoPopedom(id,popedom_name,popedom_value) values (1,'电厂一览', 'PRIV_ELECTINFO')
insert intoPopedom(id,popedom_name,popedom_value) values (2,'概算信息', 'PRIV_BUDGET')
insert intoPopedom(id,popedom_name,popedom_value) values (3,'合同信息', 'PRIV_CONTRACT')
insert intoPopedom(id,popedom_name,popedom_value) values (4,'投资信息', 'PRIV_INVESTMENT')
insert intoPopedom(id,popedom_name,popedom_value) values (5,'进度信息', 'PRIV_PROCESS')
insert intoPopedom(id,popedom_name,popedom_value) values (6,'安全信息', 'PRIV_SAFETY')
insert intoPopedom(id,popedom_name,popedom_value) values (7,'日常工作分配', 'PRIV_SETUP')
insert intoPopedom(id,popedom_name,popedom_value) values (8,'日常工作浏览', 'PRIV_AWAKE')
insert intoPopedom(id,popedom_name,popedom_value) values (9,'日常工作录入', 'PRIV_TASK')
insert intoPopedom(id,popedom_name,popedom_value) values (10,'提醒基本信息设置', 'PRIV_BASE')
insert intoPopedom(id,popedom_name,popedom_value) values (11,'个人信息设置', 'PRIV_PERSON')
insert intoPopedom(id,popedom_name,popedom_value) values (12,'超级管理员', 'PRIV_ADMIN')
insert intoPopedom(id,popedom_name,popedom_value) values (13,'设计信息', 'PRIV_DESIGN')
insert intoPopedom(id,popedom_name,popedom_value) values (14,'质量信息', 'PRIV_QUALITY')
insert intoPopedom(id,popedom_name,popedom_value) values (15,'日常工作汇报', 'PRIV_REPORT')
insert intoPopedom(id,popedom_name,popedom_value) values (16,'日常工作审核', 'PRIV_CHECK')
insert intoPopedom(id,popedom_name,popedom_value) values (17,'电厂信息录入', 'PRIV_ElECTINFO')
insert intoPopedom(id,popedom_name,popedom_value) values (18,'设计信息录入', 'PRIV_DESIGNINFO')
实例2:相对复杂一点的例子
SELECT 'INSERT INTOPrevise(PreviseID,PreviseDate,PreviseInfo) VALUES
('+cast(PreviseID as varchar(10))+',''' +CONVERT(varchar(19), PreviseDate, 121) + ''',
''' + PreviseInfo + ''')' FROM PreviseWHERE PreviseID > 100
http://blog.csdn.net/wangpeng047/article/details/8868849
mysqldump -u root -p [新数据库名] > [导出数据库名].sql