當前位置:首頁 » 編程語言 » sql遍歷數組
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql遍歷數組

發布時間: 2022-05-29 21:36:19

sql server 查詢表 in一個數組

如果是一維數組:$nams=implode(',', ArrarLIst);
select * from table1 where name in ($names);

//注意字元查詢需要前後帶引號,數字不用

如果是多維數組:得遍歷ArrarLIst數組,取出相應的name再串聯起來

⑵ 遍歷數組存入資料庫問題

你要使用循環
foreach函數返回數組的元素里的一個指針,只指向後面一個元素的值,所以採用循環可以遍歷出數組里的所有元素

⑶ sql 遍歷數組特殊符號生成行

如果你是sql server 2005就可以這么寫:
select A.[總號],B.content from (
SELECT *,[value]=CONVERT(XML,'<v>'+REPLACE([內容],'.','</v><v>')+'</v>')
from test
) A
OUTER APPLY (
SELECT id=N.v.value('.','varchar(100)')
FROM A.[value].nodes('/v') N(v)
) B
這其它的我就不知道了。

⑷ T-SQL中怎麼聲名數組

T-SQL對字元串的處理能力比較弱,比如要循環遍歷象1,2,3,4,5這樣的字元串,如果用數組的話,遍歷很簡單,但是T-SQL不支持數組,所以處理下來比較麻煩。下邊的函數,實現了象數組一樣去處理字元串。

一、按指定符號分割字元串,返回分割後的元素個數,方法很簡單,就是看字元串中存在多少個分隔符號,然後再加一,就是要求的結果。

Create function Get_StrArrayLength
(
@str varchar(1024), --要分割的字元串
@split varchar(10) --分隔符號
)
returns int
as
begin
declare @location int
declare @start int
declare @length int

set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location<>0
begin
set @start=@location+1
set @location=charindex(@split,@str,@start)
set @length=@length+1
end
return @length
end

調用示例:select dbo.Get_StrArrayLength('78,1,2,3',',')
返回值:4

二、按指定符號分割字元串,返回分割後指定索引的第幾個元素,象數組一樣方便

Create function Get_StrArrayStrOfIndex
(
@str varchar(1024), --要分割的字元串
@split varchar(10), --分隔符號
@index int --取第幾個元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int

set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)

set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
--這兒存在兩種情況:1、字元串不存在分隔符號 2、字元串中存在分隔符號,跳出while循環後,@location為0,那默認為字元串後邊有一個分隔符號.

return substring(@str,@start,@location-@start)
end
調用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
返回值:9

三、結合上邊兩個函數,象數組一樣遍歷字元串中的元素

declare @str varchar(50)
set @str='1,2,3,4,5'
declare @next int
set @next=1
while @next<=dbo.Get_StrArrayLength(@str,',')
begin
print dbo.Get_StrArrayStrOfIndex(@str,',',@next)
set @next=@next+1
end

⑸ java,sql語句刪除,條件是一個數組,如何一條sql語句寫出來

遍歷方式的執行sql
遍歷數據,每一個數組元素刪除一次

⑹ 如何遍歷由createSQLquery 進行聯合查詢得到的結果

需要遍歷每條查詢結果來進行某些操作,例如拆分查詢結果的字元串就要通過循環來進行,以下給出了一種循環遍歷的例子供大家參考。

假設表TblTest有兩個欄位:id, value, 而value的值在查到後需要拆分,如:'aa,bb,cc',就可利用以下循環來進行(拆分的例子可以參考另一篇文章:Sql Server中如何拆分字元串)。

/* tmp table that store the flag to indicate if this record is processed */
declare @TblTest_tmp table(
id decimal(18,0),
[flag] int
)

/* total count for the query result */
declare @totalcount int
declare @rownum int

select @totalcount = count(1) from TblTest

set @rownum = 1

while @rownum <= @totalcount
begin
declare @id decimal(18,0),
@value varchar(2000)

select top 1 @id=[id], @value=[value] from TblTest where flag=0

/* do sth for @value, e.g. select * from split(@value, ',') */

update @TblTest_tmp set flag = 1 where [id] = @id
set @rownum = @rownum + 1
end

⑺ java.sql.Array如何遍歷

用getArray()轉化成數組,OK!!!

⑻ 如何在SQL語句中使用循環和數組

sql中沒有數組吧,至少目前為止我不知道有這玩意兒。。。

循環語法如下:

declare@nextint
set@next=1
while@next<10
begin
--循環中的邏輯
@next=@next+1
end

⑼ 在SQL中如何從數組中獲取值再進行查詢

----首先定義一個split函數,其作用是將字元串拆分成表
CREATEFUNCTION[fn_split]
(@SourceSqlvarchar(8000),@StrSepratevarchar(10))
RETURNS@temptable
(
[n]intNULL,
[a]varchar(100)NULL
)
AS
BEGIN
declare@iint,@nint;
set@n=0;
set@SourceSql=rtrim(ltrim(@SourceSql));
set@i=charindex(@StrSeprate,@SourceSql);
while(@i>=1)
begin
set@n=@n+1;
insert@temp([n],[a])values(@n,left(@SourceSql,@i-1));
set@SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i);
set@i=charindex(@StrSeprate,@SourceSql);
end
if(@SourceSql<>'')
begin
set@n=@n+1;
insert@temp([n],[a])values(@n,@SourceSql);
end
return
END
GO

--接下來利用這個函數將數組轉化成表,查出A的對應值
declare@Cvarchar(100),@Dvarchar(100);
set@C='a1,a2,a3,a4,a5,a6';
set@D='b1,b2,b3,b4,b5,b6';
declare@Avarchar(10),@Bvarchar(10);
set@A='a4';
select@B=t2.afromfn_split(@C,',')t1,fn_split(@D,',')t2wheret1.n=t2.nandt1.a=@A;
select@B;
--這里將得到@B=b4

--接下來就可以使用@B了
select TOP 7 * from Data_Content where title = @B order BY ID DESC