Circular usage in Oracle and MSSQL

Source: Internet
Author: User

1. Oracle

  1. CREATE OR REPLACE FUNCTIONSETSTATE (OLDVALUE VARCHAR2, pos number, SVALUE VARCHAR2)
  2. RETURNVARCHAR2
  3. IS
  4. RETURN_VALUE VARCHAR2 (20 );
  5. Len number (8 );
  6. I NUMBER (8 );
  7. TEMP_VALUE VARCHAR2 (1 );
  8. BEGIN
  9. LEN: = LENGTH (OLDVALUE );
  10. If pos> LENTHEN
  11. RETURN 'The specified index is longer than the string length! ';
  12. ENDIF;
  13. I: = 0;
  14. RETURN_VALUE: ='';
  15. FORIIN1. LEN LOOP
  16. TEMP_VALUE: = SUBSTR (OLDVALUE, I, 1 );
  17. If I = POSTHEN
  18. TEMP_VALUE: = SVALUE;
  19. ENDIF;
  20. RETURN_VALUE: = RETURN_VALUE | TEMP_VALUE;
  21. ENDLOOP;
  22. RETURNRETURN_VALUE;
  23. END;

2. MSSQL

  1. If exists (SELECT*FROMSYSOBJECTSWHERE NAME='Setstate')
  2.  DROP FUNCTIONSETSTATE
  3. GO
  4. CREATE FUNCTIONSETSTATE (@ oldvalue nvarchar (20), @ POSINT, @ Svalue nvarchar (1 ))
  5. RETURNSNVARCHAR (20)
  6. AS
  7. BEGIN
  8.  DECLARE@ RETURN_VALUE NVARCHAR (20 ),
  9. @ LENGTHINT,
  10. @ IINT,
  11. @ TEMP_VALUE NVARCHAR (1 );
  12.  SET@ LENGTH = LEN (@ OLDVALUE );
  13.  SET@ I = 1;
  14.  SET@ RETURN_VALUE ='';
  15. IF (@ POS> @ LENGTH)
  16. SET@ RETURN_VALUE = @ OLDVALUE;
  17.  ELSE
  18. BEGIN
  19. WHILE (@ I <= @ LENGTH)
  20. BEGIN
  21. SET@ TEMP_VALUE =SUBSTRING(@ OLDVALUE, @ I, 1 );
  22. IF (@ I = @ POS)
  23. SET@ TEMP_VALUE = @ SVALUE;
  24. SET@ RETURN_VALUE = @ RETURN_VALUE + @ TEMP_VALUE;
  25. SET@ I = @ I + 1;
  26. END
  27. END
  28.  RETURN@ RETURN_VALUE;
  29. END
  30. GO

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.