打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SqlServer表数据导成Insert语句
SqlServer表数据导成Insert语句
2010年03月04日 星期四 10:09

create table test(

id int not null constraint pk_test primary key,

name nvarchar(100) null,

description nvarchar(200) null

)

生成insert 语句的SQL如下:

set nocount on

select 'insert into test(id, name, description) values(''' + convert(varchar(10), id) + ''', ''' + name + ''',''' +description + ''')' + char(13) + char(10)

from test --可以加过滤条件

set nocount off

第二种,利用存储过程:

创建:

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(8000)),''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

exec   (@sql)   

go

执行:

exec spGenInsertSQL tablename

删除:

drop proc spGenInsertSQL

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
将sql server表中的数据导出为inert into语句的形式
SQLServer动态建表
自动生成表记录Insert的SQL语句
Sql server中用现有表中的数据创建Sql的Insert插入语句
SQL语言快速入门(二)
我的数据库学习“曲线” - 《程序员》官方BLOG - 博客频道 - CSD...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服