This code doesn't make any sense. What if there's more than one incident_id in the Dipper_incident table? How do you know which one to select? Outside of that, if you are going to use count(*) like that, you will have to GROUP BY incident_id if you want this to work. Since the statement won't work anyway, I would recommend starting over though.
Think about what you're doing. A variable can only hold one value at a time. Here is an example of what your code would do if you got it working:CREATE TABLE Dipper_incident(incident_id INT, col1 INT)INSERT Dipper_incident(incident_id, col1) SELECT 1,1 UNION ALL SELECT 2,2 UNION ALL SELECT 3,3 UNION ALL SELECT 4,4 UNION ALL SELECT 5,5 UNION ALL SELECT 5,6 UNION ALL SELECT 5,7DECLARE @rowcntr INT, @dip_incident_id INT,@dipper_inc_count INTSELECT @dipper_inc_count = COUNT(*), @dip_incident_id = incident_id FROM Dipper_incidentGROUP BY incident_idSELECT @rowcntr = 1WHILE (@rowcntr <= @dipper_inc_count) BEGIN PRINT 'Row Count' + CONVERT(VARCHAR(10),@rowcntr) + CONVERT(VARCHAR(10),@dip_incident_id) SET @rowcntr = @rowcntr + 1 ENDDROP TABLE Dipper_incident
Result Set: Row Count15Row Count25Row Count35
Are you looking for something like this?CREATE TABLE Dipper_incident(incident_id INT, col1 INT)INSERT Dipper_incident(incident_id, col1) SELECT 1,1 UNION ALL SELECT 2,2 UNION ALL SELECT 3,3 UNION ALL SELECT 4,4 UNION ALL SELECT 5,5 UNION ALL SELECT 5,6 UNION ALL SELECT 5,7SELECT 'The row count for incident_id ' + CAST(incident_id AS VARCHAR(55)) + ' = ' + CAST(COUNT(incident_id) AS VARCHAR(55))FROM Dipper_incidentGROUP BY incident_idDROP TABLE Dipper_incident
The result would then be:The row count for incident_id 1 = 1The row count for incident_id 2 = 1The row count for incident_id 3 = 1The row count for incident_id 4 = 1The row count for incident_id 5 = 3
If this is what you are trying to achieve, you should just return a recordset with the incident_id and count. Let the presentation layer add the rest and format.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA.