इस तरह एक समारोह का उपयोग करें:
CREATE function [dbo].[list_to_table] (@list varchar(4000))
returns @tab table (item varchar(100))
begin
if CHARINDEX(',',@list) = 0 or CHARINDEX(',',@list) is null
begin
insert into @tab (item) values (@list);
return;
end
declare @c_pos int;
declare @n_pos int;
declare @l_pos int;
set @c_pos = 0;
set @n_pos = CHARINDEX(',',@list,@c_pos);
while @n_pos > 0
begin
insert into @tab (item) values (SUBSTRING(@list,@c_pos+1,@n_pos - @c_pos-1));
set @c_pos = @n_pos;
set @l_pos = @n_pos;
set @n_pos = CHARINDEX(',',@list,@c_pos+1);
end;
insert into @tab (item) values (SUBSTRING(@list,@l_pos+1,4000));
return;
end;
जैसे उपयोग करने के बजाय, आप फ़ंक्शन द्वारा दी गई तालिका के साथ एक आंतरिक जुड़ाव बनाते हैं:
select * from table_1 where id in ('a','b','c')
हो जाता है
select * from table_1 a inner join [dbo].[list_to_table] ('a,b,c') b on (a.id = b.item)
एक unindexed 1M रिकॉर्ड तालिका में दूसरे संस्करण में लगभग आधा समय लगा ...
चियर्स