怎么把表里的数据 变成SQL语句

guanjm 2006-06-08 11:27:29
怎么把表里的数据 变成SQL语句
就是把表里的数据 都变成insert into 的插入语句
...全文
199 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
拼动态sql就可以了,比如表tableA 有3个字段colA int型, colB varchar型, colC datetime型select 'insert into tableA(colA, colB, colC) values(' + cast(colA as varchar) + ', '
+ ''' + colB + '',' + ''' + convert(varchar(17), colC, 121) + '')' + char(10) + 'go'
from tableA

  • 打赏
  • 举报
回复
拼动态sql就可以了,比如表tableA 有3个字段colA int型, colB varchar型, colC datetime型select 'insert into tableA(colA, colB, colC) values(' + cast(colA as varchar) + ', '

from tableA
WangZWang 2006-06-08
  • 打赏
  • 举报
回复
--使用如下过程
CREATE Proc sp_GenInsertSQL (@tablename varchar(256),@filer varchar(1000))
as
begin
declare @sql varchar(8000),
@sqlValues varchar(8000)

set @sql =' ('
set @sqlValues = 'values (''+'

Select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'

when xtype in (58,61)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'

when xtype in (167)

then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

when xtype in (231)

then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

when xtype in (175)

then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'

when xtype in (239)

then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'

else '''NULL'''

end as Cols,name

from syscolumns

where id = object_id(@tablename)

) T

set @sql ='select ''INSERT INTO ['+ @tablename + ']' +
left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4)
+ ')'' from '+@tablename+' '+@filer

--print @sql

exec (@sql)
end

--调用
Exec sp_GenInsertSQL '表名','条件' --如果没有条件就为空,如果有输入Where 条件
xeqtr1982 2006-06-08
  • 打赏
  • 举报
回复
第三方工具。。

http://www.678e.cn/SoftView/SoftView_8582.html
OracleRoob 2006-06-08
  • 打赏
  • 举报
回复
使用第三方工具ms-sql.exe 2.0
guanjm 2006-06-08
  • 打赏
  • 举报
回复
第三方工具。。

http://www.678e.cn/SoftView/SoftView_8582.html
------------------------------
就这个 太爽了 谢谢
itblog 2006-06-08
  • 打赏
  • 举报
回复
同意,用第三方工具~
superunusa 2006-06-08
  • 打赏
  • 举报
回复
写出主本语句,然后分拆就就行了。

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧