I'm making a mess of the condition statement while calling a stored procedure from within a stored procedure. Can anyone help me out?
CREATE PROCEDURE [dbo].[sp_Leaderboard]
@CompetitionId INTEGER
AS
DECLARE @Competition TABLE
(CompId INTEGER,
CompFormat NVARCHAR(10)
)
INSERT INTO @Competition
SELECT CompetitionId, CompetitionFormatType
FROM dbo.Competitions
LEFT JOIN dbo.CompetitionFormat ON dbo.Competitions.CompetitionFormatId = dbo.CompetitionFormat.CompetitionFormatId
WHERE CompetitionId = @CompetitionId
CASE WHEN @Competition.CompFormat = "Strokes" THEN EXEC [dbo].[sp_Strokes] ELSE EXEC [dbo].[sp_Stableford]
In addition something I know I have not catered for in my code is that I need to also take the parameter @CompetitionId into which ever procedure I go to.
Why not just use an IF
statement?
IF (SELECT TOP 1 CompFormat FROM @Competition) = 'Strokes'
BEGIN
EXEC [dbo].[sp_Strokes] @CompetitionId
END
ELSE
BEGIN
EXEC [dbo].[sp_Stableford] @CompetitionId
END
You can do what you want and eliminate the table variable at the same time. Try this:
DECLARE @CompFormat varchar(50)
SELECT @CompFormat = CompetitionFormatType
FROM dbo.Competitions
LEFT JOIN dbo.CompetitionFormat
ON dbo.Competitions.CompetitionFormatId = dbo.CompetitionFormat.CompetitionFormatId
WHERE CompetitionId = @CompetitionId
IF @CompFormat = 'Strokes'
BEGIN
EXEC [dbo].[sp_Strokes] @CompetitionId
END
ELSE
BEGIN
EXEC [dbo].[sp_Stableford] @CompetitionId
END
Or perhaps you should create a new stored procedure that takes the @Competition.CompFormat
as a parameter, and then inside it, use an IF-statement to decide from which table to query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.