create Procedure sp_getpricelist
(
@m_strPID varchar(1000)
)
as
begin
--region 创建临时表
IF object_id('tempdb..##ThisIsATest') is not null
begin
drop table ##ThisIsATest
end
CREATE TABLE ##ThisIsATest
(
ID int Primary Key, --经销商报价产品ID
PID bigint, --产品ID
Province nvarchar(100) default(''), --省份
City nvarchar(100) default(''), --城市
Price money default(0.00) --价格
--其它字段忽略
)
--endregion 创建临时表结束
declare @m_lPriceId bigint
declare @SplitArray table(ID bigint Primary Key)
declare @tempIndex int
set @tempIndex=1
if charindex(',',@m_strPID)=0
begin
insert into @SplitArray values(Convert(bigint,@m_strPID))
set @tempIndex=0
end
while @tempIndex>0
begin
set @tempIndex=charindex(',',@m_strPID)
print @tempIndex
if @tempIndex>0
begin
insert into @SplitArray values(Convert(bigint,substring(@m_strPID,1,@tempIndex-1)))
set @m_strPID=substring(@m_strPID,@tempIndex+1,datalength(@m_strPID)-@tempIndex)
if charindex(',',@m_strPID)=0
begin
insert into @SplitArray values(Convert(bigint,@m_strPID))
end
end
end
--select * from @SplitArray
declare p_cursor1 cursor for select ID from @SplitArray
open p_cursor1
fetch next from p_cursor1 into @m_lPriceId
declare @loopi1 int
set @loopi1=1 --设置计数器
while @@FETCH_STATUS = 0
begin
declare @m_pCursorId int --循环指针
declare @strsql nvarchar(4000)
declare @ParamName varchar(100)
declare @PValue nvarchar(4000)
declare @Province nvarchar(100)
declare @City nvarchar(100)
declare @Price money
declare @PID bigint
insert into ##ThisIsATest(ID) values(@m_lPriceId)
declare p_cursor cursor for select B.ID from T_Product_PriceInfo A,T_Product_Param_Value B,T_Cate_Param C,T_Product_Quote D where D.ID=@m_lPriceId and A.ID=B.P_ID and B.C_ID=C.ID and A.ID=D.P_ID
open p_cursor
fetch next from p_cursor into @m_pCursorId
declare @loopi int
set @loopi=1 --设置计数器
while @@FETCH_STATUS = 0
begin
select
@PID=A.ID,
@ParamName=C.ParamName,@Province=D.Province,@City=D.City,@Price=D.Price,
@PValue =
case
when (C.ValueType>0 and C.ValueType<11) then Convert(nvarchar(4000),B.S_Value)
when (C.ValueType>10 and C.ValueType<21) then Convert(nvarchar(4000),B.I_Value)
when (C.ValueType>20 and C.ValueType<31) then Convert(nvarchar(4000),B.D_Value)
end
from T_Product_PriceInfo A,T_Product_Param_Value B,T_Cate_Param C,T_Product_Quote D
where
D.ID=@m_lPriceId and A.ID=D.P_ID and
A.ID=B.P_ID and B.C_ID=C.ID and B.ID=@m_pCursorId
if col_length('tempdb..##ThisIsATest',@ParamName) is null
begin
set @strsql='Alter table ##ThisIsATest add '+@ParamName+' nvarchar(4000)'
Exec(@strsql)
end
set @strsql ='update ##ThisIsATest set '+@ParamName+'='''+@PValue+''','
set @strsql=@strsql+' PID='+Convert(nvarchar(10),@PID)+','
set @strsql=@strsql+' Province='''+@Province+''','
set @strsql=@strsql+' City='''+@City+''','
set @strsql=@strsql+' Price=convert(money,'''+Convert(nvarchar(10),@Price)+''')'
--其它字段忽略
set @strsql=@strsql+' where ID='+Convert(nvarchar(10),@m_lPriceId)
Exec(@strsql)
set @loopi= @loopi+1
fetch next from p_cursor into @m_pCursorId
end
close p_cursor
DEALLOCATE p_cursor
set @loopi1= @loopi1+1
fetch next from p_cursor1 into @m_lPriceId
end
close p_cursor1
DEALLOCATE p_cursor1
delete from ##ThisIsATest where PID is null
set @strsql='select * from ##ThisIsATest'
Exec(@strsql)
drop table ##ThisIsATest
--go
--删除临时表 http://www.shenjk.com/
end
go
sp_getpricelist '1,2,3,4,5,6,7'
相关推荐
本资源结合实例实现一个复杂的存储过程,存储过程中有用到游标、临时表、循环、递归等知识,sql文件附有实例数据表创建的sql语句。
临时表与永久表相似,但临时表存储在 tempdb 中,当不再使用时会自动删除。 临时表有局部和全局两种类型 2者比较: 局部临时表的名称以符号 (#) 打头 仅对当前的用户连接是可见的 当用户实例断开连接时被自动删除...
存储过程使用游标、临时表实现动态SQL查询 2020.3.24 用能第二周周二上午,任务需求:编写存储过程查询监测设备状态以及目标监测设备状态。(任务完成) 改存储过程包含了大量相关知识,特此记录,以便此后使用! 为...
原创sql存储过程函数范例,一是为了自己方便查找,今天到公司因为没有我保存的一些范例,一个简单的例子写了半个小时,如果有范例直接套几分钟肯定搞定,所以索性上传到CSDN上,何时何地都能找到我的范例了。...
FOR (SELECT dish_id, order_num FROM t_order_temp where userId = @userId) --查出需要的集合放到游标中 OPEN My_Cursor; --打开游标 FETCH NEXT FROM My_Cursor INTO @DishId, @OrderNum; --读取第一行数据(将...
涉及sql语言建表、插入数据、视图、存储过程(临时表、游标)运用。 代码本人编写。
游标是SQL的一个内存工作区,由系统或用户以...游标的作用就是用于临时存储从数据库中提取的数据块。在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理,最后将处理结果显示出来或最终写回数据库。
内容包括SQL语言基础、常规数据查询、高级数据过滤、字符串查询、日期函数、日期函数、...存储过程和函数、使用游标、使用触发器、事务处理、安全性控制、SQL高级特性、数据库对象查询、数据库安全与维护和嵌入式SQL等...
我们在处理存储过程中的结果集时,可以使用游标,因为游标允许我们迭代查询返回的一组行,并相应地处理每行。mysql的游标为只读,不可滚动和敏感三种模式,我们来看下: 只读:无法通过光标更新基础表中的数据。 ...
使用游标将查询数据插到临时表。从临时表中抽出数据到文件。
SQL和CLR用户自定义函数、存储过程、触发器、事务和新的错误处理结构、应用并发模型支持并发用户、使用Service Broker来控制数据库应用程序中的异步处理等。 内容简介 本书是Inside Microsoft SQL Server 2005系列...
内容包括SQL语言基础、常规数据查询、高级数据过滤、字符串查询、日期函数、日期函数、...存储过程和函数、使用游标、使用触发器、事务处理、安全性控制、SQL高级特性、数据库对象查询、数据库安全与维护和嵌入式SQL等...
尽量避免使用游标,游标效率很差,如果使用游标就不要在游标的循环中使用表连接操作 注意where语句的写法,应该根据索引的顺序、范围的顺序、范围的大小来确定条件子句的前后顺序,尽量让字段顺序与索引顺序...
内容包括SQL语言基础、常规数据查询、高级数据过滤、字符串查询、日期函数、日期函数、...存储过程和函数、使用游标、使用触发器、事务处理、安全性控制、SQL高级特性、数据库对象查询、数据库安全与维护和嵌入式SQL等...
内容包括SQL语言基础、常规数据查询、高级数据过滤、字符串查询、日期函数、日期函数、...存储过程和函数、使用游标、使用触发器、事务处理、安全性控制、SQL高级特性、数据库对象查询、数据库安全与维护和嵌入式SQL等...
-- 隐式游标 (使用的表为Oracle默认自带的emp表) -- sql%rowcount:影响记录条数 sql%found:是否有满足条件的记录 set serveroutput on; declare v_ename a_emp.ename%type; begin select ename into v_ename ...
下面代码示例中, 先是宣告你在游标中需使用变量,也就是临时存储处理字段的数据。 2. 宣告一个游标,并SELECT需要处理的数据集。 3. 打开游标(#8行代码)。 4. 从游标中拿来FETCH NEXT 数据给变量赋值。 5. 循环@@...
资源名称:夜鹰全新sql2008高级视频教程 资源目录: 本套教程重点讲解的内容包括:sql语法基础、组合查询、去除重复、聚合函数、子查询、临时表、存储过程、触发器、游标、事务回滚、事务并发控制、约束等技术,...
*存在递归查询情况如果关联Table太多对性能会造成较大影响,往往推荐采用临时表转为分步骤操作提高性能 *尽量使用表关联查询而不使用函数,但涉及类似于代码表要重复关联多次取数据问题时候又适合使用函数
SQL一些存储过程示例 演示了存储过程基本使用。 以及演示了在存储过程中使用:临时表、函数、事务、嵌套查询、游标、异常处理、数据库的身份权限等。 详细使用见示例。