循环使用具有不同值

时间:2015-05-22 16:36:01

标签: sql-server tsql stored-procedures

嘿我有多个BannerID,我需要存储过程中的参数我想循环存储过程,更改作为参数传递的BannerID。这是我的代码,

 create table #a
        (BannerID int,
        BannerName varchar(100)NULL,
        InterActionRate Decimal(5,3) NULL
        )        

Declare @Banker int
Set @Banker = 0

insert into #a(BannerID)
Values (21212),(21577)

WHILE (@Banker > 2)
BEGIN

Insert Into #a(BannerName,InteractionRate)
Exec BannerSummaryReport @BannerID=BannerID,@DateStart = N'05/01/15',@DateEnd = N'05/20/15'
Set @Banker = @Banker +1
END
Select * from #a

我一直收到错误:

消息8114,级别16,状态5,过程BannerSummaryReport,第0行 将数据类型nvarchar转换为int时出错。错误循环,好像它被调用了两次以上,我永远不会执行查询。如果有帮助。任何想法?

编辑:我添加了一个名为@Banker的变量,我将其用作while循环的计数器。这使得循环现在结束了。但是我的存储过程仍然没有填充数据,IE BannerName和InteractionRate在表中仍为空。

有些人已经请求存储过程问题是我无法访问它我可以给你一个输出的例子,

Exec BannerSummaryReport @ BannerID = 21212,@ DateStart = N'05 / 01/15',@ DateEnd = N'05 / 20/15'

我得到一个项目清单

ReportID    BannerName  TagName CompanyName BannerStatusID  BannerID    Impressions FlashImpressions    NoScriptImpressions UniqueViers TotalInterActions   WebCT   ListingCT   ListingClickThrough TotalCT InterActionRate ClickThroughRate    InterActionDiff ClickThroughDiff    ActiveBanners   DataSort1   DataSort2   RollOverCount   RollOverTime    ListingVideoPlayCount   ListingVideoPlayTime    ListingEmailDealer  ListingEmailFriend  ClickSortItem1  ClickSortItem2  ClickSortMenu   ClickLogo   ClickMap    ClickWebSite    DateAdded   PercentagePlayed    TagVideoPlayCount   TagVideoPlayTime    TagVideoTwitter ListingVideoTwitter TagVideoFacebook    ListingVideoFacebook    TagVideoPinterest   ListingVideoPinterest   ListingThumbnail    ListingScroll   TagVideoPlayButtonCount UpdateFilterButtonClick ClickTopSheetExtendImage    AccountID   ModelSelectClick    TrimSelectClick ExteriorColorSelectClick    InteriorColorSelectClick    InventoryViewClick  ShareButtonClick    ZipCodeEntered  MapClickThrough MenuOpen    SummaryClick    Misellaneous    BannerType  VPaidPreRollTime    VPaidPreRollCount   Mids    Ends    CreativeIsTrackable CreativeWasViewable
1   JimmyEllisDemo  All DO NOT TOUCH    1   21212   10932905    906549  0   0   11385   63  13  0   0   0.10414 0.00058 NULL    NULL    0   Make    Model   11291   128193472   14  163846  0   0   1   0   2   0   0   63  2012-01-13  0   7328    48262968    0   0   0   0   0   0   0   0   1   0   0   666 0   0   0   0   0   0   0   0   0   0   0   1   0   0   NULL    NULL    NULL    NULL

1 个答案:

答案 0 :(得分:1)

很明显,您的代码中存在错误。我可以重现错误:

create procedure spTest @i int
as
Select @i as i
go

exec spTest @i = blabla

错误:

  

将数据类型nvarchar转换为int时出错。

现在看看你的代码:

 Exec BannerSummaryReport @BannerID=BannerID,@DateStart = N'05/01/15',@DateEnd = N'05/20/15'

什么是@BannerID=BannerID?您没有将int值传递给存储过程的int参数。您正在传递BannerID字样。

修改

我想你想要遍历#a表中的所有行,并使用从特定行的存储过程返回的数据更新BannerNameInterActionRate列。不幸的是,没有直接的方式。

您需要声明另一个临时表,该表将包含以相同顺序存储proc返回的所有列。即

create table #a
(
 BannerID int,
 BannerName varchar(100) NULL,
 InterActionRate int NULL
)        

insert into #a(BannerID)
Values (21212),(21577)

create table #tmp
(
ReportID
BannerName
TagName
CompanyName
BannerStatusID
BannerID
Impressions
FlashImpressions
NoScriptImpressions
UniqueViers
TotalInterActions
WebCT
ListingCT
ListingClickThrough
TotalCT
InterActionRate
ClickThroughRate
InterActionDiff
ClickThroughDiff
ActiveBanners
DataSort1
DataSort2
RollOverCount
RollOverTime
ListingVideoPlayCount
ListingVideoPlayTime
ListingEmailDealer
ListingEmailFriend
ClickSortItem1
ClickSortItem2
ClickSortMenu
ClickLogo
ClickMap
ClickWebSite
DateAdded
PercentagePlayed
TagVideoPlayCount
TagVideoPlayTime
TagVideoTwitter
ListingVideoTwitter
TagVideoFacebook
ListingVideoFacebook
TagVideoPinterest
ListingVideoPinterest
ListingThumbnail
ListingScroll
TagVideoPlayButtonCount
UpdateFilterButtonClick
ClickTopSheetExtendImage
AccountID
ModelSelectClick
TrimSelectClick
ExteriorColorSelectClick
InteriorColorSelectClick
InventoryViewClick
ShareButtonClick
ZipCodeEntered
MapClickThrough
MenuOpen
SummaryClick
Misellaneous
BannerType
VPaidPreRollTime
VPaidPreRollCount
Mids
Ends
CreativeIsTrackable
CreativeWasViewable
) 

为这些列提供合适的类型。

然后你需要游标来迭代表#a中的行:

Declare @BannerID int

declare cur cursor fast_forward for
select BannerID from #a

open cur

fetch next from cur into @BannerID

while @@FETCH_STATUS = 0
begin

insert into #tmp
exec spTest @BannerID

fetch next from cur into @BannerID
end

close cur
deallocate cur

最后一步是从#a表更新#tmp表:

update a set BannerName = t.BannerName, InterActionRate = t.InterActionRate
from #a a
join #tmp t on a.BannerID = t.BannerID

现在您已在表#a中更新了数据。