Stored procedure (Stored Procedure) using (c)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/masson32/article/details/5006620

 Following The second, followed by the stored procedure used to achieve other functions

Seven string split:

       If only one stored procedure parameter is nvarchar or varchar like string type, if a plurality of strings, this stored procedure to be invoked multiple times, so that the database is connected to a plurality of times, less efficient, it can be composed of a plurality of strings string, signed and spaced apart, and the string stored in the resolution process, then other operations, so that only one call a stored procedure, of course, higher efficiency than the multiple calls. The following is how to split the string (strings are separated by commas):

 

Process explain: string found @Name the first separator " , " position, in SQL , the position of the string from a beginning (to the C # open start difference string position). This string is then pre-separator removed, this string is the first string required string @Name subtracting the string has been made and delimiters. In this way, re-string the string has been subtracted made to operate continuously repeated. Until the last character string, this string is not delimiter, the string itself is the need to take.

Here a few functions to explain:

( . 1 ) CHARINDEX ( separator, string ) : obtaining a first position designation delimiter in the string. Return int , such as string, "ABC, EFG, HIJ" , calls CHARINDEX ( ',', 'ABC, EFG, HIJ' ), returns 4 . If there is no matching delimiters, it returns 0.

( 2 ) left ( string, left N characters ) , returns this string from left N characters. Such as call left (ABCDEFG, 3) , then returns abc

    ( . 3 ) right (( string, from right N characters ) , returns this string from right N characters as call right (ABCDEFG,. 3) , is returned EFG .

 

Eight randomly selected table records:

       If you need to select from a table of random N records are returned, the need to use Order by NEWID () . The following format:

select top N * from tableName order by newid()

       Note that, here, N is a number has been determined, i.e., before the call has been determined N value. * Represent the columns you want to select, you can add where statements were randomly selected in the qualifying line. As shown, from the following A random selection of Table 2 records that meet the conditions of Article:

 

    If N is the SQL query out by other tables to determine the value, you can put in a variable written statement, to be N to determine and then execute it, as shown below.

Nine of temporary table:

       If you need to check out a bunch of records, and then take the data records from the heap, then use the temporary table is a good choice. Create a temporary table, the table name needs to add " # ." The format is:

create table #t ( parameter name parameter type ) , the following are examples, the table A , table B , table C in the name of the query are out on the temporary table, and then remove all the temporary table name.

 

   至此,已经将所用到的使用存储过程的功能使用进行了介绍,希望可以帮到初学者,同时也可以记录一下自己学习的过程。呵呵

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/masson32/article/details/5006620