sqlserver-ado.net 执行存储过程中使用临时表出错

问题描述

ado.net 执行存储过程中使用临时表出错 1C
ado.net执行存储过程提示找不到该临时表,但是在sql中直接执行就没问题,求教各位大神。
sql所在环境windows azure

  if (exists (select * from sys.objects where name = 'proc_Activity_StudioList'))    drop proc proc_Activity_StudioListgocreate proc proc_Activity_StudioList@province int@type int@collegeId int@index int@size int as  IF EXISTS  (SELECT  * FROM dbo.SysObjects WHERE ID = object_id(N'#StudioList') AND OBJECTPROPERTY(ID 'IsTable') = 1)         drop table #StudioList    declare @page int@pageend int @sql nvarchar(4000)    set @page =(@index - 1) * @size + 1    set @pageend = @index*@size      set @sql = 'create table #StudioList(num floatPId intUserNickName nvarchar(500)UserAvatar nvarchar(500)UpCount intCommentCount intRegisterCount int    Userid  bigintRegion nvarchar(100)Province nvarchar(100))     INSERT  INTO #StudioList         select distinct  num=( a.[RegisterCount]*0.5 + a.[UpCount]*0.25 + a.[CommentCount]*0.25 ) a.[PId]temp.[UserNickName]temp.[UserAvatar]a.UpCounta.CommentCounta.RegisterCounttemp.[Userid]r.[CityName] as Regionr1.[CityName] as Province from [UserInfo] as temp    inner join [dbo].ActivityStudioSet as a on a.userid = temp.[UserId]     left join [dbo].[Region] as r on temp.[Region] = r.[Codeid]    left join [dbo].[Region] as r1 on temp.[Province] = r1.[Codeid]    where  temp.[UserRole]=3  '    if(@province > 0)        set @sql = @sql +' and  temp.[Province] =   '+cast(@province as varchar(10))    if(@collegeId > 0)        set @sql = @sql +' and  temp.CollegeId =   '+cast(@collegeId as varchar(10))    if(@type=1)        set @sql = @sql +' and a.[IsRecommend] =1 '    --print @sql    exec(@sql)    set @sql ='select * from  ( select  ROW_NUMBER() over(order by [Userid] ) as rownumber * from #StudioList ) as a'    if(@type = 1)        set @sql = @sql + '  where a.rowNumber between '+cast(@page as varchar(10))+' and '+cast(@pageend as varchar(10))     else if(@type = 2)        set @sql = @sql + ' where a.rowNumber between '+cast(@page as varchar(10))+' and '+cast(@pageend as varchar(10))+' order by a.[RegisterCount] desc '    else         set @sql = @sql + ' where a.rowNumber between '+cast(@page as varchar(10))+' and '+cast(@pageend as varchar(10))+' order by num desc '--print @sqlexec( @sql)IF EXISTS  (SELECT  * FROM dbo.SysObjects WHERE ID = object_id(N'#StudioList') AND OBJECTPROPERTY(ID 'IsTable') = 1)         drop table #StudioList  goexec proc_Activity_StudioList @province=0@type=1@collegeId=0@index=1@size=30

解决方案

Exception=Metis.MobileServer.MetisException.Exceptions.MetisDbException: Type:Error
Description: SQL 执行异常!
InnerException: Type: System.Data.SqlClient.SqlException (InnerException 1)
Message: Invalid object name '#StudioList'.
Source: .Net SqlClient Data Provider
HelpLink:
Data: System.Collections.ListDictionaryInternal
StackTrace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception Boolean breakConnection Action1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception Boolean breakConnection Action
1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj Boolean callerHasConnectionLock Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior SqlCommand cmdHandler SqlDataReader dataStream BulkCopySimpleResultSet bulkCopyHandler TdsParserStateObject stateObj Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds RunBehavior runBehavior String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior RunBehavior runBehavior Boolean returnStream Boolean async Int32 timeout Task& task Boolean asyncWrite SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior RunBehavior runBehavior Boolean returnStream String method TaskCompletionSource1 completion Int32 timeout Task& task Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior RunBehavior runBehavior Boolean returnStream String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at Dapper.SqlMapper.<QueryImpl>d__11
1.MoveNext()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToListTSource
at Dapper.SqlMapper.QueryT
at Metis.MobileServer.DataAccess.BaseDal.QueryListByProcT
TargetSite: Void OnError(System.Data.SqlClient.SqlException Boolean System.Action`1[System.Action])

Message: QueryListByProc :User=>proc_Activity_StudioList=>{""province"":0type"":1collegeId"":0index"":1size"":30}

解决方案二:
你用的是azure sql还是azure虚拟机中的sql server?

解决方案三:
你好 Shrap2011,

你的问题可能是因为局部临时表(以#开头)的作用域问题引起的,修改了一下你的存储过程的代码,应该没问题了。

--if (exists (select * from sys.objects where name = 'proc_Activity_StudioList'))if object_id('proc_Activity_StudioList''P') is not null    drop proc proc_Activity_StudioListgocreate proc proc_Activity_StudioList@province int@type int@collegeId int@index int@size int as -- IF EXISTS  (SELECT  * FROM dbo.SysObjects WHERE ID = object_id(N'#StudioList') AND OBJECTPROPERTY(ID 'IsTable') = 1)  IF object_id(N'tempdb..#StudioList''U') IS NOT NULL        drop table #StudioList    declare @page int@pageend int @sql nvarchar(4000)    set @page =(@index - 1) * @size + 1    set @pageend = @index*@size    create table #StudioList(num floatPId intUserNickName nvarchar(500)UserAvatar nvarchar(500)UpCount intCommentCount intRegisterCount int    Userid  bigintRegion nvarchar(100)Province nvarchar(100))    SELECT * FROM #StudioList      set @sql = 'INSERT  INTO #StudioList         select distinct  num=( a.[RegisterCount]*0.5 + a.[UpCount]*0.25 + a.[CommentCount]*0.25 ) a.[PId]temp.[UserNickName]temp.[UserAvatar]a.UpCounta.CommentCounta.RegisterCounttemp.[Userid]r.[CityName] as Regionr1.[CityName] as Province from [UserInfo] as temp    inner join [dbo].ActivityStudioSet as a on a.userid = temp.[UserId]     left join [dbo].[Region] as r on temp.[Region] = r.[Codeid]    left join [dbo].[Region] as r1 on temp.[Province] = r1.[Codeid]    where  temp.[UserRole]=3  '    if(@province > 0)        set @sql = @sql +' and  temp.[Province] =   '+cast(@province as varchar(10))    if(@collegeId > 0)        set @sql = @sql +' and  temp.CollegeId =   '+cast(@collegeId as varchar(10))    if(@type=1)        set @sql = @sql +' and a.[IsRecommend] =1 '    --print @sql    --exec(@sql)    set @sql ='select * from  ( select  ROW_NUMBER() over(order by [Userid] ) as rownumber * from #StudioList ) as a'    if(@type = 1)        set @sql = @sql + '  where a.rowNumber between '+cast(@page as varchar(10))+' and '+cast(@pageend as varchar(10))     else if(@type = 2)        set @sql = @sql + ' where a.rowNumber between '+cast(@page as varchar(10))+' and '+cast(@pageend as varchar(10))+' order by a.[RegisterCount] desc '    else         set @sql = @sql + ' where a.rowNumber between '+cast(@page as varchar(10))+' and '+cast(@pageend as varchar(10))+' order by num desc '--print @sqlexec( @sql)--局部临时表生命周期仅限于这个存储过程的执行过程中,执行完自动drop了--IF EXISTS  (SELECT  * FROM dbo.SysObjects WHERE ID = object_id(N'#StudioList') AND OBJECTPROPERTY(ID 'IsTable') = 1) --        drop table #StudioList  goexec proc_Activity_StudioList @province=0@type=1@collegeId=0@index=1@size=30
时间: 2024-11-01 22:25:01

sqlserver-ado.net 执行存储过程中使用临时表出错的相关文章

mysql 存储过程中游标临时表问题

问题描述 mysql 存储过程中游标临时表问题 DELIMITER $$ USE laolao$$ DROP PROCEDURE IF EXISTS parent_sport_sort1$$ CREATE DEFINER=root@% PROCEDURE parent_sport_sort1(IN jidb VARCHAR(64),IN uname VARCHAR(64),IN starttime VARCHAR(64),IN endtime VARCHAR(64), IN startmonth

Oracle存储过程中使用临时表

原文地址: http://sosuny.javaeye.com/blog/551006   一.Oracle临时表知识   在Oracle中,临时表分为SESSION.TRANSACTION两种,SESSION级的临时表数据在整个SESSION都存在,直到结束此次SESSION:而 TRANSACTION级的临时表数据在TRANACTION结束后消失,即COMMIT/ROLLBACK或结束SESSION都会清除 TRANACTION临时表数据.  1) 会话级临时表 示例  1创建 Sql代码 

SQL Server数据库的存储过程中定义的临时表,真的有必要显式删除(drop table #tableName)吗?

原文:SQL Server数据库的存储过程中定义的临时表,真的有必要显式删除(drop table #tableName)吗?   本文出处:http://www.cnblogs.com/wy123/p/6704619.html      问题背景 在写SQL Server存储过程中,如果存储过程中定义了临时表,有些人习惯在存储过程结束的时候一个一个显式地删除过程中定义的临时表(drop table #tName),有些人又没有这个习惯,对于不明真相的群众或者喜欢思考的人会问,存储过程中定义的临

select-oracle临时会话表在存储过程中的使用

问题描述 oracle临时会话表在存储过程中的使用 问题: 在将db2的存储过程转换为oracle的过程中,涉及到临时会话表,会出现问题: 具体描述: 原db2中的存储过程涉及临时会话表的部分: --创建临时会话表 DECLARE GLOBAL TEMPORARY TABLE COL_NAMES( id integer, COLNAMES varchar(50) )WITH REPLACE NOT LOGGED on commit preserve ROWS ; DELETE FROM SESS

在SQL触发器或存储过程中获取在程序登录的用户_MsSql

实现一个AuditLog的功能,是B/S结构专案. 每个用户可以登录系统,在程序中操作数据(添加,更新和删除)需要实现记录操作跟踪.是谁添加,更新和删除的,这些信息将会插入至AuditLog表中. 一般情况之下,在SQL的触发器中,只能取到(SQL验证sa:Windows验证Domain\xxx).这些用户名,达不到效果,不能真正反映到是谁操作的. 下面是让你清楚,怎样实现在SQL触发器或存储过程中获取在程序登录的用户,是在插入,更新或删除的存储过程,把登录程序当前用户传入进去.在存储过程中,再

在SQL触发器或存储过程中获取在程序登录的用户

实现一个AuditLog的功能,是B/S结构专案. 每个用户可以登录系统,在程序中操作数据(添加,更新和删除)需要实现记录操作跟踪.是谁添加,更新和删除的,这些信息将会插入至AuditLog表中. 一般情况之下,在SQL的触发器中,只能取到(SQL验证sa:Windows验证Domain\xxx).这些用户名,达不到效果,不能真正反映到是谁操作的. 下面是让你清楚,怎样实现在SQL触发器或存储过程中获取在程序登录的用户,是在插入,更新或删除的存储过程,把登录程序当前用户传入进去.在存储过程中,再

如何在存储过程中执行别的存储过程并获取其返回的结果集

背景 在sqlserver存储过程的编写过程中,通常会碰到繁杂的业务逻辑,将一个大的存储过程拆分成多个小存储过程来做是常用的做法. 问题 在存储过程A中需要调用存储过程B,而存储过程B返回一个Table,A需要获取到B的Table,再进行下一步的处理. 本文解决的问题是:如何在A中执行B,并获取B的返回Table 思路 在A中使用Exec来执行存储过程B 在A中定义临时表,并将B返回的数据插入临时表中 步骤 新建一张测试表,并批量向表中插入若干数据 CREATE TABLE TestTable

mysql 存储过程中使用了游标和临时表,返回的临时表数据不准确

问题描述 mysql 存储过程中使用了游标和临时表,返回的临时表数据不准确 DELIMITER $$ USE laolao$$ DROP PROCEDURE IF EXISTS parent_sport_sort1$$ CREATE DEFINER=root@% PROCEDURE parent_sport_sort1(IN jidb VARCHAR(64),IN uname VARCHAR(64),IN starttime VARCHAR(64),IN endtime VARCHAR(64),

SQLServer存储过程中事务的使用方法_MsSql

本文为大家分享了SQLServer存储过程中事务的使用方法,具体代码如下 create proc usp_Stock @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId int, @StockUnit varchar(50), @StockDate datetime, @TotalMoney money , @ActMoney money , @baseId int, @Description nvarcha