如何在一個存儲過程中使用另一個存儲過程的結果
我編寫了一個存儲過程在Crystal Report中使用。它工作得很好。我還想在另外一個存儲過程中使用這個存儲過程產生的結果。我該怎么做?
專家回答:
只要存儲過程只產生了一個單個的結果,要在另外一個存儲過程中使用這個存儲過程產生的輸出,這個技術是非常直接的。這個技術就是使用一個臨時表來裝載存儲過程的巨額iguo,然后通過INSERT EXEC語句來執行這個過程并保存結果。一旦結果保留在臨時表中了,他們就可以像使用其它表數據一樣來使用它了。
這里是我們可能會復用的一個過程例子:
CREATE PROC usp_Demo_AllAuthors as select * from pubs..authors
GO
現在有一個存儲過程使用usp_Demo_AllAuthors的結果:
CREATE proc usp_Demo_SPUser as CREATE TABLE #Authors (
au_id varchar(11) NOT NULL PRIMARY KEY CLUSTERED,
au_lname varchar (40) NOT NULL ,
au_fname varchar (20) NOT NULL ,
phone char (12) NOT NULL,
address varchar (40) NULL ,
city varchar (20) NULL ,
state char (2) NULL ,
zip char (5) NULL ,
contract bit NOT NULL
)– Execute usp_Demo_AllAuthors storing the
– results in #Authors
insert into #Authors
exec usp_Demo_AllAuthors– Here we use the #Authors table. This example only
– only selects from the temp table but you could do much
– more such as use a cursor on the table or join with
– other data.
SELECT au_fName + ‘ ‘ + au_lname as [name]
, address+’, ‘+city+’, ‘+state+’ ‘+zip [Addr]
from #AuthorsDROP TABLE #Authors
GO
SQL Server 2005中的存儲過程并發問題
問題提交于2006年7月26日
我在SQL Server2005中遇到了并發問題。我持有車票的公共汽車上有一些空閑的座位。我在插入銷售的查票之前,需要查看是否還有空閑的座位。我的存儲過程做的事情如下所示:
CREATE PROCEDURE add_ticket — parameters DECLARE free_seats int BEGIN TRANSACTION SELECT free_seats = COUNT(*) FROM tickets WHERE seat_is_not_taken IF free_seats <> 0 INSERT INTO tickets VALUES(…) — some other statements END TRANSACTION
文章來源于領測軟件測試網 http://www.kjueaiud.com/