sql - CASE vs IF-ELSE-IF vs GOTO keyword -
i have rules in application , have written business logic of rules in procedure. @ time of creation of procedure came know case
statement won't work in scenario. have tried 2 ways perform same operations (using if-else-if
or goto
) shown below.
method 1 using if-else-if conditions:
declare @v_ruleid smallint; if (@v_ruleid = 1) begin /*my business logic*/ end else if (@v_ruleid = 2) begin /*my business logic*/ end else if (@v_ruleid = 3) begin /*my business logic*/ end /* ... ... ... ...*/ else if (@v_ruleid = 19) begin /*my business logic*/ end else if (@v_ruleid = 20) begin /*my business logic*/ end
method 2 using goto statement:
declare @v_ruleid smallint, @v_temp varchar(100); set @v_temp = 'goto rule' + convert(varchar, @v_ruleid); execute sp_executesql @v_temp; rule1: begin /*my business logic*/ end rule2: begin /*my business logic*/ end rule3: begin /*my business logic*/ end /* ... ... ... ...*/ rule19: begin /*my business logic*/ end rule20: begin /*my business logic*/ end
today have 20 rules. can increase number in future. if can able use case statement have not problem performance, can't worried performance of procedure.
also 1 thing noticed procedure execute application.
my question is:
is there way use case statement in procedure? if not, method best use in procedure improve performance of code?
try 1 -
declare @v_ruleid smallint select @v_ruleid = 1; declare @temp table ( ruleid int , query nvarchar(max) ) insert @temp (ruleid, query) values (1, 'exec dbo.usp_test1;'), (2, 'create table dbo.table1 (id int);'), (3, 'drop table dbo.table1;') declare @sql nvarchar(max) select @sql = query @temp ruleid = @v_ruleid print @sql exec sys.sp_executesql @sql
Comments
Post a Comment