我在表中有一个存储过程列表,其中包含日期时间,需要动态/同时执行。
正常执行正常,但是按顺序执行所有程序。
表格结构
+----------+----------+-----------------------+
| Screenid | SP Name | ExecutionTime |
+----------+----------+-----------------------+
| 102457 | SPName1 | 2017-03-02 09:15:000 |
| 103587 | SpName7 | 2017-03-02 09:15:000 |
| 104578 | SPName15 | 2017-03-02 10:25:0000 |
+----------+----------+-----------------------+
这是我的代码:
DECLARE Tbl_cursor CURSOR FOR
SELECT DISTINCT Srctable,SrcSP,Truncatereq,executeReq,exectime
FROM IDB_Define_Source a (nolock)
WHERE exectime < getdate()
AND execstatus = 'N'
ORDER BY exectime
OPEN Tbl_cursor;
-- Perform the first fetch.
FETCH NEXT FROM Tbl_cursor INTO @srctable, @srcSp, @trunreq, @exereq, @exetime ;
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
DECLARE @str nvarchar(max)
SET @str = 'EXEC reportsdb.reportuser.'+ @srcSp
EXEC sp_executesql @str
UPDATE T1
SET execstatus = 'Y'
FROM IDB_Define_Source T1
WHERE Srctable = @srctable
END TRY
BEGIN CATCH
UPDATE T1
SET exectime = exectime + datediff(dd, exectime, getdate()) + 1,
execstatus = 'Y',
ErrorDtl = ERROR_MESSAGE()
FROM IDB_Define_Source T1
WHERE Srctable = @srctable
END CATCH
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM Tbl_cursor INTO @srctable, @srcSp, @trunreq, @exereq, @exetime;
END
CLOSE Tbl_cursor;
DEALLOCATE Tbl_cursor;
答案 0 :(得分:0)
您可以使用光标然后尝试Logic Like
DECLARE @Query text
SET @Query = (Select Col_SP FROM Tablename)
EXECUTE sp_executesql @Query
答案 1 :(得分:0)
是的,您可以循环调用所有内容。从表中获取列表并进行一个while循环并调用它。简单的例子就像波纹管一样。
DECLARE @Total_Count INT,@Counter INT = 1;
SELECT ROW_NUMBER() OVER(Order by SPName) RowNo,SPName
INTO #TmpTable
FROM #TableName
SET @Total_Count = (Select Max(RowNo) from #TmpTable);
WHILE @Counter <= @Total_Count
BEGIN
Declare @EXESPName Varchar(200);
@EXESPName = (Select SPName FROM TmpTable WHERE RowNo = @Counter)
Exec @EXESPName
SET @Counter = @Counter + 1;
END;