【问题标题】:Looping through a stored Procedure with different values循环访问具有不同值的存储过程
【发布时间】:2015-05-23 03:23:41
【问题描述】:

嘿,我有多个 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 在表中仍然为空。

由于有些人要求存储过程,但问题是我无法访问它,但我可以给你一个输出示例,

执行 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

【问题讨论】:

  • 我的第一个猜测是 @DateStart 和/或 @DateEnd 被声明为整数参数而不是字符串参数。您的 while 条件看起来永远不会终止,但这是另一回事。
  • 对我来说,您的WHILE 似乎永远不会结束
  • @Gordon Linoff 和 Lamak,嘿,伙计们,我已经编辑了查询,所以它有一个永无止境的循环,感谢解决了一个问题!
  • 这将有助于发布您的存储过程 BannerSummaryReport 的一些请求。
  • 我们仍然不知道使用“Select *”的存储过程的确切输出。尝试类似“选择 BannerName,InteractionRate”。即使您的“插入...”有效,它也会将新行添加到您的临时表#a,而不是更新现有行。

标签: sql-server tsql stored-procedures


【解决方案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 返回的所有列。 IE。

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

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

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

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

【讨论】:

  • 啊,这是有道理的,但是我如何将#a 中的 BannerIDs 传递给存储过程?我尝试使用#a.BannerID,但这给了我'.'附近的语法错误
猜你喜欢
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多