SQL merges the same field values for multiple-line records _mssql

Source: Internet
Author: User
1. From the database in the first query to meet the criteria of records, stored in a DataTable, in the use of C # and so began to traverse the table, using the primary key in DataRow, and then read the corresponding criteria for the number of records, merged these second read to the record content, Return to the previous DataRow data row. There is nothing wrong with this, but if the volume of data is large, we may face countless open disconnected database links, the speed efficiency will be very low.
2. Read data from the database to a table and return to the UI layer. Everyone wants to do this, but it used to be stupid not to study this, because today, because of the large amount of data, I have to think of some other ways to improve efficiency.
GOOGLE~HK, if there is an answer, and then according to gourd painting gourd, himself wrote a
The goal is to obtain a non quantitative, qualifying part-time record and record each part of the project in a few fields, and then return to table one at a time
Copy Code code as follows:

Create function fn_getjoblistbypid--creates custom functions to obtain all project numbers and number of items assigned to part-time participation
(
@ParttimerID int
)
Returns @t table (Jobs varchar (5000), Parttimerid int,totalcount int)
As
Begin
DECLARE @sql varchar (5000), @TotalCount int
Set @sql = '
Set @TotalCount =0
Select @sql = @sql +j.jobno+ '-' +j.jobwave+ ', @TotalCount = @TotalCount +1
From Onjb_jobapplication A,onjb_jobs J
where a.parttimerid= @ParttimerID
and a.result= ' V '
and A.jobid=j.jobid
Insert @t values (@sql, @ParttimerID, @TotalCount)
Return
End

Reference
Copy Code code as follows:

--...........................
--Done project
LEFT join (select Jobs,parttimerid,totalcount from Fn_getjoblistbypid (@ParttimerID)) as J1
on P.par Ttimerid=j1. Parttimerid
--In doing project
LEFT join (select Curjobs,parttimerid,curcount from Fn_getcurjobsbypid (@ParttimerID)) as J2
on P.parttimerid=j2. Parttimerid
where p.parttimerid= @ParttimerID

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.