根据表名生成insert和update字段列表

--1.生成插入字段列表
if exists(select 1 from sysobjects where id=object_id('usp_sql_insert_get') and type='P')
   drop proc usp_sql_insert_get
go
create proc usp_sql_insert_get
   @tableName varchar(20) --表名
as
/**********
[版本号]1.2.0.1
[创建时间]2010.06.02
[作者]ZHOEN
[版权]Copyright(c) 2010
[描述]
[功能说明]
   根据表名生成插入字段列表
[参数说明]
   @tableName varchar(20) --表名
[返回值]
[结果集、排序] 
[调用的sp]
[调用实例]
   exec usp_sql_insert_get @tableName
[修改历史]
**********/
  set nocount on

  declare @rowCount int,
          @rowIndex int,
          @fields varchar(1000)

  select identity(int,1,1) as lsh,[name] into #tempData
  from syscolumns
  where id=object_id(@tableName)

  select @rowCount=count(1) from #tempData

  select @rowIndex=1 , @fields = ''
  while @rowIndex<=@rowCount
  begin
    select @fields = @fields + ',' + [name]
    from #tempData
    where lsh = @rowIndex

    set @rowIndex =  @rowIndex + 1
  end

  select @fields as fields
go
--2.生成更新字段列表
if exists(select 1 from sysobjects where id=object_id('usp_sql_update_get') and type='P')
   drop proc usp_sql_update_get
go
create proc usp_sql_update_get
   @tableName varchar(20) --表名
as
/**********
[版本号]1.2.0.1
[创建时间]2010.06.02
[作者]ZHOEN
[版权]Copyright(c) 2010
[描述]
[功能说明]
   根据表名生成更新字段列表
[参数说明]
   @tableName varchar(20) --表名
[返回值]
[结果集、排序] 
[调用的sp]
[调用实例]
  exec usp_sql_update_get @tableName
[修改历史]
**********/
  set nocount on

  declare @rowCount int,
          @rowIndex int,
          @fields varchar(1000)

  select identity(int,1,1) as lsh,[name] into #tempData
  from syscolumns
  where id=object_id(@tableName)

  select @rowCount=count(1) from #tempData

  select @rowIndex=1 , @fields = ''
  while @rowIndex<=@rowCount
  begin
    select @fields = @fields + ',a.' + [name] + ' = b.' + [name]
    from #tempData
    where lsh = @rowIndex

    set @rowIndex =  @rowIndex + 1
  end

  select @fields as fields
go

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值