I have a schema where I have defined procedures. This procedures can be modified and should be copied between all tenants (we have schema-based tenancy). The main idea is to have some sort of templated procedures which will be automatically updated through the other tenant's schemas after procedures had been modified. To write this 'copying' procedure I wrote something like this:
DECLARE
@proc_name NVARCHAR(150) = 'ProcedureToCopy',
@proc_sql NVARCHAR(MAX),
@schema_name NVARCHAR(MAX) = 'test';
SELECT
@proc_sql = REPLACE(ROUTINE_DEFINITION, '[templates].', '['+@schema_name+'].')
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_TYPE='PROCEDURE' AND
ROUTINE_NAME = @proc_name AND
ROUTINE_SCHEMA = 'templates'
PRINT(@proc_sql);
EXEC(@proc_sql);
But I have faced an issue with big procedures (more than 17k symbols). It can not be copied because NVARCHAR(MAX)
is limited with 4k symbols, so my @proc_sql
is trimmed and doesn't contain all required procedure definition.
Can anybody suggest a solution to my problem or maybe another approach?