Home Database Mysql Tutorial SQL server使用自定义函数以及游标

SQL server使用自定义函数以及游标

Jun 07, 2016 pm 06:03 PM
cursor Custom function

最近忙于动态监测软件的开发,处理有关标准宗地编码和区段编码关系,关系如下表所示

现在需要将表中的数据转换为如下表所示结果:

在SQL server数据库中,创建自定义函数,通过游标,将表的数据转化为结果表,函数代码如下所示:

代码如下:
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 --定义游标
For(select landCode from land where sectCode=@name )
open myCur –-打开游标
fetch next from myCur into tempStr –将游标下移
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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommend the best Windows 11 mouse cursor solution Recommend the best Windows 11 mouse cursor solution Apr 23, 2023 am 09:52 AM

Windows 11 has a seemingly unlimited amount of customization options, from default settings to every third-party app on the Internet. There are even apps that can change the appearance of your mouse cursor. Modifying the cursor is a great way to give your computer a unique look. You don't have to stick the same boring black and white pointer on every computer. But even so, you don't have to download software to change the look of your cursor. How to change the appearance of the cursor? Windows 11 offers a small amount of customization for the cursor. You can change the cursor by going into Control Panel and selecting Mouse Options there. A new window called "Mouse Properties" will appear. In the mouse properties you can change the color scheme, size and design. Your computer will naturally

How to write custom functions in MySQL using Python How to write custom functions in MySQL using Python Sep 22, 2023 am 08:00 AM

How to use Python to write custom functions in MySQL MySQL is an open source relational database management system that is often used to store and manage large amounts of data. As a powerful programming language, Python can be seamlessly integrated with MySQL. In MySQL, we often need to use custom functions to complete some specific calculations or data processing operations. This article will introduce how to use Python to write custom functions and integrate them into MySQL. For writing custom functions,

In-depth analysis of the declaration and call of JS custom functions In-depth analysis of the declaration and call of JS custom functions Aug 03, 2022 pm 07:28 PM

A function is a set of reusable code blocks that perform a specific task (have a specific functionality). In addition to using built-in functions, we can also create our own functions (custom functions) and then call this function where needed. This not only avoids writing repeated code, but also facilitates the later maintenance of the code.

How to customize functions in PHP How to customize functions in PHP May 18, 2023 pm 04:01 PM

In PHP, a function is a set of reusable blocks of code that are identified by a name. PHP supports a large number of ready-made functions, such as array_push, explode, etc., but sometimes you need to write your own functions to implement specific functions or improve code reusability. In this article, I will introduce how to customize functions in PHP, including function declaration, calling and using function parameters. Declaration of functions To declare a function in PHP, you need to use the keyword function. The basic syntax of the function is as follows:

How to write custom stored procedures and functions in MySQL using PHP How to write custom stored procedures and functions in MySQL using PHP Sep 21, 2023 am 11:02 AM

How to write custom stored procedures and functions in MySQL using PHP In the MySQL database, stored procedures and functions are powerful tools that allow us to create custom logic and functions in the database. They can be used to perform complex calculations, data processing and business logic. This article will introduce how to write custom stored procedures and functions using PHP, with specific code examples. Connecting to the MySQL database First, we need to connect to the MySQL database using the MySQL extension for PHP. can use

Introduction to the basics of Python functional programming Introduction to the basics of Python functional programming Apr 11, 2023 pm 10:49 PM

Basic knowledge of functions: Master the basic syntax specifications and calling methods of custom functions, and master the usage and calling rules of various parameters of functions. 1. Python function (Function) is an organized, reusable code segment used to implement a single or related function. Functions can improve application modularity and code reuse. We have already touched on many of the built-in functions provided by Python, such as print(). But you can also create your own functions, which are called user-defined functions. 2. Basic rules for customizing a function. You can define a function with the functions you want. The following are simple rules: the function code block starts with the def keyword, followed by the function identifier name and parentheses.

Creation of PHP user-defined functions Creation of PHP user-defined functions Apr 14, 2024 am 09:18 AM

PHP custom functions allow encapsulating code blocks, simplifying code and improving maintainability. Syntax: functionfunction_name(argument1,argument2,...){//code block}. Create function: functioncalculate_area($length,$width){return$length*$width;}. Call the function: $area=calculate_area(5,10);. Practical case: Use a custom function to calculate the total price of the items in the shopping cart, simplifying the code and improving readability.

Comparison of the pros and cons of built-in functions and custom functions in Golang Comparison of the pros and cons of built-in functions and custom functions in Golang May 16, 2023 pm 08:51 PM

Golang is a very popular programming language with a very powerful function library. In Golang, functions are considered first-class citizens, which means that Golang functions can be passed, copied, and overloaded like variables. In addition, Golang also provides two types of built-in functions and custom functions. In this article, we will explore the pros and cons of built-in functions and custom functions in Golang to help readers understand when to choose which type of function. First, let's look at the built-in functions. built-in letter

See all articles