SQL function data solves the problem of insert multiple rows of automatic flow

Source: Internet
Author: User

In the memory process of MSSQL, there will be a one-time insertion of multiple conditions, insert into ...... select ....

In this case, if the inserted Table contains custom business flow logs ['Bu '+ date + flow logs], you have to use the game tag, then, you can use an automatic snapshot to store the processing process. Because the SELECT statement cannot be used to return the result set of a column, the function is acceptable.

The table name here must be clear and the reason is simple. MSSQL has many restrictions on custom function numbers, such as the inability to perform dynamic T-SQL, the insert, update, and delete operations cannot be performed on the table in the "Data preview" column. [Table changes are acceptable].

-- Select DBO. fn_getautoid ('', 'st3 _ padd', 'po', '2014/1/21', 4)
Create   Function Fn_getautoid
(
@ No_comp Nvarchar ( 10 ), -- Company
@ Tablename Nvarchar ( 100 ), -- Table Name
@ Prefix Nvarchar ( 4 ), -- Prefix
@ Dt_trn Nvarchar ( 10 ), -- Date
@ Id_len Tinyint , -- Growth of water
@ Rownum Int -- Forward sort
)
Returns   Nvarchar ( 30 )
As
Begin
Declare   @ Autoid Nvarchar ( 30 ) -- Automatic ticket renewal
Declare   @ Prefix_all Nvarchar ( 15 ) -- Prefix

If   @ No_comp   Is   Null
Set @ No_comp = ''

If   @ Prefix   Is   Null
Set   @ Prefix = ''

If   @ Id_len   Is   Null
Set   @ Id_len = 4

Set   @ Dt_trn = Replace ( Replace ( @ Dt_trn , ' / ' , '' ), ' - ' , '' )

-- Prefix
Set   @ Prefix_all = @ Prefix + @ Dt_trn

-- Add Table
If   @ Tablename = ' St3_padd '
Begin
Select @ Autoid = @ Prefix_all + Right ( ' 00000000 ' + Cast ( Isnull ( Max ( Cast ( Right (No_po, @ Id_len ) As   Int )), 0 ) + @ Rownum   As   Nvarchar ), @ Id_len )
From St3_padd
Where (No_comp = @ No_comp   Or   @ No_comp = '' )
And No_po Like   @ Prefix_all + ' % '
End
/* Other tables ..
Else if @ tablename =''
Begin
End */
Return   @ Autoid
End 


Insert a numeric SQL statement is similar to the following SQL statement:

Insert   Into St3_padd (no_comp, no_po, no_src, seq_src)
Select No_comp, DBO. fn_getautoid (no_comp, ' St3_padd ' , ' Po ' , ' 2011/07/21 ' , 4 , Row_number () Over ( Order   By No_club), no_club, no_seq
From St3_clubmd
Where No_comp = ' 0001 '
And No_club = ' Fr201511290001 '

Select * FromSt3_padd 

Note:In this example, the table name and the correlation between them use the actual substitution method, which is only used as the reference.

This example does not involve data analysis and issues the insertion question. It is estimated that the problem can be solved only by using the camera system.

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.