I have updated your Logic as per below and it works fine, please have a look:
DECLARE @SearchStr VARCHAR(50)='Surat'
DECLARE @Results TABLE(DatabaseName NVARCHAR(500), TableName nvarchar(370),ColumnName nvarchar(370), ColumnValue nvarchar(3630),PrimaryKey nvarchar(200), PrimaryKeyValue nvarchar(4000))
SET NOCOUNT ON
DECLARE @TableList AS Table
(
TableName VARCHAR(500),
RowNo INT
)
DECLARE @ColumnList AS Table
(
ColumnName VARCHAR(500),
RowNo INT
)
DECLARE @PrimaryKeyList AS Table
(
PrimaryKeyName VARCHAR(500)
)
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110), @PrimaryKey nvarchar(200), @CurrentTableName nvarchar(256)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%', '''')
DECLARE @DatabaseCount INT=0, @Index INT=0, @DatabaseName NVARCHAR(500), @TotalColumnCount INT, @ColumnIndex INT=0, @TotalTableCount INT, @TableIndex INT=0
SELECT
*,
ROW_NUMBER() OVER (ORDER BY name) AS RowNo
INTO #tblDatabases
FROM Sys.Databases
WHERE name NOT IN ('master','model','msdb','tempdb')
SELECT @DatabaseCount=COUNT (*) FROM #tblDatabases
WHILE @Index<@DatabaseCount
BEGIN
SET @Index=@Index+1
SELECT @DatabaseName='',@TableIndex=0,@ColumnIndex=0,@TableName='',@ColumnName=''
SELECT @DatabaseName=name FROM #tblDatabases WHERE RowNo=@Index
DELETE FROM @TableList
INSERT INTO @TableList
EXEC('
SELECT QUOTENAME(TABLE_SCHEMA) + ''.'' + QUOTENAME(TABLE_NAME),
ROW_NUMBER() OVER (ORDER BY TABLE_NAME)
FROM ['+@DatabaseName+'].INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = ''BASE TABLE''
')
SELECT @TotalTableCount=COUNT(*) FROM @TableList
WHILE @TableIndex<@TotalTableCount
BEGIN
SET @TableIndex=@TableIndex+1
SELECT @ColumnName = '',@ColumnIndex=0
SELECT @TableName=TableName FROM @TableList WHERE RowNo=@TableIndex
SET @CurrentTableName=REPLACE(REPLACE(REPLACE(@TableName,'[dbo].',''),'[',''),']','')
DELETE FROM @ColumnList
INSERT INTO @ColumnList
EXEC('SELECT
COLUMN_NAME,ROW_NUMBER() OVER (ORDER BY COLUMN_NAME)
FROM ['+@DatabaseName+'].INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME('''+@TableName+''', 2)
AND TABLE_NAME = PARSENAME('''+@TableName+''', 1)
AND DATA_TYPE IN (''char'', ''varchar'', ''nchar'', ''nvarchar'')
')
SELECT @TotalColumnCount=COUNT(*) FROM @ColumnList
WHILE @ColumnIndex<@TotalColumnCount
BEGIN
SET @ColumnIndex=@ColumnIndex+1
SET @ColumnName=''
SELECT @ColumnName=ColumnName FROM @ColumnList WHERE RowNo=@ColumnIndex
DELETE FROM @PrimaryKeyList
INSERT INTO @PrimaryKeyList
EXEC('
SELECT Col.Column_Name from
['+@DatabaseName+'].INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
['+@DatabaseName+'].INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type = ''PRIMARY KEY''
AND Col.Table_Name= '''+@CurrentTableName+'''
')
SELECT @PrimaryKey=''
SELECT @PrimaryKey=PrimaryKeyName FROM @PrimaryKeyList
SET @PrimaryKey=ISNULL(@PrimaryKey,'')
IF @ColumnName IS NOT NULL AND @PrimaryKey<>''
BEGIN
INSERT INTO @Results
EXEC
(
'SELECT '''+@DatabaseName+''','''+@CurrentTableName+''',''' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) , '''+@PrimaryKey+''', [' + @PrimaryKey + ']
FROM ['+@DatabaseName+'].' + @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END
END
SELECT ROW_NUMBER() OVER (ORDER BY DatabaseName) AS Id,DatabaseName,TableName,ColumnName, ColumnValue AS SearchValue,PrimaryKeyValue AS Pk, PrimaryKey FROM @Results
DROP TABLE #tblDatabases