生成插入语句的sql

wishY 2009-09-02 11:09:01
求一个生成插入语句的sql. 要可以选定字段的那种.
怎么写?
我找到一个,但是是生成全部的字段.但我现在需要去掉一些字段.

Create Proc proc_insert (@tablename varchar(256))
as -- 表名称
begin
set nocount on
Declare @sqlstr varchar(4000),
@sqlstr1 varchar(4000),
@sqlstr2 varchar(4000)

Select @sqlstr='select ''Insert '+@tablename

Select @sqlstr1= ' Values ( ''+', @sqlstr2=' ('

Select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from(Select case

-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'

when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'

when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'

when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'

-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

else '''NULL'''

end as col,a.colid,a.name

from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36

)t order by colid

select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename

exec( @sqlstr)

set nocount off

end
...全文
111 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiequan2 2009-09-02
  • 打赏
  • 举报
回复
declare @t table(id int,idx varchar(50))
insert into @t
select 1,'a' union all
select 2,'b' union all
select 3,'c'




select 'insert into @t(idx) select '''+idx+'''' from @t --将idx改成你想要的字段

/*
insert into @t(idx) select 'a'
insert into @t(idx) select 'b'
insert into @t(idx) select 'c'

(3 行受影响)


*/
zhangle861010 2009-09-02
  • 打赏
  • 举报
回复
楼主贴的都够我学习了! 


只能帮顶了!!
华夏小卒 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wishy 的回复:]
引用 6 楼 josy 的回复:
这个需求,楼主可以插入完整的字段后,在去掉不要的字段

这个貌似是个主意.
[/Quote].
wishY 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 josy 的回复:]
这个需求,楼主可以插入完整的字段后,在去掉不要的字段
[/Quote]
这个貌似是个主意.
--小F-- 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dawugui 的回复:]
你理解错了.

他要生成sql语句的存储过程.
[/Quote]
撤退 都是我的错
百年树人 2009-09-02
  • 打赏
  • 举报
回复
这个需求,楼主可以插入完整的字段后,在去掉不要的字段
pl_mm 2009-09-02
  • 打赏
  • 举报
回复
既然有選擇
存出過程應該還要有個參數傳遞吧~~
dawugui 2009-09-02
  • 打赏
  • 举报
回复
你理解错了.

他要生成sql语句的存储过程.
xiequan2 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dawugui 的回复:]
困难,帮顶.
[/Quote]
乌龟都觉得困难,我只能帮顶了
--小F-- 2009-09-02
  • 打赏
  • 举报
回复
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用) 
法一:select * into b from a where 1 <>1
法二:select top 0 * into b from a

2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from a;
dawugui 2009-09-02
  • 打赏
  • 举报
回复
困难,帮顶.

34,593

社区成员

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

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