Context. The discovery of amino acids in meteorites fallen to Earth and the detection of glycine, the simplest of them, in samples returned from a comet to Earth strongly suggest that the chemistry of the interstellar medium is capable of producing such complex organic molecules and that they may be widespread in our Galaxy. Aims. Our goal is to investigate the degree of chemical complexity that can be reached in the interstellar medium, in particular in dense star-forming regions. Methods. We performed an unbiased, spectral line survey toward Sgr B2(N) and (M), two regions where high-mass stars are formed, with the IRAM 30 m telescope in the 3 mm atmospheric transmission window. Partial surveys at 2 and 1.3 mm were performed in parallel. The spectra were analyzed with a simple radiative transfer model that assumes local thermodynamic equilibrium but takes optical depth effects into account. Results. About 3675 and 945 spectral lines with a peak signal-to-noise ratio higher than 4 are detected at 3 mm toward Sgr B2(N) and (M), i.e. about 102 and 26 lines per GHz, respectively. This represents an increase by about a factor of two over previous surveys of Sgr B2. About 70% and 47% of the lines detected toward Sgr B2(N) and (M) are identified and assigned to 56 and 46 distinct molecules as well as to 66 and 54 less abundant isotopologues of these molecules, respectively. In addition, we report the detection of transitions from 59 and 24 catalog entries corresponding to vibrationally or torsionally excited states of some of these molecules, respectively, up to a vibration energy of 1400 cm −1 (2000 K). Excitation temperatures and column densities were derived for each species but should be used with caution. The rotation temperatures of the detected complex molecules typically range from ∼50 to 200 K. Among the detected molecules, aminoacetonitrile, n-propyl cyanide, and ethyl formate were reported for the first time in space based on this survey, as were five rare isotopologues of vinyl cyanide, cyanoacetylene, and hydrogen cyanide. We also report the detection of transitions from within twelve new vibrationally or torsionally excited states of known molecules. Absorption features produced by diffuse clouds along the line of sight are detected in transitions with low rotation quantum numbers of many simple molecules and are modeled with ∼30-40 velocity components with typical linewidths of ∼3-5 km s −1 . Conclusions. Although the large number of unidentified lines may still allow future identification of new molecules, we expect most of these lines to belong to vibrationally or torsionally excited states or to rare isotopologues of known molecules for which spectroscopic predictions are currently missing. Significant progress in extending the inventory of complex organic molecules in Sgr B2(N) and deriving tighter constraints on their location, origin, and abundance is expected in the near future thanks to an ongoing spectral line survey at 3 mm with ALMA in its cycles 0 and 1. The present single-dish surve...