临时表的使用

  一、临时表主要用于在数据库临时存放数据时使用,表明以#号开头。

  --1.查询数据库是否存在临时表#t_Data,有则删除

    if exists (select * from tempdb.dbo.sysobjects where id=object_id('tempdb.dbo.#t_Data'))

     drop table #t_Data;

  --2.复制表tbgYearBudgetList,把表tbgYearBudgetList中的数据根据条件筛选出来,添加到临时表#tbgYearBudgetListTemp中

  --此时临时表与tbgYearBudgetList表完全相同

    select * into #tbgYearBudgetListTemp from tbgYearBudgetList  where YearBudgetID=1001

  --3.把tbgYearBudgetList表筛选出来的数据加到临时表中
    insert into #tbgYearBudgetListTemp(YearBudgetID,ProjectName)
    select YearBudgetID,'0' from tbgYearBudgetList  where YearBudgetID=1001

  --4、创建临时表,以字段  类型的格式创建

    Create Table #t_Data  (

      ID int identity(1,1), --数据条数id列

      DeptID  int,

      name varchar(500),

    )

  

posted @ 2016-09-30 17:47  ☺Pual  阅读(124)  评论(0编辑  收藏  举报