存储过程老报名为 'myCursor' 的游标不存在、对象名 '#Temgps00520130808' 无效,这我给的入参是2013-8-8
ALTER PROCEDURE [dbo].[P_GetBusLocation]
@Date1 datetime
AS
BEGIN
SET NOCOUNT ON;
declare @VehicleNo nvarchar(50),@VGroupID int,@RouteNumber varchar(200),@TableName varchar(50),@VehicleID bigint ,
@OldVehicleID bigint,@SqlStr1 varchar(1024),@SqlStr2 varchar(1024),@TemTableName varchar(50),@SqlStr3 varchar(1024)
set @TableName = 'gps005' + CONVERT(varchar(20), @Date1, 112)
set @TemTableName = '#Tem' + @TableName
set @SqlStr1 = 'create Table ' + @TemTableName+'(GpsDateTime datetime,VehicleID bigint,VehicleNo nvarchar(50),RouteNumber varchar(200),Flag tinyint
,Longitude decimal(9,6),Latitude decimal(8,6),Speed decimal(4,1),Direction smallint,Mileage bigint
,Position varchar(200),AvgSpeed decimal(4,1),StorageTime datetime)'
exec(@SqlStr1)
set @SqlStr2 = 'insert into '+@TemTableName+' select t1.GpsDateTime,t1.VehicleID,null,null,t1.Flag
,t1.Longitude,t1.Latitude,t1.Speed,t1.Direction,t1.Mileage,t1.Position,t1.AvgSpeed,t1.StorageTime from '+@TableName +' as t1'
exec(@SqlStr2)
set @OldVehicleID = 0
set @SqlStr3 = 'declare myCursor cursor FORWARD_ONLY STATIC for select VehicleID from '+@TemTableName
exec(@SqlStr3)
open myCursor
fetch next from myCursor into @VehicleID
while(@@FETCH_STATUS = 0)
begin
if(@VehicleID != @OldVehicleID)
begin
select @VGroupID = VGroupID,@VehicleNo = VehicleLic from VehicleInfo where VehicleID = @VehicleID
select @RouteNumber = RouteNumber FROM RouteInfo where VGroupID = @VGroupID
exec('update '+@TemTableName+' set VehicleNo = '+@VehicleNo+',RouteNumber = '+@RouteNumber+' where VehicleID = '+@VehicleID)
set @OldVehicleID = @VehicleID
end
fetch next from myCursor into @VehicleID
end
exec('select * from '+ @TemTableName)
close myCursor
deallocate myCursor
END
执行:
DECLARE@return_value int
EXEC@return_value = [dbo].[P_GetBusLocation]
@Date1 = N'2013-8-8'
SELECT'Return Value' = @return_value
GO
报错:
消息 208,级别 16,状态 0,第 1 行
对象名 '#Temgps00520130808' 无效。
消息 208,级别 16,状态 0,第 1 行
对象名 '#Temgps00520130808' 无效。
消息 16916,级别 16,状态 1,过程 P_GetBusLocation,第 35 行
名为 'myCursor' 的游标不存在。
消息 16916,级别 16,状态 1,过程 P_GetBusLocation,第 38 行
名为 'myCursor' 的游标不存在。
消息 28102,级别 16,状态 1,过程 P_GetBusLocation,第 39 行
批处理执行由于调试器请求而终止。
存储 cursor
[解决办法]
你都没有output参数,你不可那么执行
直接 :
EXEC dbo].[P_GetBusLocation] '2013-8-8'
你要有
SELECT 'Return Value' = @return_value
必须在创建时
ALTER PROCEDURE [dbo].[P_GetBusLocation]
@Date1 datetime,@xxxx int out put
AS
......
......
go
例如:
CREATE PROC TEST2
@PA1 INT,
@PA2 INT,
@PA3 INT OUTPUT
AS
SET @PA3 = @PA1 + @PA2
GO
DECLARE @PA4 INT
EXEC TEST2 2,4,@PA4 OUTPUT
SELECT @PA4
ALTER PROCEDURE [dbo].[P_GetBusLocation] @Date1 DATETIME
AS
BEGIN
SET NOCOUNT ON;
DECLARE @VehicleNo NVARCHAR(50) ,
@VGroupID INT ,
@RouteNumber VARCHAR(200) ,
@TableName VARCHAR(50) ,
@VehicleID BIGINT ,
@OldVehicleID BIGINT ,
@SqlStr1 VARCHAR(1024) ,
@SqlStr2 VARCHAR(1024) ,
@TemTableName VARCHAR(50) ,
@SqlStr3 VARCHAR(1024)
SET @TableName = 'gps005' + CONVERT(VARCHAR(20), @Date1, 112)--解决方案:由于临时表作用域只针对当前的存储过程.所以直接创建一个固定表名的临时表即可.
create TABLE #Tempgps005
(GpsDateTime datetime,VehicleID bigint,VehicleNo nvarchar(50),RouteNumber varchar(200),Flag tinyint
,Longitude decimal(9,6),Latitude decimal(8,6),Speed decimal(4,1),Direction smallint,Mileage bigint
,Position varchar(200),AvgSpeed decimal(4,1),StorageTime datetime)
SET @TemTableName = '#Tempgps005'
/*SET @SqlStr1 = 'create Table ' + @TemTableName
+ '(GpsDateTime datetime,VehicleID bigint,VehicleNo nvarchar(50),RouteNumber varchar(200),Flag tinyint
,Longitude decimal(9,6),Latitude decimal(8,6),Speed decimal(4,1),Direction smallint,Mileage bigint
,Position varchar(200),AvgSpeed decimal(4,1),StorageTime datetime)'
EXEC(@SqlStr1)*/ --#1.根据@TemTableName变量,用EXEC创建的临时表,作用域只在exec()中可见,外面不可见,所以报错
SET @SqlStr2 = 'insert into ' + @TemTableName--不可见,所以报错.如果用全局临时表,可见.但要考虑并发问题。
+ ' select t1.GpsDateTime,t1.VehicleID,null,null,t1.Flag
,t1.Longitude,t1.Latitude,t1.Speed,t1.Direction,t1.Mileage,t1.Position,t1.AvgSpeed,t1.StorageTime from '
+ @TableName + ' as t1'
EXEC(@SqlStr2) --不可见
SET @OldVehicleID = 0
declare myCursor cursor FORWARD_ONLY STATIC for select VehicleID from #Tempgps005--这儿也不可见
OPEN myCursor
FETCH NEXT FROM myCursor INTO @VehicleID
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
IF ( @VehicleID != @OldVehicleID )
BEGIN
SELECT @VGroupID = VGroupID ,
@VehicleNo = VehicleLic
FROM VehicleInfo
WHERE VehicleID = @VehicleID
SELECT @RouteNumber = RouteNumber
FROM RouteInfo
WHERE VGroupID = @VGroupID
EXEC('update '+@TemTableName+' set VehicleNo = '+@VehicleNo+',RouteNumber = '+@RouteNumber+' where VehicleID = '+@VehicleID)
SET @OldVehicleID = @VehicleID
END
FETCH NEXT FROM myCursor INTO @VehicleID
END
EXEC('select * from '+ @TemTableName) --不可见
CLOSE myCursor
DEALLOCATE myCursor
END
go
--执行:
DECLARE @return_value INT
EXEC @return_value = [dbo].[P_GetBusLocation] @Date1 = N'2013-8-8'
SELECT 'Return Value' = @return_value
GO
set @TableName = 'gps005' + CONVERT(varchar(20), @Date1, 112)
-- 这个临时表的名称,可以随便定义一个
create Table #TemTableName
(
GpsDateTime datetime,VehicleID bigint,VehicleNo nvarchar(50),
RouteNumber varchar(200),Flag tinyint
,Longitude decimal(9,6),Latitude decimal(8,6),
Speed decimal(4,1),Direction smallint,Mileage bigint
,Position varchar(200),AvgSpeed decimal(4,1),
StorageTime datetime
)
insert into #TemTableName
exec('select t1.GpsDateTime,t1.VehicleID,null,null,t1.Flag
,t1.Longitude,t1.Latitude,t1.Speed,t1.Direction,t1.Mileage,
t1.Position,t1.AvgSpeed,t1.StorageTime from ' + @TableName +' as t1')