I am creating a varchar(max) string for submission to 'EXEC' and it is less than 1000 bytes long, yet the 'exec' errors out - seems to not get the entire string.
Below is the code that errors out:
create or alter procedure usp_utility_ListFullTextCatalog_items @Database varchar(100)
as
declare @sql varchar(max), @DBName varchar(max)
set @DBName = @Database
set @sql = cast('' as varchar(max))
+ 'SELECT t.name as TableName,
c.name as FTCatalogName,
i.name as UniqueIdxName,
cl.name as ColumnName,
cdt.name as DataTypeColumnName '
+ 'from ' + @DBName + '.sys.tables t inner join '
+ ' ' + @DBName + '.sys.fulltext_indexes fi on '
+ ' ' + 't.[object_id] = fi.[object_id] inner join '
+ ' ' + @DBName + '.sys.fulltext_index_columns ic on '
+ ' ic.[object_id] = t.[object_id] inner join '
+ ' ' + @DBName + '.sys.columns cl on '
+ ' ic.column_id = cl.column_id and '
+ ' ic.[object_id] = cl.[object_id] inner join '
+ ' ' + @DBName + '.sys.fulltext_catalogs c on '
+ ' fi.fulltext_catalog_id = c.fulltext_catalog_id inner join '
+ ' ' + @DBName + '.sys.indexes i on '
+ ' fi.unique_index_id = i.index_id and '
+ ' fi.[object_id] = i.[object_id] left join '
+ ' ' + @DBName + '.sys.columns cdt on '
+ ' ic.type_column_id = cdt.column_id and '
+ ' fi.object_id = cdt.object_id'
select len(@sql)
select right(@sql,50)
execute @sql
go
execute usp_utility_ListFullTextCatalog_items 'AdventureWorks2022'
When I run the above, the first 'select' at the bottom of the procedure shows the length of @sql to be 900.
The 'execute @sql' statement at the end of the stored procedure produces the below error:
Msg 203, Level 16, State 2, Procedure usp_utility_ListFullTextCatalog_items, Line 33 [Batch Start Line 34]
The name 'SELECT t.name as TableName,
c.name as FTCatalogName,
i.name as UniqueIdxName,
cl.name as ColumnName,
cdt.name as DataTypeColumnName from AdventureWorks2022.sys.tables t inner join AdventureWorks2022.sys.fulltext_indexes fi on t.[object_id] = fi.[object_id] inner join AdventureWorks2022.sys.fulltext_index_columns ic on ic.[object_id] = t.[object_id] inner join AdventureWorks2022.sys.columns cl on ic.column_id = cl.column_id and ic.[object_id] = cl.[object_id] inner join AdventureWorks2022.sys.fulltext_catalogs c on fi.fulltext_catalog_id = c.fulltext_catalog_id inne' **_is not a valid identifier._**
The error message does not show the entire value of @sql, yet the second 'select' at the bottom of the procedure shows the last 50 bytes of @sql to be "= cdt.column_id and fi.object_id = cdt.object_id" - which is what they are supposed to be.
execute (@sql);Think thats your complete issue tbh dbfiddle.uk/n3NChKxD