I am trying to create a stored procedure to replace the following code:
' -- Show Vehicle Options --
Sub VehicleOptions1(strSelected, strFieldID, strFieldName, strVehicleFieldName, strTable, intSpace)
' -- Create Recordset --
strSQL = "SELECT " & strFieldID & ", " & strFieldName & " FROM " & strTable & " ORDER BY " & strFieldName
Set objRS = siteConn.Execute(strSQL)
If Not objRS.EOF Then
response.write Space(intSpace) & "<ul>" & vbCrLf
Do While Not objRS.EOF
response.write Space(intSpace + 2) & "<li><input type=""checkbox"" class=""checkbox"" name=""" & strVehicleFieldName & """ value=""" & objRS(strFieldID) & """"
If Not CheckBlank(strSelected) Then
arrValues = CreateArray(strSelected)
If IsArray(arrValues) Then
For x = 0 To UBound(arrValues)
If CStr(arrValues(x)) = CStr(objRS(strFieldID)) Then
response.write " checked"
Exit For
End If
Next
End If
End If
response.write ">" & objRS(strFieldName) & "<span>(" & VehicleCount(strVehicleFieldName, objRS(strFieldID), "", "", "") & ")</span></li>" & vbCrLf
objRS.MoveNext
Loop
response.write Space(intSpace) & "</ul>" & vbCrLf
End If
objRS.Close
Set objRS = Nothing
End Sub
Within this code, I am creating a dynamic recordset to create a select menu for different table ID's and names. In addition, I am calling another function that performs a COUNT function for that ID in my tbl_Vehicles table. This is running VERY slow. I want to grab the ID, name, and COUNT in ONE query by creating a stored procedure. Here is what I created:
CREATE PROCEDURE [dbo].[sp_VehicleOptions]
@strFieldID varchar(50) = NULL,
@strFieldName varchar(100) = NULL,
@strTable varchar(100) = NULL
AS
DECLARE @strSQL varchar(max)
BEGIN
SET NOCOUNT ON;
-- Create Recordset --
SET @strSQL = @strSQL + 'SELECT ' + @strFieldID + ', ' + @strFieldName + ', '
SET @strSQL = @strSQL + '(SELECT COUNT(' + @strFieldID + ') FROM tbl_Vehicles WHERE ' + @strFieldName + ' = ' + @strFieldID + ') As VehicleCount '
SET @strSQL = @strSQL + 'FROM ' + @strTable + ' ORDER BY ' + @strFieldName
EXECUTE @strSQL
END
For whatever reason, it is not returning any rows. Any thoughts?