Monday, 15 June 2015

How can I do branching in a SQL Server Stored Procedure? -



How can I do branching in a SQL Server Stored Procedure? -

i have next table:

create table [dbo].[test] ( [testid] int identity (1, 1) not null, [testtypeid] int not null, [examid] int not null, [teststatusid] int not null, [topicid] int not null );

i using stored procedure created inserting data:

create procedure dbo.sp_ins_test @examid int , @teststatusid int , @testtypeid int , @topicid int begin

how can different action depending on value of @testtypeid ?

what want if testtypeid = 1 update test records have topicid = @topicid , alter teststatusid 2.

if testtypeid = 2 update test records have examid = @examid , alter teststatusid 2.

here have far:

update dbo.test set teststatusid = 2 testtypeid = @testtypeid , title = @title; update dbo.test set teststatusid = 2 examid = @examid , title = @title;

however not sure how select 1 or other update depending on value of testtypeid.

create procedure dbo.sp_ins_test @examid int , @teststatusid int , @testtypeid int , @topicid int begin if @testtypeid = 1 begin update dbo.test set teststatusid = 2 testtypeid = @testtypeid , title = @title; end if @testtypeid = 2 begin update dbo.test set teststatusid = 2 examid = @examid , title = @title; end end

sql-server

No comments:

Post a Comment