SQL Notes [long-term updates] (

Source: Internet
Author: User
SQL Notes [long-term update] (-2013.7) -- tableA -- CREATETABLETABLEA (-- Column1VARCHAR (20), -- Column2VARCHAR (20), -- Column3VARCHAR (20), -- Column4int, -- Column5VARCHAR (20), -- Column6DATETIME --) * -- ROW_NUMBER () OVER (), use of casewhen

SQL Notes [long-term updates] (-2013.7) -- tableA -- CREATE TABLE TABLEA (-- Column1 VARCHAR (20), -- Column2 VARCHAR (20), -- Column3 VARCHAR (20 ), -- Column4 int, -- Column5 VARCHAR (20), -- Column6 DATETIME --)/* -- ROW_NUMBER () OVER (), use case when

SQL Notes [long-term updates] (-2013.7)

-- TableA

-- Create table tablea (
-- Column1 VARCHAR (20 ),
-- Column2 VARCHAR (20 ),
-- Column3 VARCHAR (20 ),
-- Column4 int,
-- Column5 VARCHAR (20 ),
-- Column6 DATETIME
--)
/*
-- ROW_NUMBER () OVER (), case when usage:
Select ROW_NUMBER () OVER (Order by a. Column1, a. Column2) AS rowid
, Case when a. Column3 is not null then (
Case when a. Column3 = '1' AND a. Column4 is not null and a. Column4> 0 THEN 'I'
WHEN a. Column3 = '2' AND a. Column4 is not null and a. Column4> 0 THEN 'II'
WHEN a. Column3 = '3' AND a. Column4 is not null and a. Column4> 0 THEN 'II'
WHEN a. Column3 = '4' AND a. Column4 is not null and a. Column4> 0 THEN 'iv'
WHEN a. Column3 = '5' AND a. Column4 is not null and a. Column4> 0 THEN 'V'
WHEN a. Column3 = '6' AND a. Column4 is not null and a. Column4> 0 THEN 'vi'
WHEN a. Column3 = '7' AND a. Column4 is not null and a. Column4> 0 THEN '7'
WHEN a. Column3 = '8' AND a. Column4 is not null and a. Column4> 0 THEN 'viii'
WHEN a. Column3 = '9' AND a. Column4 is not null and a. Column4> 0 THEN 'ix'
ELSE ''end -- + '(' + CONVERT (VARCHAR (20), a. Column4) + ')'
) ELSE ''end AS type number
,
From TABLEA
*/


/*
-- The first day of this month
Select cast (CONVERT (VARCHAR (20), DATEADD (MONTH,-1, DATEADD (DAY, 1-DATEPART (DAY, GETDATE (), DATEADD (MONTH, 1, GETDATE (), 112) as datetime)
-- Last day of this month
Select cast (CONVERT (VARCHAR (20), DATEADD (DAY,-DATEPART (DAY, GETDATE (), DATEADD (MONTH, 1, GETDATE (), 112) as datetime)
-- The first day of next month
Select cast (CONVERT (VARCHAR (20), DATEADD (DAY, 1-DATEPART (DAY, GETDATE (), DATEADD (MONTH, 1, GETDATE (), 112) as datetime)
*/




/* -- Example of complex stored procedures:
Create proc [SP _ use B for complex stored procedures] (
@ ID1 VARCHAR (400) =''
, @ ID2 VARCHAR (400) = 'sdf7fgef-f4b0-4sfe-8ecb-cbgefb0fcfgh '--''
, @ Result varchar (100) output
)
As

BEGIN
DECLARE @ row number INT = 1
DECLARE @ RESULT_temp VARCHAR (100) =''
SET @ RESULT =''

WHILE (@ row number <= 9) -- (CONVERT (int, @ I ))
BEGIN
SET @ RESULT_temp =''
Exec sp _ complex stored procedures use A @ number 1 = @ ID1, @ Number 2 = @ ID2, @ number of types = @ row number, @ RESULT = @ RESULT_temp OUTPUT
IF @ RESULT_temp is not null and @ RESULT_temp <>''
BEGIN
IF @ RESULT = ''-- @ row number = 1
BEGIN
SET @ RESULT = @ RESULT_temp
END
ELSE
BEGIN
SET @ RESULT + = ',' + @ RESULT_temp
END
END
SET @ row number + = 1
END
END
*/



/* -- The following is a relatively long SQL statement until the end (which involves some uncommon SQL statements and is worth adding to the favorites)
DECLARE @ idnumber VARCHAR (50) = 'jxsfge' -- when a report file is placed, the Hong Kong virtual host, this line of comment, server space, that is, @ idnumber is used as a parameter.

DECLARE @ I VARCHAR (400) = 1
DECLARE @ parameter type 1 VARCHAR (20) = 'xx 1'
DECLARE @ parameter type 2 VARCHAR (20) = 'xx 2'
DECLARE @ parameter Type 3 VARCHAR (20) = 'xx 3'
DECLARE @ parameter Type 11 VARCHAR (20) = 'xx 11'
DECLARE @ parameter type 22 VARCHAR (20) = 'xx 22'
DECLARE @ parameter type 33 VARCHAR (20) = 'xx 33'
DECLARE @ No. c varchar (20) =''
DECLARE @ cname varchar (20) = @ ID
DECLARE @ professional temp VARCHAR (400) =''
DECLARE @ No. INT =''

DECLARE @ rows varchar (400) =''
Exec sp _ use C @ A number = @ idnumber for complex stored procedures, @ ROWS1 = @ rows output -- the OUTPUT "number of ROWS" can be obtained.

-- Declare a temporary table @ t_TableA and @ t_TableX
Declare @ t_TableA table (j VARCHAR (10), Mark VARCHAR (50), professional VARCHAR (50), serial number VARCHAR (10), a varchar (50 ), cislatest varchar (50 ))
Declare @ t_TableX table (j VARCHAR (10), Mark VARCHAR (50), professional VARCHAR (50), serial number VARCHAR (10), a varchar (50 ), VARCHAR (50 ),
Remarks VARCHAR (50); English name: VARCHAR (500 ))
-- Start Loop
WHILE (CONVERT (int, @ I) = (CONVERT (int, @ ROWS) -- @ I = @ ROWS
BEGIN
PRINT @ I

Insert into @ t_TableA (j, Mark, Major, serial number, A, CISLATEST )(
Select top 1 * FROM (
SELECT ROW_NUMBER () OVER (order by tableb. Professional DESC, TABLEB. a asc) AS j,
Case when tableb. Major = 'computer 'then' COM'
When tableb. Major = 'then'
When tableb. Major = 'ecommerce 'then' EM'
When tableb. Major = 'International trader' THEN 'itrade'
When tableb. Major = 'statistical analysts'
When tableb. Major = 'city builder' THEN 'C'
When tableb. Major = 'System maintenance 'then' SYS'
ELSE ''end AS major,
'1' AS number, TABLEB. ida as a number A, TABLEB. CISLATEST
FROM TABLEB
Where tableb. A number A = @ id and tableb. CISLATEST = '1'
) AS m1 WHERE m1.j = (CONVERT (int, @ I ))
)

-- Call method. @ time indicates the return value.
SET @ A number A = (select top 1 a from @ t_TableA WHERE j = (CONVERT (int, @ I )))
PRINT @

IF (@ professional temp = (select top 1 Professional FROM @ t_TableA WHERE j = (CONVERT (int, @ I ))))
BEGIN
SET @ serial number + = 1
END
ELSE
BEGIN
SET @ No. = 1
SET @ professional temp = (select top 1 Professional FROM @ t_TableA WHERE j = (CONVERT (int, @ I )))
END

SET @ parameter type 1 =''
SET @ parameter type 2 =''
SET @ parameter Type 3 =''
SET @ parameter Type 11 =''
SET @ parameter type 22 =''
SET @ parameter type 33 =''

Exec sp _ complex stored procedures use B @ type = @ parameter Type 1, @ CNAME = @ CNAME, @ time = @ parameter Type 11 OUTPUT, @ A number =''
Exec sp _ complex stored procedures use B @ type = @ parameter type 2, @ CNAME = @ CNAME, @ time = @ parameter type 22 OUTPUT, @ A number =''
Exec sp _ complex stored procedures use B @ type = @ parameter Type 3, @ CNAME = @ CNAME, @ time = @ parameter type 33 OUTPUT, @ A number = @ No. c output -- multiple results can be OUTPUT at the same time

Delete from @ t_TableA -- clear a temporary table

PRINT 'parameter Type 11: '+ @ parameter Type 11 +' parameter type 22: '+ @ parameter type 22 +' parameter type 33: '+ @ parameter type 33


-- Display all directly
BEGIN
Insert into @ t_TableX (j, Mark, professional, serial number, A, remarks )(
Select top 1 * FROM (
SELECT ROW_NUMBER () OVER (order by tableb. Professional DESC, TABLEB. a asc) AS j,
Case when tableb. Major = 'computer 'then' COM'
When tableb. Major = 'then'
When tableb. Major = 'ecommerce 'then' EM'
When tableb. Major = 'International trader' THEN 'itrade'
When tableb. Major = 'statistical analysts'
When tableb. Major = 'city builder' THEN 'C'
When tableb. Major = 'System maintenance 'then' SYS'
ELSE ''end AS major,
'1' AS sequence number, TABLEB. ida as a number A, TABLEB. CISLATEST,
@ Parameter Type 11 + @ parameter type 22 + @ parameter type 33
FROM TABLEB
WHERE serial number = @ id and tableb. CISLATEST = '1'
) AS m1 WHERE m1.j = (CONVERT (int, @ I ))
)
END
SET @ I + = 1

END

SELECT j, Mark, Major, serial number, A, A name, remarks FROM @ t_TableX

*/


:

Posted on

, Hong Kong VM

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.