Sqlserver2000 Stored Procedure learning notes

Source: Internet
Author: User
Start to learn how to write the stored procedure.
First, I figured out several basic statements, starting from 0:
Declare a variable: declare @ Gao varchar (30)
Keyword: declare
Variable name: @ Gao
Why @? I don't know. It's necessary. Remember this first, and I will come back to supplement it later.

Assignment: Select @ Gao = convert (varchar, getdate (), 111)
Keywords: select
Strange usage. Why select? I don't know if there are any other methods. Remember this first...
Use a function: Convert (varchar, getdate (), 111). The first parameter is the output data type, and the second parameter is a random legal expression or function, the third is the date format code output. I have included a special post description.
Http://blog.csdn.net/gaoyunpeng/archive/2007/03/19/1533594.aspx

Output: Print @ Gao
Keyword: Print
I tried a bunch of them, write, out, and output, and finally I knew that I needed to use print, grack.

Complete operation:
Declare @ Gao varchar (30)
Select @ Gao = convert (varchar, getdate (), 111)
Print @ Gao

Output:
2007/03/19

Well, it's a success.

======================================

@ Trancount is used to obtain the number of transactions currently executed. Why are two @ s used? Waiting for learning...

Exec and execute seem to have the same functions ~ It is used to execute an SQL statement or a stored procedure. Or is it simply the two names of the same thing ??

======================================

See the following SQL statement.
Select count (*) from MSDB. DBO. syscategories where name = n' [uncategorized (local)]'
N '[uncategorized (local)]' is depressing, because I don't know what n is ~~ So -- Query!
Result:
All Unicode strings must be uppercase letters (n) written before the SQL Server online book topic "use Unicode data" When Unicode string constants are used in SQL Server. The "N" prefix represents the SQL-92 standard, which is related to the national language and must be capitalized. If you run a unicode String constant without a prefix to use N, sqlserver uses the string before it reaches the non-Unicode code page of the current database.
The above is a brief explanation written on Microsoft's help homepage, which is difficult to translate. However, people have declared that it is machine-based automatic translation, but it is enough to explain the problem, that N represents a unicode string ~, Complete.

========================================================== ====

Raiserror Function
Looking for information everywhere, I found no, and finally found it on msdn. Later I thought, too, people's own things must have a help document. Since there is a help document, it seems redundant for others to write more. I have reprinted the help Article and wrote a lot of it, but the general usage is not so luocheng, So I recorded a common version, the Code is as follows:
Raiserror ('error! The cigarette inventory is smaller than or equal to 0 and cannot be sold. ', 16,1)
Explanation:
The first parameter: Error message. It can be a string directly, a string variable, or an error code, it seems that the value must be greater than 50000 (I don't know why I chose such a large number). If the user chooses to directly write the error message, the default value of this Code is 50000; you can select only one of the preceding three parameters, namely, a direct value, variable, or code.
The second parameter indicates the error severity. The value ranges from 0 to 25. Among them, 20-25 is regarded as a fatal error (especially serious ~), Users who require certain permissions can send out the error message. They also need to add other parameters, too many. Besides, we usually don't use those, so just select a number, A dozen or so (I don't know if I need to strictly select a number here, and I will try again later). If the specified number is smaller than 0, it will take 0.
Third parameter:

Any integer between 1 and 127. The negative value of state is 1 by default. If the value is 0 or greater than 127, an error is generated.

If the same user-defined error is thrown at multiple locations, you can use a unique status code for each location to locate the code segment that causes the error.

The above is what I said on msdn, which is quite neat and can be referenced directly.

Execution result:

Raiserror ('error! The cigarette inventory is smaller than or equal to 0 and cannot be sold. ', 16,1)
Displayed after execution:
Server: MSG 50000, level 16, state 1, line 7
Error! The cigarette inventory is smaller than or equal to 0 and cannot be sold.

Complete.

==========================================

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.