DECLARE @ColumnTotal INT,
@HTProcOfficeColumnNames NVARCHAR(4000),
@TBLColumnNames NVARCHAR(4000),
@query NVARCHAR(4000),
@subqueryInsert NVARCHAR(4000),
@subqueryValues NVARCHAR(4000)
SELECT @ColumnTotal = Count(*)
FROM (SELECT htprocoffice
FROM table
WHERE COLUMN NOT LIKE '% - CO'
GROUP BY COLUMN) T
SELECT @HTProcOfficeColumnNames = COALESCE(@HTProcOfficeColumnNames
+ ',[' + htprocoffice + ']', '[' + htprocoffice + ']')
FROM (SELECT htprocoffice
FROM table
WHERE COLUMN NOT LIKE '% - CO'
GROUP BY COLUMN) T
ORDER BY COLUMN
SELECT @TBLColumnNames = COALESCE(@TBLColumnNames + ',' + NAME, NAME)
FROM table
WHERE id IN (SELECT id
FROM table
WHERE type = 'U'
AND NAME = 'TableName')
AND NAME LIKE 'HTOffice%'
ORDER BY NAME
以下代码是否有任何错误?
set @subqueryInsert = substring(@TBLColumnNames,1,@ColumnTotal*11-1) --The error occurs here but not sure though.
set @subqueryInsert = 'INSERT INTO Table
(RunDate,OrderBy,Period,IConOffice,IConOfficeType,'+ @subqueryInsert+')'
set @subqueryValues =
replace(replace(@HTProcOfficeColumnNames,'[',''''), ']','''')