遍历具有不同值的存储过程
Looping through a stored Procedure with different values
嘿,我有多个 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在table中仍然为空。
由于有些人请求存储过程,问题是我无法访问它,不过我可以给你一个输出示例,
执行 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
很明显你的代码有错误。我可以重现错误:
create procedure spTest @i int
as
Select @i as i
go
exec spTest @i = blabla
错误:
Error converting data type nvarchar to int.
现在看看你的代码:
Exec BannerSummaryReport @BannerID=BannerID,@DateStart = N'05/01/15',@DateEnd = N'05/20/15'
什么是@BannerID=BannerID
?您没有将 int 值传递给存储过程的 int 参数。您正在传递单词 BannerID
.
编辑:
我想您想要遍历 #a
table 中的所有行,并使用从特定行的存储过程返回的数据更新 BannerName
和 InterActionRate
列。不幸的是没有直接的方法。
您需要声明另一个临时 table,它将以相同的顺序包含存储过程 returns 的所有列。 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
)
为这些列指定适当的类型。
然后您将需要游标来遍历 table #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
table:
更新 #a
table
update a set BannerName = t.BannerName, InterActionRate = t.InterActionRate
from #a a
join #tmp t on a.BannerID = t.BannerID
现在您在 table #a
.
中更新了数据
嘿,我有多个 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在table中仍然为空。
由于有些人请求存储过程,问题是我无法访问它,不过我可以给你一个输出示例,
执行 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
很明显你的代码有错误。我可以重现错误:
create procedure spTest @i int
as
Select @i as i
go
exec spTest @i = blabla
错误:
Error converting data type nvarchar to int.
现在看看你的代码:
Exec BannerSummaryReport @BannerID=BannerID,@DateStart = N'05/01/15',@DateEnd = N'05/20/15'
什么是@BannerID=BannerID
?您没有将 int 值传递给存储过程的 int 参数。您正在传递单词 BannerID
.
编辑:
我想您想要遍历 #a
table 中的所有行,并使用从特定行的存储过程返回的数据更新 BannerName
和 InterActionRate
列。不幸的是没有直接的方法。
您需要声明另一个临时 table,它将以相同的顺序包含存储过程 returns 的所有列。 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
)
为这些列指定适当的类型。
然后您将需要游标来遍历 table #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
table:
#a
table
update a set BannerName = t.BannerName, InterActionRate = t.InterActionRate
from #a a
join #tmp t on a.BannerID = t.BannerID
现在您在 table #a
.