现在的位置: 首页 > 综合 > 正文

临时表操作

2012年09月01日 ⁄ 综合 ⁄ 共 1169字 ⁄ 字号 评论关闭

081225区别是一个是内存中表declare,一个是实现存在的表。

  1. --创建虚表的方法
  2. if object_id('tempdb..#')is not null drop table #
  3. go
  4. create table #(num varchar(10))
  5. insert # select 123
  6. insert # select 456
  7. insert # select 'ad'
  8. insert # select '2008-12-25'
  9. select cast(num as datetime) from # where isdate(num)=1
  10. --select cast(num as int) from # where isnumeric(num)=1
  11. --cast(str as int)转化 isnumeric(num)判断能不能转化为数字
  12. --cast(str as nvarchar)

081218第一个临时表应用

  1. --================临时表入门示例==================
  2. --================张树强==081218================
  3. create procedure [dbo].[Test_TempTable]
  4. as
  5. begin
  6.     declare @indextable table(id int identity(1,1),nid int) --这个临时表,有一个id主键,自增。
  7.     insert into @indextable (nid) select id from ttest --把数据插入临时表
  8.     select * from @indextable
  9. end
  10. GO

临时表与永久表相似,但临时表存储在 tempdb 中,当不再使用时会自动删除。
临时表有局部和全局两种类型
2者比较:
局部临时表的名称以符号 (#) 打头
仅对当前的用户连接是可见的
当用户实例断开连接时被自动删除
全局临时表的名称以符号 (##)

sysobjects这个表里存放临时表的信息。
if object_id('tempdb..#temptable1')is not null
begin
drop table #temptable1
end
不太理解这些语句,这里判断一下,然后删除临时表。代码运行通过。
http://www.cnblogs.com/mjgforever/archive/2008/08/15/849201.html
http://blog.chinaunix.net/u/15773/showart_348981.html
create table temptable1(gid varchar(50))
insert into temptable1
select gid from t_Goods
select * from temptable1

抱歉!评论已关闭.