0

I'm having some trouble writing functions in T-SQL for SQL Server 2014.

I understand that T-SQL functions are not allowed to execute things like UPDATE/DELETE etc but this is a problem for me because I have several SQL processes that I use for PostgreSQL that do exactly what I want but I can't make a SQL Server version of them because of this limitation.

For example I have the following (really watered down version) of a process I use on PostgreSQL.

drop type if exists localtype cascade;
create type localtype as (
   id integer,
   status text
);

drop function if exists rowmaintenance();
create function rowmaintenance(total integer, status text)
returns setof localtype as $$

   declare
      result localtype;
      maincursor record;

begin

   for maincursor in
      select * from colours
      where co_num <= total
   loop

      update colours
      set co_status = status;

      result.id := maincursor.co_num;
      result.status = status;

      return next result;

   end loop;

end $$
language 'plpgsql';

select * from rowmaintenance(10, 'active');

Many of our processes will run through records and make updates and then return results (several rows) of what has been updated. I am unable to achieve this in T-SQL. Is there an alternate language or way to achieve something similar in T-SQL?

I've tried T-SQL functions but the limitations are too severe

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106

0 Answers0