日期:2014-05-17  浏览次数:20507 次

一个存储过程插入临时表 另一个存储过程 查询临时表
是这样 我有一个事务临时表 RcvBusinessHSESSION
我在存储过程A中 往这个表里插入了一部分数据
然后我A里面会调用另一个存储过程B
这个B会查询A刚才插入到临时表中的数据。
但是当我查询的时候 就提示我 试图访问已经在使用的事务处理临时表。。
求大神们帮忙解决一下。。
有没有什么暴力使用的方法。。

在线等
临时表 存储过程 多个

------解决方案--------------------
你搞的太复杂了吧

事务结束前,别的连接时无法查询当前表中的内容的,
比如你在一个窗口中

begin tran

insert into t1 values (1)

别提交

你再开一个窗口,select * from t1 试试看,查不到结果吧

你的情况,不开事务的话,应该是可以的

我试了下

--第一个过程
create PROCEDURE Test1
-- Add the parameters for the stored procedure here

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
if exists (select * from sys.objects where name='temp1' and type='U')
drop table temp1
  select * into temp1 from Account
 
 exec test2
END
GO

--第二个过程
create PROCEDURE test2
-- Add the parameters for the stored procedure here

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
select * from temp1

END

--这样是没问题的
exec Test1


------解决方案--------------------
事务有ACID原子隔离属性,事务不提交前,其他的过程语句是访问不了还没有提交的数据,否则就成为dirty read了。
可以通过with nolock来访问表,如select * from temptable (with nolock) with (nolock)
但楼主的问题似乎简单问题复杂化了,完全可以在一个Stored procedure中处理完这些的


例如

--with transaction version
begin tran
insert into tmptable(..) values(..)
commit
select * from tmptable with (nolock)  where ...
 

--without transaction version
insert into tmptable(..) values(..)
if @@rowcount >0
begin
select * from tmptable with (nolock)  where ...
end