sql server - What is the best way to toggle user defined column name or default column name in stored procedure -
what best approach stored procedure toggle between user defined column names or default column name
here have done far. fine small query, there better way of doing larger query.
-- drop stored procedure if exists if exists(select * sys.procedures schema_id = schema_id('dbo') , name = n'sp_test') drop procedure dbo.sp_test go create procedure [dbo].[sp_test] -- /* declare parameters */ @columnname bit =0 begin -- select statement fetch record if(@columnname =1) ( select top 100 im.inc_ref, im.id dbo.test im ) else ( select top 100 im.inc_ref ref, im.id id dbo.test im ) end go -- ============================================ -- execute stored procedure -- ============================================ declare @columnname bit set @columnname =0 exec [dbo].[sp_test] @columnname
thanks in advance
when need different results stored procedure based on parameter, we'll call 1 of 2 "sub" stored procedures. in case, be:
create procedure [dbo].[sp_test] -- /* declare parameters */ @columnname bit = 0 begin if ( @columnname = 1 ) exec dbo.[sp_test1] else exec dbo.[sp_test2] end go create procedure dbo.[sp_test1] begin select top 100 im.inc_ref , im.id dbo.test im end go create procedure dbo.[sp_test2] begin select top 100 im.inc_ref ref , im.id id dbo.test im end go
i've found gets around issue of badly cached plans.
Comments
Post a Comment