use tempdb 
go
if object_id(N'dbo.T') is not null
drop table dbo.t
go
create table T(id int identity,name varchar(30))
--insert  
declare @v_T table(id int,name varchar(30))
insert top (5) into t(name) output inserted.* into @v_t output inserted.*
select top (5) name from (
select 'sp_msforeachdb' as name union all
select 'sp_msforeachtable' union all
select 'sp_mointor' union all[code=SQL]
select 'sp_spaceused' union all
select 'sp_updatestats' union all
select '@@max_connections' union all
select '@@pack_received' union all
select '@@pack_sent' union all
select '@@pack_errors' ) a order by name 
select * from @v_t  --delete 
declare @v_T table(id int,name varchar(30))
delete top (5)  from t output deleted.* into @v_t output deleted.* where name in 
(select top (5) name from (
select 'sp_msforeachdb' as name union all
select 'sp_msforeachtable' union all
select 'sp_mointor' union all
select 'sp_spaceused' union all
select 'sp_updatestats' union all
select '@@max_connections' union all
select '@@pack_received' union all
select '@@pack_sent' union all
select '@@pack_errors' ) a order by name )--update 
declare @name varchar(300)
declare @v_T table(id int,newname varchar(30),oldname varchar(30))
update top (5) t set @name=name=(select max(name) from t)
output inserted.id,inserted.name,deleted.name into @v_t 
output inserted.id,inserted.name,deleted.name  where name in 
(select top (5) name from (
select 'sp_msforeachdb' as name union all
select 'sp_msforeachtable' union all
select 'sp_mointor' union all
select 'sp_spaceused' union all
select 'sp_updatestats' union all
select '@@max_connections' union all
select '@@pack_received' union all
select '@@pack_sent' union all
select '@@pack_errors' ) a order by name )
select * from @v_t
select @name--清除测试表
drop table t
--以上指是抛砖引玉,举一反三。希望大家能学习到其中的奥秒。
[/code]

解决方案 »

  1.   


    use tempdb 
    go
    if object_id(N'dbo.T') is not null
    drop table dbo.t
    go
    create table T(id int identity,name varchar(30))
    --insert  
    declare @v_T table(id int,name varchar(30))
    insert top (5) into t(name) output inserted.* into @v_t output inserted.*
    select top (5) name from (
    select 'sp_msforeachdb' as name union all
    select 'sp_msforeachtable' union all
    select 'sp_mointor' union all
    select 'sp_spaceused' union all
    select 'sp_updatestats' union all
    select '@@max_connections' union all
    select '@@pack_received' union all
    select '@@pack_sent' union all
    select '@@pack_errors' ) a order by name 
    select * from @v_t  --delete 
    declare @id int ,@name varchar(30)
    declare @v_T table(id int,name varchar(30))
    delete top (5)  from t output deleted.* into @v_t output deleted.* where name in 
    (select top (5) name from (
    select 'sp_msforeachdb' as name union all
    select 'sp_msforeachtable' union all
    select 'sp_mointor' union all
    select 'sp_spaceused' union all
    select 'sp_updatestats' union all
    select '@@max_connections' union all
    select '@@pack_received' union all
    select '@@pack_sent' union all
    select '@@pack_errors' ) a order by name )--update 
    declare @name varchar(300)
    declare @v_T table(id int,newname varchar(30),oldname varchar(30))
    update top (5) t set @name=name=(select max(name) from t)
    output inserted.id,inserted.name,deleted.name into @v_t 
    output inserted.id,inserted.name,deleted.name  where name in 
    (select top (5) name from (
    select 'sp_msforeachdb' as name union all
    select 'sp_msforeachtable' union all
    select 'sp_mointor' union all
    select 'sp_spaceused' union all
    select 'sp_updatestats' union all
    select '@@max_connections' union all
    select '@@pack_received' union all
    select '@@pack_sent' union all
    select '@@pack_errors' ) a order by name )
    select * from @v_t
    select @name--清除测试表
    drop table t
    --以上指是抛砖引玉,举一反三。希望大家能学习到其中的奥秒。 再发一下。学习一下发贴技巧,呵呵。
      

  2.   


    insert top (5) into t(name) output inserted.* into @v_t output inserted.*
    select top (5) name from (
    select 'sp_msforeachdb' as name union all
    select 'sp_msforeachtable' union all
    select 'sp_mointor' union all[code=SQL]这段语句超出常规语法,请大牛解释
      

  3.   


    output 的地方好像只用写一遍吧,写两遍也可以?