如何在存储过程中删除表变量``~~????

lxy_lxy 2009-03-03 04:56:51
如何我在while循环中声明了一个表变量如下:

declare @table table(id int identity(1,1),tid int)

每次循环的时候我都要重新定义 @table,为的是的赋值都是新的,尤其是自动增长列id

可是,现在我没次循环到这点时候,@table都不重新新建,而是直接向里面插入了其他数据,怎么办、、、、、、、、?那么如何在存储过程中将@table删除掉呢···??
...全文
463 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-03-03
  • 打赏
  • 举报
回复
通过临时表实现
create table #ls
(

)
sjt000 2009-03-03
  • 打赏
  • 举报
回复
declare @table table(id int identity(1,1),tid int)
--------------------------------------------------------
这样写应该是不能通过的
因为表变量和临时表的一个区别就是表变量中不能定义索引列
天乐_那由他 2009-03-03
  • 打赏
  • 举报
回复
http://www.cnblogs.com/zhc088/archive/2008/01/31/1059936.html

sql 创建表变量,临时表
Posted on 2008-01-31 14:42 小飞龙(Jack) 阅读(677) 评论(1) 编辑 收藏 网摘
基本原则:能用表变量就用表变量.实在不行才使用临时表
表变量主要是开销系统的内存,而临时表则使用tempdb.对于小数据量的中间数据存储,可以使用表变量,而当需要临时保存的数据很大时,建议使用临时表.
declare @tb table(id int,name varchar(50),age int) --创建表变量

insert @tb select 1,'nn',14
select * from @tb


create table #t(id int,name varchar(50),years int,nums int)--创建临时表

insert #t select 1,'nn',14,15
union all select 1,'nn',14,15
insert into #t exec sp_gets --可以用于存储过程或动态SQL结合

select * from #t
drop table #t --删除临时表


实例
------------------------------------------------------------------ ----------------------------

declare @tab table
(
id int,
name nvarchar(50)
)
insert into @tab(id,name)
select person_id,1 from personinfo
select * from @tab
------------------------------------------------------------------ ----------------------------


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Jack zhang>
-- Create date: <2007-08-04>
-- Description: <获取家庭吸烟总量(创建表变量)>
-- =============================================

ALTER PROCEDURE [dbo].[Family_SmokeTotal]
(@Family_ID int)
AS
declare @tab table(Person_ID int)
insert into @tab(Person_ID)
select Person_ID from PersonInfo where Family_ID=@Family_ID
select Sum(SmokeCount) '家庭吸烟总数' from PersonActionInfo where Person_id in (select Person_id from @tab)

//临时表
CREATE TABLE #tmp
(
rq NVARCHAR(10),
shengfu NVARCHAR(1)
)
INSERT into #tmp select'2005-05-09','胜'
Insert into #tmp select'2005-05-09','胜'
insert into #tmp select'2005-05-09','负'
insert into #tmp select'2005-05-09','负'
insert into #tmp select'2005-05-10','胜'
insert into #tmp select'2005-05-10','负'
insert into #tmp select'2005-05-10','负'

SELECT rq,sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负'
from
#tmp
group by rq

drop table #tmp

天乐_那由他 2009-03-03
  • 打赏
  • 举报
回复
若只想清空后使用,也可以在循环开始时使用delete删除所有数据
天乐_那由他 2009-03-03
  • 打赏
  • 举报
回复
若用循环,可以把表变量的创建放到循环内部
一弗楚 2009-03-03
  • 打赏
  • 举报
回复
用临时表吧
create table #table(id int identity(1,1),tid int)
insert into #table
values(1)
select * from #table
drop table #table
go
create table #table(id int identity(1,1),tid int)
insert into #table
values(1)
select * from #table
drop table #table
天乐_那由他 2009-03-03
  • 打赏
  • 举报
回复
表变量在批处理结束时自动被系统删除

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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