SQL 노트 [장기 업데이트] (- 2013.7)

14418 단어 sql
--IF EXISTS(SELECT  * FROM dbo.SysObjects WHERE ID = object_id(N'[TABLEA]') ) DROP TABLE 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 :
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 'III'
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 'VII'
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
,
from TABLEA a
*/


/*
--
SELECT CAST(CONVERT(VARCHAR(20), DATEADD(MONTH,-1, DATEADD(DAY,1-DATEPART(DAY,GETDATE()),DATEADD(MONTH, 1,GETDATE()))) ,112) AS DATETIME)
--
SELECT CAST(CONVERT(VARCHAR(20),DATEADD(DAY,-DATEPART(DAY,GETDATE()),DATEADD(MONTH, 1,GETDATE())) ,112) AS DATETIME)
--
SELECT CAST(CONVERT(VARCHAR(20), DATEADD(DAY,1-DATEPART(DAY,GETDATE()),DATEADD(MONTH, 1,GETDATE())) ,112) AS DATETIME)
*/




/* -- :
create proc [SP_ B] (
@ID1 VARCHAR(400)=''
,@ID2 VARCHAR (400)='sdf7fgef-f4b0-4sfe-8ecb-cbgefb0fcfgh' --''
,@RESULT VARCHAR(100) output
)
as

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

WHILE (@ <=9) --(CONVERT(int,@i))
BEGIN
SET @RESULT_temp=''
EXEC SP_ A @ 1=@ID1,@ 2=@ID2,@ =@ ,@RESULT=@RESULT_temp OUTPUT
IF @RESULT_temp IS NOT NULL AND @RESULT_temp <> ''
BEGIN
IF @RESULT='' --@ =1
BEGIN
SET @RESULT=@RESULT_temp
END
ELSE
BEGIN
SET @RESULT+=','+@RESULT_temp
END
END
SET @ +=1
END
END
*/



/* -- SQL , ( sql , )
DECLARE @ID VARCHAR(50)='JXsfge' -- , , @ID

DECLARE @i VARCHAR(400) =1
DECLARE @ 1 VARCHAR(20)=' 1'
DECLARE @ 2 VARCHAR(20)=' 2'
DECLARE @ 3 VARCHAR(20)=' 3'
DECLARE @ 11 VARCHAR(20)=' 11'
DECLARE @ 22 VARCHAR(20)=' 22'
DECLARE @ 33 VARCHAR(20)=' 33'
DECLARE @ C VARCHAR(20)=''
DECLARE @CNAME VARCHAR(20)=@ID
DECLARE @ temp VARCHAR(400) =''
DECLARE @ INT =''

DECLARE @ROWS VARCHAR(400) =''
EXEC SP_ C @ =@ID ,@ROWS1=@ROWS OUTPUT -- " "

-- @t_TableA、@t_TableX
declare @t_TableA table(j VARCHAR(10), VARCHAR(50), VARCHAR(50), VARCHAR(10), A VARCHAR(50),CISLATEST VARCHAR(50))
declare @t_TableX table(j VARCHAR(10), VARCHAR(50), VARCHAR(50), VARCHAR(10), A VARCHAR(50), VARCHAR(50),
VARCHAR(50), VARCHAR(500))
--
WHILE (CONVERT(int,@i))=(CONVERT(int,@ROWS))--@i=@ROWS
BEGIN
PRINT @i

INSERT INTO @t_TableA (j, , , , A,CISLATEST)(
SELECT TOP 1 * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY TABLEB. DESC,TABLEB. A ASC) AS j ,
CASE WHEN TABLEB. =' ' THEN 'COM'
WHEN TABLEB. =' ' THEN 'EN'
WHEN TABLEB. =' ' THEN 'EM'
WHEN TABLEB. =' ' THEN 'ITRADE'
WHEN TABLEB. =' ' THEN 'M'
WHEN TABLEB. =' ' THEN 'C'
WHEN TABLEB. =' ' THEN 'SYS'
ELSE '' END AS ,
'1' AS ,TABLEB.IDA AS A,TABLEB.CISLATEST
FROM TABLEB
WHERE TABLEB. A = @ID AND TABLEB.CISLATEST='1'
) AS m1 WHERE m1.j=(CONVERT(int,@i))
)

-- ,@
SET @ A=(SELECT TOP 1 A FROM @t_TableA WHERE j=(CONVERT(int,@i)))
PRINT @ A

IF(@ temp =(SELECT TOP 1 FROM @t_TableA WHERE j=(CONVERT(int,@i))))
BEGIN
SET @ +=1
END
ELSE
BEGIN
SET @ =1
SET @ temp=(SELECT TOP 1 FROM @t_TableA WHERE j=(CONVERT(int,@i)))
END

SET @ 1=''
SET @ 2=''
SET @ 3=''
SET @ 11=''
SET @ 22=''
SET @ 33=''

EXEC SP_ B @ =@ 1,@CNAME=@CNAME,@ =@ 11 OUTPUT,@ =''
EXEC SP_ B @ =@ 2,@CNAME=@CNAME,@ =@ 22 OUTPUT,@ =''
EXEC SP_ B @ =@ 3,@CNAME=@CNAME,@ =@ 33 OUTPUT,@ =@ C OUTPUT --

DELETE FROM @t_TableA --

PRINT ' 11 :'+@ 11+' 22:'+@ 22+' 33:'+@ 33


--
BEGIN
INSERT INTO @t_TableX (j, , , , A , , )(
SELECT TOP 1 * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY TABLEB. DESC,TABLEB. A ASC) AS j ,
CASE WHEN TABLEB. =' ' THEN 'COM'
WHEN TABLEB. =' ' THEN 'EN'
WHEN TABLEB. =' ' THEN 'EM'
WHEN TABLEB. =' ' THEN 'ITRADE'
WHEN TABLEB. =' ' THEN 'M'
WHEN TABLEB. =' ' THEN 'C'
WHEN TABLEB. =' ' THEN 'SYS'
ELSE '' END AS ,
'1' AS ,TABLEB.IDA AS A,TABLEB.CISLATEST,
@ 11+@ 22+@ 33
FROM TABLEB
WHERE = @ID AND TABLEB.CISLATEST='1'
) AS m1 WHERE m1.j=(CONVERT(int,@i))
)
END
SET @i+=1

END

SELECT j, , , , A , , FROM @t_TableX

*/


 
다운로드 주소: http://files.cnblogs.com/jx270/SQL.rar

좋은 웹페이지 즐겨찾기