Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use custom functions and cursors in SQL server

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

SQL server how to use custom functions and cursors, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Serial number

Standard parcel code (landCode)

Region coding (sectCode)

one

131001BG001

G001

two

131001BG002

G001

three

131001BG003

G001

four

131001BG004

G002

five

131001BG005

G003

Now you need to convert the data in the table to the results shown in the following table:

Serial number

Section coding

Included standard parcels

one

G001

131001BG001,131001BG002,131001BG003

two

G002

131001BG004

three

G003

131001BG005

In the SQL server database, create a custom function to convert the data of the table into the result table through cursors. The function code is as follows: copy the code as follows: create function combstr (@ name nvarchar (50)) returns nvarchar (300) as begin declare @ resultStr nvarchar (300) declare @ tempStr nvarchar (500) declare @ flag int declare myCur cursor-define the cursor For (select landCode from land where sectCode=@name) open myCur-open the cursor fetch next from myCur into tempStr-move the cursor down set @ flag=0 while @ @ fetch_status=0 begin if @ flag=0 begin set @ resultStr=@tempStr end else begin set @ resultStr=@resultStr+' '+ @ tempStr end set @ flag=@flag+1 fetch next from myCur into @ tempStr end close myCur deallocate myCur return @ result end

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report