Some understanding of SQL Server stored procedure-hop cursors

Source: Internet
Author: User

In the database operation request recently given to me by my boss, there is a table with a shopping list style, with the customer ID, product ID, number of items, unit price and total price of the item, and the date of sale. There is also a product discount information table, on this basis, the price of the same commodity prices will be different, different customer maximum discount amount, the discount rate also varies, requires SQL Server stored procedure to close the cursor

At first, there was no thought and a clue, listen to the boss is to use stored procedures to store the query to the data, and then iterate through the cursor. For a stored procedure, the cursor is not well-known, but the result set that was found in the SQL statement is placed in the stored procedure, and the cursors are not understood in depth. So in the beginning, I was thinking of using SQL statements to directly add the results to the cursor, which was later found to be difficult to implement. The customer ID, the item ID in the new table is constant, the field values are changed all the time, and are also associated with other tables.

The solution is to link the two tables through a foreign key association, define the variables to receive the data that is found, and add them through the loop in the cursor. However, in the process of cursor implementation encountered a problem to understand that the same customer each time the purchase of goods and unit price are different, and each time the discount calculation. This requires manipulation of each cursor, but how to determine the difference between the next cursor and the previous one, here I use the IF condition to judge, by giving him a variable value for each cursor, let the value of his variable equal to the customer ID I get, compare each time ID difference. Variable settings are much more, for each operation changes the value of basically set a variable to accept, for the insert and update operation is also through the ID variable to be judged. But this operation seems to be too complicated, the IF condition in the loop is judged more, whether there is a better way to operate it? In the next section I will say one of my own ideas, of course, the next section will be some of the relevant information found in the written

Some understanding of SQL Server stored procedure-hop cursors

Related Article

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.