Reputation: 75
I'm trying to create a stored proc for the first time, but I just can't get it right. So I have a proc which is returning a count from the db, based on the parameters being fed to it. I'm not sure what I'm doing wrong, if the SQL is wrong or if I'm mapping the function incorrectly. Here's my SQL:
USE [AuditTracker_Codegen]
GO
/****** Object: StoredProcedure [dbo].[GetAllFindingsCount] Script Date: 05/12/2016 08:43:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetAllFindingsCount](@ReportId int, @Priority int, @IsDeleted bit)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT COUNT(*)
from [dbo].[App_Finding] finding
WHERE finding.ReportId = @ReportId and finding.Priority = @Priority and finding.IsDeleted = @IsDeleted
END
First off, does this SQL query look right? I've executed it in SSMS and it seems to return the correct result.
I then go to VS, update model from database, add the stored procedure. Then I go to Function Imports in Model Browser, right-click, Add Function Import. Select the stored proc from the dropdown list, click "Get Column Information", then press "Create New Complex Type". Give the thing a name, then press OK. As far as I know, that should be it, right? Or am I wrong?
In my code I then try
var context = new AuditTrackerDatabaseContext();
var findingsOne = context.GetAllFindingsCount_Result(report.ReportId,
(int) PriorityStatus.One, false).ToString();
And that's where I get the error above. When I check mapping details on the edmx for this proc, it says
Maps to GetAllFindingsCount Column
Column1 : Int32 <- Column1
Is anyone able to advise me on what I'm doing wrong here, why it's not working?
Upvotes: 0
Views: 1136
Reputation: 24280
You probably need to give the count value a name, like this:
SELECT COUNT(*) AS RESULT_OF_COUNT
from ...
Upvotes: 1