create a function returns a table (using cursor)
来源:互联网 发布:淘宝 账号 霸王条款 编辑:程序博客网 时间:2024/06/03 17:58
ALTER FUNCTION [dbo].[GetWeighingInfoByTransactionId] (-- Add the parameters for the function here@transaction_id UniqueIdentifier)RETURNS @weighingInfo table (first_weight decimal (10,2),first_weighing_datetime datetime, second_weight decimal (10,2),second_weighing_datetime datetime)AS Begindeclare @p_first_weight decimal (10,2), @p_first_weighing_datetime datetime, @p_second_weight decimal (10,2), @p_second_weighing_datetime datetimedeclare cur_weigh cursor for select weight,weighing_datetime from dbo.tbl_weighingwhere weigh_code = @transaction_id order by weighing_datetime desc;declare @num intset @num = (select count(*) from dbo.tbl_weighingwhere weigh_code = @transaction_id )OPEN cur_weighIF (@num = 0) set @num = 0ELSEBEGINif (@num = 1) beginFETCH NEXT FROM cur_weigh into @p_second_weight,@p_second_weighing_datetimeInsert into @weighingInfo values (null,null,@p_second_weight,@p_second_weighing_datetime)endelseBEGINFETCH NEXT FROM cur_weigh into @p_second_weight,@p_second_weighing_datetimeWHILE @@FETCH_STATUS = 0beginFETCH NEXT FROM cur_weigh into @p_first_weight,@p_first_weighing_datetimebreak;endinsert into @weighingInfo values (@p_first_weight,@p_first_weighing_datetime,@p_second_weight,@p_second_weighing_datetime)ENDENDCLOSE cur_weighDEALLOCATE cur_weighreturn End
How to use it:
select * from dbo.GetWeighingInfoByTransactionId ('836CE377-E7AD-4E25-ADAF-0C44742BD402')
Call the function join with other table (Cross Apply) , the parameter is in the selected result and it be will pass into the function every row.
select distinct weigh_code,s.*from tbl_weighingcross applydbo.GetWeighingInfoByTransactionId(weigh_code) s
http://msdn.microsoft.com/en-us/library/ms131103.aspx
http://www.sqlteam.com/article/returning-complex-data-from-user-defined-functions-with-cross-apply
- create a function returns a table (using cursor)
- A function returns function
- Is there a way to loop through a table variable in TSQL without using a cursor?
- CREATE TABLE A LIKE B
- ABAP Using a Cursor to Read Data
- SQL: CREATE a table from another table
- using INSTR function for obtaining data from a table, based on a selection from the master table (FK relation)
- sql cursor update a table by another table
- CareerCup Write a function that given a position returns the digit in that 0123456789101112131415..
- Write a function that takes a string as input and returns the string reversed.
- The "data" option should be a function that returns a per-instance value in component definitions.
- How to create a function in SQL
- Create a new GUID using FreePascal
- Create a Host Bridge Using virsh
- Create a global table for LUA stack
- Create a table unless it already exists
- Create a Model and Database Table
- Using a Custom Action to Create a Database During Installation
- Java基础23天--05--网络编程(TCP和UDP)
- MPICH2(1.2.1) Debian6 安装配置参考
- devexpress xtrareport
- 这是冒泡排序的变种吗? 冒泡排序和选择排序
- Java基础24天--01--网络编程(TCP--上传图片)
- create a function returns a table (using cursor)
- naive date(time) 和aware date(time)的区别 及 pymongo操作dateime的注意事项
- 利用表分区简化数据库维护
- 可变参数表 -----如何实现printf函数(1)
- 数据库镜像入门
- 关于 SQL Server 群集的几个关键技巧
- oracle安装与卸载
- 配置PLSQL 与ORACLE客户端集成连接ORACLE服务器
- 关于中断处理程序是否可以睡眠问题