Select statement problem
hi,
i have table userinfo column name
name
kalees
mano
ashok
i pass query input 'kalees','mano','babu'
i need output babu
i need select data input value except userinfo table.
try tis;
declare @vtable table (name varchar(20)) declare @vqueryinput varchar(max) insert into @vtable select 'kalees' union all select 'mano' union all select 'ashok' set @vqueryinput = 'kalees,mano,babu' select [value] from dbo.fnsplit(@vqueryinput,',') except select name from @vtable
if passing comma separated values, correct way 'kalees,mano,xyz'. check @vqueryinput variable in above script.
if dont have dbo.fnsplit, here code; comma separated value splitter function helpful in development.
create function [dbo].[fnsplit] (@pstring nvarchar(max),@psplitchar char(1)) returns @tbltemp table (tid int,value varchar(1000)) as begin declare @vstartposition int declare @vsplitposition int declare @vsplitvalue varchar(1000) declare @vcounter int set @vcounter=1 select @vstartposition = 1,@vsplitposition=0 set @vsplitposition = charindex( @psplitchar , @pstring , @vstartposition ) if (@vsplitposition=0 and len(@pstring) != 0) begin insert into @tbltemp ( tid , value ) values ( 1 , @pstring ) return --------------------------------------------------------------->> end set @pstring=@pstring+@psplitchar while (@vsplitposition > 0 ) begin set @vsplitvalue = substring( @pstring , @vstartposition , @vsplitposition - @vstartposition ) set @vsplitvalue = ltrim(rtrim(@vsplitvalue)) insert into @tbltemp ( tid , value ) values ( @vcounter , @vsplitvalue ) set @vcounter=@vcounter+1 set @vstartposition = @vsplitposition + 1 set @vsplitposition = charindex( @psplitchar , @pstring , @vstartposition ) end return end
please visit blog easy , used t-sql scripts
SQL Server > Transact-SQL
Comments
Post a Comment