2013-04-09 39 views
1

之間共享我有一個步驟可稱之爲它創建了一個臨時表溫度表的過程

#temp1 

一個

caller 

過程和EXEC另一個步驟可調用該

callee 

將某些值插入到#temp1表中的過程,以便調用程序可以使用它。現在問題是我的被調用程序只能通過調用程序或其他創建#temp1表的程序調用,然後執行被調用程序。有沒有想引入一個「如果」條件檢查,如果被叫方已被稱爲主叫方的方式則僅執行語句

'insert #temp1 ....' 

和其他明智的,避免它。

感謝

回答

1

可以爲了知道是誰調用的過程「被叫」實現邏輯 - 添加參數去「被叫」默認值= NULL(例如),然後通過僅更改「來電顯示」程序添加此參數的值爲1(例如)。在「被調用者」過程中檢查這個參數的值,如果它不是NULL,那麼使用你的臨時表。

代碼示例:

create procedure [callee] 
(
@CalledByCaller bit = NULL 
) 
as 
set nocount on 
If @CalledByCaller =1 
begin 
    SELECT '3' 
    --work with temp table 
end 
SELECT '2' 
go 

create procedure [caller] 
as 
set nocount on 

exec [callee] @CalledByCaller =1 

go 
0

如果被叫總是會產生一個結果集,它是結果集應該如果其可用插入#temp1,那麼你可以考慮轉移的INSERT進入外部程序,使用INSERT ... EXECcaller

CREATE procedure caller 
as 
    --Various things 
    create table #temp1 (column1,column2) 
    --Other things 
    insert into #temp1 (column1,column2) exec callee 
    --Work with #temp1 
+0

不能使用插入EXEC因爲SQL不會讓我有嵌套插入EXEC – LivingThing 2013-04-09 08:54:15

+0

@LivingThing - 有[that]的解決方法(http://blogs.technet.com/b/wardpond/archive/2005/08/01/408502.aspx),如果這基本上是你想要做的(嵌套的插入... exec) – 2013-04-09 08:56:49