Here's my take with the listings
package. The code below defines a new environment via \lstnewenvironment
that saves the listing in a box and then typesets it in a \marginpar
(or \marginnote
if you like):
\documentclass{article}
\usepackage[marginparwidth=5cm,includemp,showframe]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings,xcolor}
\newbox\marginlst
\newlength\marginlstoffset
% framerule + framesep + rulesep:
\setlength\marginlstoffset{\dimexpr2mm+3pt+2pt\relax}
\lstnewenvironment{marginlisting}
{%
\lstset{
basicstyle = \small\ttfamily,
frame = leftline ,
framerule = 2mm ,
rulecolor = \color{black!20},
breaklines = true ,
linewidth = \dimexpr\marginparwidth-\marginlstoffset\relax
}%
\setbox\marginlst\hbox\bgroup
}
{%
\egroup
% or \marginnote:
\marginpar{\hspace*{\marginlstoffset}\unhbox\marginlst}%
}
\usepackage{lipsum}
\begin{document}
Some initial words
\begin{marginlisting}
BEGIN
FOR k IN c2(o.id) LOOP
dbms_output.put_line('test');
END LOOP;
END.
\end{marginlisting}
\lipsum[2]
\end{document}

Verbatim
. – dwelle May 28 '13 at 12:44