This looks like the procedure originally used for SQL Server 2000, where pivotthere was no keyword. Change the section below to use [pivot].
SELECT @sql=@sql + '''' + convert(varchar(100), [pivot]) + ''' = ' +
stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pivot + ' WHEN '
+ @delim + convert(varchar(100), [pivot]) + @delim + ' THEN ' ) + ', ' FROM
Perhaps you should also use the data type sysnamefor the parameter @table, use the function quotenamewhen concatenating table and column names and use nvarchar, not varchar.
, SQL-, . sysname - nvarchar(128). sysname nvarchar(128), , .
varchar(100) , () 100 . , .
SQL Server
CREATE TABLE "╚╦╩╗" ( "└┬┴┐" nvarchar(10))
ASCII, unicode, , ʼ (U + 02BC), .
quotename , Robert'); DROP TABLE Students;, [Robert'); DROP TABLE Students;], .