如何在sql-server中执行多个过程脚本

how to execute multiple procedure script in sql-server

以下是我在 SQL SERVER

中编写的 select 查询
select 'exec pr_tblmaster_split ''tblmaster_'+  Cast(year_id as nvarchar(4000)) +'''' as qry
from  tblmaster
group by year_id 
order by year_id asc

这将 return

qry
-------------------------------------
exec pr_tblmaster_split 'tblmaster_1'
exec pr_tblmaster_split 'tblmaster_2'

所以我的问题是 如何使用 function/procedure 运行 return 查询?


我正在尝试将 PostgreSQL 转换为 SQL 服务器,所以

在 Postgre 中SQL 我可以像这样使用 FOR 循环

declare 
rw record;
begin
  for rw in 
         select 'select fn_tblmaster_spliting(''tblmaster_'||t.tbl||''');' crTables from (
         select distinct(year_id) tbl from tblmaster order by acyrid asc)t
   loop
         execute rw.crTables;
   end loop;
   end

将结果存储在一个variable中,通过sp_executesql执行。试试这个。

declare @sql nvarchar(max)=''
select @sql+= 'exec pr_tblmaster_split ''tblmaster_'+  Cast(year as nvarchar(4000)) +'''; ' 
from  tblmaster
group by year_id 
order by year_id asc

exec sp_executesql @sql