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

sqlselect遍歷

發布時間: 2023-08-29 10:50:28

Ⅰ 如何使用 sql 語句 遍歷 資料庫里的表

---找出這個表中所有類型為varchar型的欄位
---不知道你的str型需要對應哪幾種類型,只以varchar型為例,其他類型可以查找systypes
---將所需要的type添加到最後的type篩選語句中
select t2.name from
(
---找到主鍵為aaa,並且與表中aaa欄位的外鍵相對應的表
select b.id
from
(
---找出表1中aaa外鍵所對應的主表和主鍵
select rkeyid,rkey
from
(
---找出表1中的所有外鍵
select a.id,b.* from sysobjects a inner join sysforeignkeys b
on a.id=b.fkeyid
where xtype='U' and name='表1'
)c
inner join
(
---找出aaa欄位所在的所有表
select id from syscolumns
where name='aaa'
)d
on c.id=d.id
)a
inner join
(
---找出aaa欄位所在的所有表
select id from syscolumns
where name='aaa'
)b
on a.rkeyid=b.id
)t1
inner join syscolumns t2
on t1.id=t2.id
where t2.type=39

回答修改:
根據需求,修改如下
declare @name varchar(255)
---找出主表的名字
set @name=
(select t2.name from
(---找出SampleDetails中的lngYyGlAID外鍵所對應的主表
---有可能SampleDetails中不只有一個外鍵
---如果確定只有一個外鍵,不用和d相交
select rkeyid
from
(
---找出SampleDetails中的所有外鍵
select a.id,b.* from sysobjects a inner join sysforeignkeys b
on a.id=b.fkeyid
where xtype='U' and name='SampleDetails'
)c
inner join
(
---找出lngYyGlAID欄位所在的所有表
select id from syscolumns
where name='lngYyGlAID'
)d
on c.id=d.id)t1
inner join
sysobjects t2
on t1.rkeyid=t2.id)

---從主表中查出strcode欄位的所有信息
declare @sql varchar(255)
set @sql='select strCode from '+@name
exec(@sql)

Ⅱ 在MySql下,怎麼用SQL語句遍歷一個樹結構

f exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [tb]
GO

--示例數據
create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
insert [tb] select 0,'中國'
union all select 0,'美國'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江蘇'
union all select 6,'蘇州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'無錫'
union all select 2,'紐約'
union all select 2,'舊金山'
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_id]') and xtype in (N'FN', N'IF', N'TF'))

drop function [dbo].[f_id]
GO

/*--樹形數據處理

級別及排序欄位

--鄒建 2003-12(引用請保留此信息)--*/

/*--調用示例

--調用函數實現分級顯示

select replicate('-',b.[level]*4)+a.name

from [tb] a,f_id()b

where a.[id]=b.[id]

order by b.sid
--*/
create function f_id()
returns @re table([id] int,[level] int,sid varchar(8000))
as
begin

declare @l int

set @l=0

insert @re select [id],@l,right(10000+[id],4)

from [tb] where [pid]=0

while @@rowcount>0

begin

set @l=@l+1

insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)

from [tb] a,@re b

where a.[pid]=b.[id] and b.[level]=@l-1

end

return
end
go

--調用函數實現分級顯示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
go

--刪除測試
drop table [tb]
drop function f_id
go

/*--結果
中國
----北京
----上海
----江蘇
--------蘇州
------------常熟
--------南京
--------無錫
美國
----紐約
----舊金山
加拿大

--*/

Ⅲ SQL遍歷資料庫,代碼語句解釋

這是一存儲過程,
這過程是為了查到表中的欄位名類似@str的所有表
如果存在,剛輸出select [fieldname] from [tablename]
if exists() --如果結果集不為空
print--則輸出 select [fieldname] from [tablename]
a.xusertype是欄位類型,
具體可以下語句查詢select * from systypes where xtype in (175,239,231,167,56,60,108,106)
a 表為syscolumns 是系統欄位表,存著當前資料庫所有對象的欄位名
b 表為sysobjects 是系統對象表,存著當前資料庫所有的對象(表,視圖,過程,索引,關健字,約束等)
xtype='U'是用戶表
a.status>=0這個條件是沒有用的,MSSQL 系統欄位表的status都是>=0的.

整個過程核心為一個游標cursor
select s=''
from syscolumns a
join sysojbects b on a.id = b.id
where....
open cursor
fetch --讀到@S
while --開始循環
execute @S
fetch
end
釋放 cursor
你的問題是b.name無效? 那是有效的.b-->sysobjects

其實這個可以寫得簡單點不用寫得這么復雜.
可以這么寫:
create procere procSelect (@str varchar(100))
as
select 'select ' + A.Name + ' from ' + B.Name
from syscolumns A
left join sysobjects B on A.id = B.id
where B.xtype = 'u' and A.Name like '%' + @str + '%'
and A.xusertype in (175,239,231,167,56,60,108,106)

Ⅳ sql寫語句如何循環執行10000次

調用循環執行,例如:

declare@nint
set@n=0
begin
while@n<10000
set@n=@n+1
--這里運行您要執行的1萬次操作
--例如您提問中的那些動作查詢

end

Ⅳ sql語句,sql怎麼循環查詢,把一個list中的所有值當做查詢條件,查詢符合這個list的所有的數據

selectf1fromtable1的結果集做為查詢條件循環查詢。
如:
set@a=selectf1fromtable1
foreach(@a)
{
select*fromtable2
wheref2=@a
}

Ⅵ 怎麼用SQL實現日期區間遍歷每天數據

以下可以達到你的要求不?
SQL code
?

SELECT 開始日期
,結束日期
,用戶ID
,工作內容
FROM 基礎數據
,(SELECT TO_DATE('2010/12/01', 'YYYY/MM/DD') + ROWNUM - 1 DAY_DATE FROM DUAL CONNECT BY ROWNUM < 32) DT
WHERE 基礎數據.開始日期 = DT.DAY_DATE

Ⅶ sql 遍歷所有表中 某項 值為已知數的查詢語句

即然經典,那就多加分啦,只要輸出表名:
===============================
ALTER proc Full_Search(@string varchar(50))
as
begin

declare @tbname varchar(50)
declare tbroy cursor for select name from sysobjects
where xtype= 'u ' --第一個游標遍歷所有的表

open tbroy
fetch next from tbroy into @tbname
while @@fetch_status=0
begin

declare @colname varchar(50)
declare colroy cursor for select name from syscolumns
where id=object_id(@tbname) and xtype in (
select xtype from systypes
where name in ( 'varchar ', 'nvarchar ', 'char ', 'nchar ') --數據類型為字元型的欄位
) --第二個游標是第一個游標的嵌套游標,遍歷某個表的所有欄位

open colroy
fetch next from colroy into @colname
while @@fetch_status=0
begin

declare @sql nvarchar(1000),@j int
select @sql= 'select @i=count(1) from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%'''
exec sp_executesql @sql,N'@i int output',@i=@j output --輸出滿足條件表的記錄數
if @j> 0
BEGIN
select 包含字串的表名=@tbname
--exec( 'select distinct '+@colname+' from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''')
END
fetch next from colroy into @colname
end

close colroy
deallocate colroy

fetch next from tbroy into @tbname
end
close tbroy
deallocate tbroy
end
go

exec Full_Search '123'

Ⅷ SQL server中遍歷所有行的循環表達式怎麼寫

你這樣試下:
UPDATE 表1 SET 表1.總數=SUM(表2.數量) FROM 表1, 表2 WHERE CHARINDEX(表2.編號, 表1.編號)=1

表二中所有001開頭的對應的「數量」相加的和
select sum(數量) from 表二 group by left(編號,3)