SQL circle algorithm ???

Source: Internet
Author: User
SQL code
-- ===================================================== ==========
-- Round-robin SQL algorithms (I feel stupid, but I don't know if there are any better algorithms)
-- Author: Cool and light flute
-- Date: 2008-02-21
-- Execution method (the parameter must be the square of the integer, but the size is not unlimited)
-- Exec raoquan 2500
-- ===================================================== ==========

Create proc raoquan
(
@ Num int
)
As
Begin

Set nocount on

Declare @ I int
Set @ I = SQRT (@ num)

If ceiling (@ I) @ I
Return

Declare @ table varchar (8000)
Declare @ insert varchar (8000)
Declare @ update varchar (8000)
Declare @ print varchar (8000)

Select @ insert = '', @ update ='', @ print =''

Select @ table ='
Create Table # T
('

Declare @ k int
Select @ k = 1
While (@ k 0)
Begin
If @ S3 % 2 = 0
Select @ Len = @ len-1
Select @ len2 = @ Len

While (@ len2> 0)
Begin
If @ S1 = ''+''
Begin
If @ S2 = 'C''
Select @ C = @ C + 1
Else
Select @ r = @ r + 1
End
Else
Begin
If @ S2 = 'C''
Select @ C = @ C-1
Else
Select @ r = @ r-1
End

Declare @ SQL varchar (8000)
Select @ SQL = ''update # t set [''+ Cast (@ C as varchar) +''] = ''+ Cast (@ I as varchar) +
''Where [1] = ''+ Cast (@ r as varchar)
Exec (@ SQL)

Select @ len2 = @ len2-1
Select @ I = @ I + 1

End

If @ s3/2% 2 = 1
Select @ S1 = ''+''
Else
Select @ S1 = ''-''

If @ S2 = 'C''
Select @ S2 = ''r''
Else
Select @ S2 = 'C''

Select @ S3 = @ S3 + 1
End

-- Select * from # T
'

Select @ print ='
Declare @ string varchar (8000)
Select @ string = ''''
Declare @ conn varchar (1000)
Select @ conn = ''''
Declare @ II int
Select @ II = SQRT ('+ Cast (@ num as varchar) + ')
Declare @ M int
Select @ M = 1
Declare @ n int
Select @ n = 1
While (@ II >=@ N)
Begin
Select @ string = ''declare @ conn varchar (1000)
Select @ conn =''
Select @ M = 1

While (@ II >=@ m)
Begin
Select @ string = @ string + ''right (space (LEN (cast ('+ Cast (@ num as varchar) +' As varchar ))) + Cast ([''+ Cast (@ M as varchar) +''] As varchar), Len (cast ('+ Cast (@ num as varchar) + 'as varchar) + 1) +''
Select @ M = @ m + 1
End
Select @ string = reverse (stuff (reverse (@ string ,''''))
Select @ string = @ string + ''from # t where [1] ='' + Cast (@ n as varchar) +''
Print @ conn''
Exec (@ string)
Select @ n = @ n + 1
End
'
Exec (@ table + @ insert + @ update + @ print)

End

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.