日期:2014-05-17  浏览次数:20371 次

select时,里边的函数依次调用一张表里的每一个值。
本帖最后由 suanleba10 于 2013-09-11 17:07:43 编辑

--table1: #advertisers ( advertiserId bigint)
231
232
234
236
674
684
710
731
732
837
--table2: #accounts (accountId int)
1
2
3
--table: dbo.KeyWords
AccountId Date                        TrackingId
1         2013-07-10 18:09:01.373     xxx
2         2013-07-10 18:09:03.321     yyy
4         2013-07-10 18:09:03.321     zzz
5         2013-07-10 18:09:03.1231    ooo




想实现以下语句, 关键是dbo.ufGetTrackingId函数需要每次调用一个@AdvertiserId,
如果用游标的话,会重复执行断AccountId in( select AccountId from #accounts),
有什么办法,可以在直接select 表 dbo.KeyWords是依次给@AdvertiserId返回#advertisers里的一个AdvertiserId值给函数dbo.ufGetTrackingId

select date, .dbo.ufGetTrackingId(@advertiserId)
from dbo.KeyWords
where AccountId in( select AccountId from #accounts)


------解决方案--------------------
declare @s varchar(100)  -->函数返回什么类型,这里就定义什么类型?
set @s=dbo.ufGetTrackingId(@advertiserId)  -->这个@advertiserId是外部传过来的

select date, @s as xxxx
from dbo.KeyWords a
inner join #accounts b on a.AccountId=b.AccountId