Ich habe eine gespeicherte Prozedur geschrieben, die den Tabellenwert angibt. Sie gibt die ID zurück, die eingefügt wurde. Wenn ich es von einer Abfrage in SSMS aus führe, funktioniert es und gibt den Wert korrekt zurück.Abrufen von Rückgabewert von gespeicherter Prozedur
Wenn ich die Prozedur aus Code aufrufen, wird ausgeführt, und fügt die Daten ein, aber nicht den Rückgabeparameter, den ich hinzugefügt und als Rückgabeparameter markiert habe.
Was mache ich hier falsch?
SP:
ALTER PROCEDURE [dbo].[InsertUpdateMeeting]
-- Add the parameters for the stored procedure here
@Meeting MeetingsTableType READONLY,
@Area AreasTableType READONLY,
@MeetingLocation MeetingLocationsTableType READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @MeetingId int = 0;
DECLARE @AreaId int;
DECLARE @MeetingLocationId int;
SELECT TOP 1 @MeetingId=m.MeetingId FROM @Meeting AS m;
SELECT TOP 1 @MeetingLocationId = ml.MeetingLocationId FROM @MeetingLocation AS ml;
SELECT TOP 1 @AreaId = a.AreaId FROM @Area AS a;
UPDATE Areas SET
Hotline=areaParam.Hotline,
Name=areaParam.Name
FROM Areas INNER JOIN @Area AS areaParam
ON Areas.AreaId=areaParam.AreaId;
IF(@@ROWCOUNT=0)
BEGIN;
INSERT INTO Areas
(Hotline,Name)
SELECT areaParam.Hotline,areaParam.Name FROM @Area AS areaParam;
SET @AreaId=SCOPE_IDENTITY();
END;
UPDATE MeetingLocations SET
Address1=ml.Address1,
Address2=ml.Address2,
Address3=ml.Address3,
City=ml.City,
[State]=ml.[State],
Zip=ml.Zip,
ClubName=ml.ClubName,
ClubDescription=ml.ClubDescription,
MapLink=ml.MapLink
FROM MeetingLocations INNER JOIN @MeetingLocation AS ml
ON MeetingLocations.MeetingLocationId=ml.MeetingLocationId;
IF(@@ROWCOUNT=0)
BEGIN;
INSERT INTO MeetingLocations
(Address1,Address2,Address3,City,[State],Zip,ClubName,ClubDescription,MapLink)
SELECT ml.Address1,ml.Address2,ml.Address3,ml.City,ml.[State],ml.Zip,ml.ClubName,ml.ClubDescription,ml.MapLink
FROM @MeetingLocation AS ml;
SET @MeetingLocationId=SCOPE_IDENTITY();
END;
UPDATE Meetings SET
AreaId = @AreaId,
[email protected],
Name = meetingParam.Name
FROM Meetings INNER JOIN @Meeting AS meetingParam
ON Meetings.MeetingId=meetingParam.MeetingId;
IF(@@ROWCOUNT=0)
BEGIN;
INSERT INTO Meetings
(MeetingLocationId,AreaId,Name)
SELECT @MeetingLocationId,@AreaId,meetingParam.Name FROM @Meeting AS meetingParam;
SET @MeetingId=SCOPE_IDENTITY();
END;
RETURN @MeetingId;
END
C# -Code:
...create tables...
var meetingLocationParam = new SqlParameter("@MeetingLocation", meetingLocationTable);
meetingLocationParam.SqlDbType = SqlDbType.Structured;
meetingLocationParam.TypeName = "MeetingLocationsTableType";
var meetingParam = new SqlParameter("@Meeting", meetingTable);
meetingParam.SqlDbType = SqlDbType.Structured;
meetingParam.TypeName = "MeetingsTableType";
var areaParam = new SqlParameter("@Area", areaTable);
areaParam.TypeName = "AreasTableType";
areaParam.SqlDbType = SqlDbType.Structured;
using (var connection = new SqlConnection(conn.ConnectionString))
using (var command = new SqlCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = "EXEC dbo.InsertUpdateMeeting";
command.Parameters.AddRange(new SqlParameter[]
{meetingParam, areaParam, meetingLocationParam, new SqlParameter()
{
Value=0,
Direction=ParameterDirection.ReturnValue,
SqlDbType=SqlDbType.Int,
ParameterName="@MeetingId"
} });
var otherRet = command.ExecuteNonQuery();
//Assert.IsTrue(ret > 0);
}
int retunvalue = (int) Command.Parameters [ "@ MeetingID"] Wert;. wird dies helfen, den Wert nach dem Ausführen des Befehls abzurufen? – Hybridzz
es hat einen Wert von 0. also nein, leider. Ich muss etwas Offensichtliches vermissen, weil das einfach sein sollte. –
Das otherRet ist normalerweise die betroffenen Zeilen und nicht Ihre @MeetingId. Überprüfen Sie einfach Ihre 'command.Parameters [" @ MeetingId "]. Value'. – Zach