T-SQL build temporary tables, processing strings

Source: Internet
Author: User
Tags insert split
Temporary table | string declare @str1 varchar (MB), @str2 varchar (m), @str3 varchar (MB), @str varchar (500)
Set @str1 = ' AADA,BBBF,CCC, '
Set @str2 = ' DDD,AAAAAA,FDSA '
Set @str3 = ' vvvvvv,aaabbbb '
Set @str = ' Fds,bbbf,eee,ddd,fff,hhhfg,dddde '
Right now:
Split the @str1 into AADA,BBBF,CCC three elements, where BBBF appeared in @str.
Split the @str2 into DDD,AAAAAA,FDSA three elements, in which DDD also appeared in @str.
Split the @str3 into VVVVVV,AAABBBB two elements, but no elements appeared in @str.
How can I use the program to achieve such a result
Actually, the real problem is this.
I have a data table with a field in it ClassID
One of the records of the data, such as: AADA,BBBF,CCC
Now to get such a result set, the ClassID will be split in accordance with the above split, the data obtained, in the @str (@str is a parameter from the outside) find out, how can I do?
---------------------------------------------------------------

DECLARE @str varchar (500)

Set @str = ' Fds,bbbf,eee,ddd,fff,hhhfg,dddde '

CREATE table your table (ClassID varchar (100))
Insert your table values (' AADA,BBBF,CCC ')
Insert your table values (' DDD,AAAAAA,FDSA ')
Insert your table values (' VVVVVV,AAABBBB ')

CREATE TABLE #临时表 (a varchar (100))
DECLARE @aaa varchar (8000)
Set @aaa = ' Insert #临时表 SELECT * FROM (SELECT ' "' (@str, ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ') + ' "" "A"
EXEC (@aaa)

SELECT * from your table where exists (select 1 from #临时表 where ', ' + your table. classid+ ', ' like '%, ' +a+ ',% ')
Go

drop table your table, #临时表
---------------------------------------------------------------

CREATE TABLE #t (
ID varchar (10))
DECLARE @str varchar (300)
Set @str = ' Fds,bbbf,eee,ddd,fff,hhhfg,dddde '
DECLARE @i int
DECLARE @len int
Set @i = 1
Set @str = ' Fds,bbbf,eee,ddd,fff,hhhfg,dddde ' + ', '
While @i < Len (@str)
Begin
Insert #t Select substring (@str, @i,charindex (",", @str, @i)-@i)
Set @i = charindex (', ', @str, @i) +1
End
SELECT * FROM datasheet A, #t B where CHARINDEX (b.id, A.classid) > 0


---------------------------------------------------------------

CREATE TABLE T (ClassID varchar (100))
Insert T values (' AADA,BBBF,CCC ')
Insert T values (' DDD,AAAAAA,FDSA ')
Insert T values (' VVVVVV,AAABBBB ')




DECLARE @str varchar (500)

Set @str = ' Fds,bbbf,eee,ddd,fff,hhhfg,dddde '

Select top Identity (int,1,1) as ID into #t from sysobjects

Select distinct a.* from T A,
(Select substring (', ' +string+ ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', id+1)-id-1) As String
From (select @str As String) B, #t C
where substring (', ' +string+ ', ', ', id,8000) like ', _% ') D
where CHARINDEX (', ' +string+ ', ', ', ', ', ', ', ', ' ' +classid+ ', ', ') >0

drop table #t

Put the #t into a fixed table, faster.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.