Skip to content
Advertisement

Procedure with cursor has missing output

I am working on an SQL Server procedure that I would like to have print the department name and students name Similar to the attached snippet

The formatting should look like the attached sample snippet and there are two columns involved. students and department tables.

The problem is that the results are incomplete/seems to be getting cut off below is the code that I am using

To run the script exec pro_depart11;

I’ve also included a snippet of the tables. students then department tables

The third snippet is of the results I am getting. I am not sure where I am going wrong with the code or how to best fix it, any help is greatly appreciated.

missingresults

Expected results

Below is the DLL/DML codes involved for the table/inserts

Advertisement

Answer

First key point, you don’t need a nested loop, order by department and have a condition for when the name changes.

Returns:

Other points to note:

  • Use good layout and whitespace and you (and others will be able to follow your code easily).
  • Use consistent casing for your code and column names. Mixing it up just makes it hard to read.
  • Don’t prefix column names when they are unique by table e.g. dname, sname – just use Name.
  • Always explicitly list the columns you are inserting into
  • Don’t assume your department ids are sequential… in real life they might not be.
  • You don’t need a group by unless you are aggregating (which you aren’t).
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement