Pinyin code query is automatically implemented using Stored Procedures + triggers

Source: Internet
Author: User

Steps:

1. Download Chinese pinyin table: http://download.csdn.net/source/326464

2. Put the downloaded T-SQL statement into the database that requires functionality to run. will generatePycode This table

3. Create a field in the table to be implemented and name itNamepy

4. Run the following stored procedures and triggers:

<1> A stored procedure is used to read and query each Chinese character in a field in its pinyin code (No changes required)

<2> trigger: When a Chinese field is inserted or updated, the trigger automatically inserts or changes the pinyin code.

Code

  --  The stored procedure converts Chinese characters into pinyin output  
Create Proc Hz2py
@ Hz Varchar ( 200 ),
@ Py Varchar ( 200 ) Output
As
Declare @ I Int
Declare @ Chr Varchar ( 2 )
Declare @ PyC Varchar ( 1 )
Set @ I = 1
While ( @ I <= Len ( @ Hz )) Begin
Set @ Chr = Substring ( @ Hz , @ I , 1 )
Set @ PyC = ( Select PY From Pycode Where Hz = @ Chr )
If ( @ PyC Is Null )
Set @ Py = @ Py + @ Chr
Else
Set @ Py = @ Py + @ PyC
Set @ I = @ I + 1
End

-- The pinyin code is changed when the trigger is inserted and updated. -- Namepy is a pinyin field. -- Bank indicates the name of the table to be implemented. -- Customername is a Chinese character field -- Cardid is the primary key of the bank table.
Create Trigger Namepy On Bank For Insert , Update
As
Declare @ Name Varchar ( 100 ), @ YY Varchar ( 100 ), @ CID Char ( 10 )

If ( Update (Customername )) Begin
Set @ YY = ''
Select @ Name = Customername, @ CID = Cardid From Inserted -- Call a stored procedure
Exec Hz2py @ Name , @ YY Output

Update Bank Set Namepy = @ YY Where Cardid = @ CID

End
Go

 

Well, if you have any questions, leave a message to me ..

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.