Convert a string pair to a dictionary (temporary table) User-Defined Function (SQL)
Source: Internet
Author: User
This function is often used, so it is written as a user-defined function. I believe it is practical.
Convert a pair of strings into a dictionary (temporary table)
However, when raiserror is used in a custom function to throw an error (stored in a stored procedure), I hope you can give me some advice.
Usage:
Select * From f_dic ('aa, bfeab, CC, DXD, eeeee ', '1, 2, 4, 1, 5 ')
Result
Aa 1
Bfeab 2
CC 4
DXD 1
Eeeee 5 create function [DBO]. [f_dic]
(
@ Keys varchar (8000 ),
@ Values varchar (8000)
)
Returns @ t table ([Key] varchar (20), value varchar (20 ))
As
Begin
-- If Len (@ keys)-len (replace (@ keys, ',') <> Len (@ values)-len (replace (@ values ,', ',''))
-- Begin
-- Raiserror (n' parameter @ keys length is different from that of @ values ',
-- 10, 1 );
-- End
While (charindex (',', @ keys) <> 0)
Begin
Insert @ T ([Key], value) values (substring (@ keys, 1, charindex (',', @ keys)-1), substring (@ values, 1, charindex (',', @ values)-1 ))
Set @ keys = stuff (@ keys, 1, charindex (',', @ keys ),'')
Set @ values = stuff (@ values, 1, charindex (',', @ values ),'')
End
Insert @ T ([Key], value) values (@ keys, @ values)
Return
End
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.