Assuming that file2 is also a pipe-separated file and you wish to match the first fields of the 2 files (i.e., 1 will match only 1 and not 12,132,etc.), try:
awk 'FNR==NR{a[$1];next}$1 in a' FS=\| file1 file2 > out.txt
@pamu
You don't need to open out.txt multiple times in the loop. Just keep it open for the duration of the loop. That'll be more efficient.
while IFS='|' read x y
do
grep "^$x|" file2
done < file1 > out.txt