How to execute multiple script procedures in sql server

Below is my select query written in SQL SERVER

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

      

and it will return

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

      

So my question is, how can I trigger a return request using a function / procedure ?.


I am trying to convert PostgreSQL to SQL Server, so

In PostgreSQL I can use FOR LOOP like this

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

      

+3


source to share


1 answer


Save the result in variable

and run through sp_executesql

. Try it.



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

      

+2


source







All Articles