This is demo store procedure where GOTO statement can be shown.
ALTER Procedure [dbo].[sp_GetStudentOrTeacherDetails]
(@ID int)
AS
BEGIN
DECLARE @IsStudent bit=0;
DECLARE @IsTeacher bit=0;
DECLARE @ConsideringDetailsTable varchar(50)
DECLARE @ConsideringDetailsTableReferingColumnName varchar(50)
SELECT
@IsStudent = IsStudent,
@IsTeacher = IsTeacher
FROM Person
WHERE Person_ID=@ID
IF @IsStudent = 1
BEGIN
SET @ConsideringDetailsTable='StudentDetails'
SET @ConsideringDetailsTableReferingColumnName='Student_ID'
GOTO GetRecord;
END
IF @IsTeacher = 1
BEGIN
SET @ConsideringDetailsTable='TeacherDetails'
SET @ConsideringDetailsTableReferingColumnName='Teacher_ID'
GOTO GetRecord;
END
GetRecord:
DECLARE @Statement varchar(max)
SET @Statement = 'SELECT * FROM '+@ConsideringDetailsTable +' WHERE '+@ConsideringDetailsTableReferingColumnName+'='+ CAST(@ID as varchar(20))
exec @Statement
No comments:
Post a Comment