I am searching in all column data in the selected SQL table, and I have a join with another table, but my issue that the 2nd table has the same column name with tableA, how can I specify each column with its table.
So I declare the string to find and the table, if I remove the join it will work, but my issue only with joining
DECLARE @stringToFind varchar(max) ='%TELECOM%'
DECLARE @table varchar(max) ='tableA'
DECLARE @sqlCommand varchar(max) = 'SELECT * FROM [' + @table + ']
Left join Table2 on
Table2.Id= tableA.IdCategory
WHERE '
SELECT @sqlCommand = @sqlCommand + '[' + COLUMN_NAME + '] LIKE ''' + @stringToFind + ''' OR '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table
AND DATA_TYPE IN ('char','nchar','ntext','nvarchar','text','varchar')
SET @sqlCommand = left(@sqlCommand,len(@sqlCommand)-3)
EXEC (@sqlCommand)
PRINT @sqlCommand
Ambiguous column name 'Type'.
question from:
https://stackoverflow.com/questions/65952657/query-to-search-in-all-columns-in-table-with-joining 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…