2015-02-06 111 views
0

這是事情。 我有一個規則表,其中包括我將運行的一些規則。 現在我需要一個過程來運行所有規則一次,我無法通過RuleId。一次執行exec多次查詢一次SQL

像規則表: RuleId RuleDescription RuleSqlQuery

而且所有的查詢返回相同的記錄(ID,姓名,其他)

那麼,如何創建一個過程。

+1

你嘗試過這麼遠嗎?從問題來看,你似乎甚至沒有嘗試創建它...... – Hozikimaru 2015-02-06 19:21:29

+0

我創建了一個輸出表,規則表。當我可以選擇一條規則時,我可以創建程序。但是如何將所有規則聯合在一起 – 2015-02-06 19:31:17

+2

您使用的是什麼dbms? – Madison 2015-02-06 19:46:42

回答

0

如果您正在使用SQL Server,也許這樣的事情...

create procedure runrules 

as 

declare @temp table (id int identity(1,1), ruleid int) 
declare @curid int 
declare @maxid int 
declare @tempsql nvarchar(max) 

insert into @temp select ruleid from rule 

select @curid = 1, @maxid = max(id) from @temp 

while @curid <= @maxid 
begin 

    select @tempsql = RuleSqlQuery 
    from rule R 
    inner join @temp T on T.ruleid = R.ruleid 
    where T.id = @curid 

    EXECUTE sp_executesql @tempsql 

    set @curid = @curid + 1 
end