Return dynamic table content using custom functions in sqlserver

Source: Internet
Author: User

Note: In the supplyplan table, each requestqty and its corresponding start and end date segment are stored, because I will determine and process the date range of requestqty that belongs to the current day in future processing, therefore, the backend database can only be designed as this storage mode. However, when displaying the page, you need to dynamically generate multiple date segments and their quantities based on each supplyplanno, therefore, a user-defined function is used to return the dynamic record set after processing! The returned field @ allrequestqty is in the format of "1 | 13 | 5 | 8". You can use the split function grouping in ASP to dynamically display fields.

/*
** Add by WLS for: Convert sort lines of SP datas to one char group by vehicle, part
*/
Create Function xzc. fn_getcontent_supplyplan
(@ S_p_no Int = NULL,
@ Viewflag varchar (1) =''
)
Returns @ fn_getcontent_supplyplan table
(
Idvarchar (8000 ),
Supplyplanno int,
Vehicle varchar (25 ),
Sort varchar (20 ),
Part varchar (25 ),
Requestqty varchar (8000 ),
Remark varchar (255)
)
As
Begin

Declare @ svehicle varchar (25), @ ssort varchar (20)
Declare @ Spart varchar (25), @ ivehicle varchar (25)
Declare @ ipart varchar (25), @ sremark varchar (255)

Declare @ requestqty int, @ theid int
Declare @ allrequestqty varchar (8000), @ allid varchar (8000)

Select @ allrequestqty =''
Select @ allid =''

/* Outer cursor */
Declare getvehiclepartsp cursor scroll
Select distinct vehicle, sort, part from supplyplan
Where supplyplanno = @ s_p_no and viewflag = @ viewflag

/* -- Order by ID DESC
Select vehicle, sort, part from (select Min (ID) as ID, vehicle, sort, part from supplyplan
Where supplyplanno = @ s_p_no group by vehicle, sort, part) A order by A. ID DESC
*/

/* Inner cursor */
Declare getcontentsp cursor scroll
Select ID, vehicle, part, requestqty
From supplyplan where supplyplanno = @ s_p_no and viewflag = @ viewflag order by ID

If @ s_p_no is null or @ s_p_no =''
Begin
Return
End

Open getvehiclepartsp

Fetch first from getvehiclepartsp into @ svehicle, @ ssort, @ Spart
While (@ fetch_status = 0)
Begin
Open getcontentsp
Fetch first from getcontentsp into @ theid, @ ivehicle, @ ipart, @ requestqty
While (@ fetch_status = 0)
Begin
If @ ivehicle = @ svehicle and @ ipart = @ Spart
Begin
Select @ allid = @ allid + '|' + convert (varchar (15), @ theid)
Select @ allrequestqty = @ allrequestqty + '|' + convert (varchar (25), @ requestqty)
End
Fetch next from getcontentsp into @ theid, @ ivehicle, @ ipart, @ requestqty
End
Close getcontentsp

Select @ allid = xzc. trim_char (@ allid, '| ')
Select @ allrequestqty = xzc. trim_char (@ allrequestqty, '| ')
Select @ sremark = (select top 1 remark from supplyplan where supplyplanno = @ s_p_no
And vehicle = @ svehicle and part = @ Spart)

Insert into @ fn_getcontent_supplyplan values (@ allid, @ s_p_no, @ svehicle, @ ssort, @ Spart, @ allrequestqty, @ sremark)

Select @ allrequestqty =''
Select @ allid =''
Select @ sremark =''

Fetch next from getvehiclepartsp into @ svehicle, @ ssort, @ Spart
End
Deallocate getcontentsp

Close getvehiclepartsp
Deallocate getvehiclepartsp


Return
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.