[prog] SQL - MSSQL2020. 12. 18. 09:21

참고 : docs.microsoft.com/ko-kr/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver15

 

 

-- 실행 1
select max(yearlyIncome) as 최고수입
from   DimCustomer (nolock)

/***********************
실행 1 의 결과 
   최고수입
-----------
1  170000.00
***********************/

----------------------------
-- 실행 2
declare @yIncome float
select @yIncome = max(yearlyIncome)
from   DimCustomer (nolock)

select @yIncome as 최고수입

----------------------------
-- 실행 3
declare @yIncome numeric
select @yIncome = max(yearlyIncome)
from   DimCustomer (nolock)

select @yIncome as 최고수입

----------------------------
-- 실행 4
declare @yIncome decimal(10, 2)
select @yIncome = max(yearlyIncome)
from   DimCustomer (nolock)

select @yIncome as 최고수입

----------------------------
-- 실행 5
declare @yIncome numeric(10, 2)
select @yIncome = max(yearlyIncome)
from   DimCustomer (nolock)

select @yIncome as 최고수입

 

 

결과 2, 3
   최고수입
-------------
1  170000

--------------------------------------------

결과 4, 5
   최고수입
-------------
1  170000.00

 

Posted by dawnawaker
[prog] SQL - MSSQL2020. 11. 4. 13:14

 

Contents at a Glance                                                          
Chapter 1 Select: Single Table  
Chapter 2 Where 
Chapter 3 Order By  
Chapter 4 Select: Multiple Tables 
Chapter 5 Aggregates  
Chapter 6 Select: New Tables  
Chapter 7 Except/Intersect/Union  
Chapter 8 Group By  
Chapter 9 System Functions  
Chapter 10 Subqueries 
Chapter 11 Delete/Insert/Update 
Chapter 12 Views/User-Defined Functions 
Chapter 13 Stored Procedures/Programming  
Chapter 14 Data Definition Language (DDL) and Data Control Language (DCL) 
Chapter 15 After You Finish 
Index

1. 사용할 데이터베이스 다운로드

   - 마이크로소프트에서 제공하는 테스트용 DB 를 다운로드 받는다.
   - 구글에서 adventureworksDW2008 database download 검색

 

 

 

 

2. 다운로드 받은 백업 DB 를 SSMS 에서 설치

------------------------------------------------------

Chapter 1 Select: Single Table 

 

 

 

Chapter 11  Delete / Insert / Update 

 

○ Select Into 
   기존에 없는 새 테이블을 만들어서 데이터 입력 
   기존에 있는 테이블에 데이터를 넣으려면 Insert Select 
select * 
into Scenario 
from DimScenario 


-- Scenario 테이블을 만들어서 
   DimScenario 테이블에 있는 모든 data 를 Scenario 테이블에 복사해 넣는다
-- 새로 만들어지는 Scenario 테이블은 PK, FK 등의 key 속성은 복사하지 않지만
   IDENTITY 속성은 복사한다.

 

DimScenario, Scenario 모두 동일

 

delete Scenario 
-------------------
select * 
into Scenario 
from DimScenario 


-- [결과] error  (데이터는 삭제되었지만 Scenario 라는 테이블이 존재하므로)

 

○ Truncate Table  : 테이블은 남기고 데이터만 삭제 
    Drop Table       : 테이블째로 삭제 
truncate table Scenario

 

 

○ Insert Into ... Select 1/3 
   - 기존에 있는 테이블에 데이터 입력 
   - Identity 칼럼이 있는 경우 주의 
insert into Scenario
select * from DimScenario


-- 결과
메시지 8101, 수준 16, 상태 1, 줄 15
테이블 'Scenario'에 있는 ID 열의 명시적 값은 열 목록이 사용되고 IDENTITY_INSERT가 ON일 때만 지정할 수 있습니다.


-- 설명
현재 Scenario 는 빈 테이블인데, 이 중 ScenarioKey 칼럼이 IDENTITY 속성.
IDENTITY 속성인 칼럼에 복사를 하려하면 위처럼 error 발생.

○ Insert Into ... Select  2/3 
   - 일부 칼럼만 복사
insert into Scenario
select ScenarioName from DimScenario


-- 결과
3 rows affected


-- 설명
존재하고 있는 Scenario 테이블에 DimScenario 테이블로 부터 ScenarioName 칼럼을 복사해 넣는다

 

○ Insert Into ... Select 3/3 
   - 일부 칼럼만 복사 
   - source 칼럼과 destination 칼럼 모두 명시
insert into Scenario (ScenarioName) 
select ScenarioName from DimScenario


-- 설명
존재하고 있는 Scenario 테이블의 ScenarioName 칼럼에
DimScenario 테이블로 부터 ScenarioName 칼럼을 복사해 넣는다

 

결과 화면

 

○ delete 테이블명 / truncate table 테이블명  차이
  - delete    : identity 칼럼 번호를 계속 이어간다
  - truncate : identity 칼럼 번호를 1 로 리셋
-- DELETE / truncate 차이

drop table Scenario

select * 
into Scenario 
from DimScenario 


select * from DimScenario

select * from Scenario

delete Scenario
-- truncate table Scenario  -- 이 부분만 바꾸어서 실행

insert into Scenario
select ScenarioName from DimScenario

select * from Scenario
delete 결과 truncate 결과

마지막 테이블 ScenarioKey 칼럼 4 부터 시작

마지막 테이블 ScenarioKey 칼럼 1 부터 시작

 

 

○ Delete 를 하고 identity 번호를 다시 1 부터 넣는 방법
  - dbcc checkident()
drop table Scenario

select * 
into Scenario 
from DimScenario ;

select * from DimScenario;  --  첫번째 결과

select * from Scenario;     --  두번째 결과

delete Scenario;

dbcc checkident(Scenario, reseed, 0);

insert into Scenario
select ScenarioName from DimScenario;

select * from Scenario;     -- 세번째 결과

 

identity 칼럼은 일반적인 insert 문이나 update 문으로는 insert 도, update 도 할 수 없다.

 

 

○ Identity 칼럼에 특정 숫자를 insert 하자
  - set identity_insert Scenario on   (off 로 하면 다시 원래대로 못쓰게 함)
  - 이때, 원하는 칼럼리스트를 넣어주어야 한다.
set identity_insert Scenario on
--
insert into Scenario                   -- 실패 (칼럼명이 없다)
values(90,'Forecast 2011')
--
insert into Scenario (ScenarioKey, ScenarioName)  -- 성공 (칼럼명이 있다)
values(99,'Forecast 2011')
--
set identity_insert Scenario off       -- 다시 원래대로
--
select * from Scenario

두 번째 쿼리의 결과

 

○ Identity 칼럼은 중간에 번호가 비어도 마지막 번호부터 이어간다.
select * from [dbo].[Scenario]        -- 결과 1
-------------------
insert into Scenario (ScenarioName)
values ('Forecast 2012')

insert into Scenario (ScenarioName)
values ('Forecast 2013')
-------------------
select * from Scenario                 -- 결과 2

 

◆◆◆◆◆◆◆◆◆◆◆◆◆

◆◆◆◆◆◆◆◆◆◆◆◆◆

 

Chapter 12  Vuews / User-Defined Functions

** 여기서는 table function 은 다루지 않고 scalar function 만 다룬다.

1. Join Select 문을 View 로

-- 테이블 3개를 join 한 select 문

select EnglishProductName, EnglishProductSubcategoryName, EnglishProductCategoryName 
from DimProduct                  as P
inner join DimProductSubcategory as S
   on P.ProductSubcategoryKey = S.ProductSubcategoryKey
inner join DimProductCategory    as C
   on S.ProductCategoryKey = C.ProductCategoryKey
where EnglishProductCategoryName = 'Bikes' or
      EnglishProductCategoryName = 'Accessories'

 

1. View 만들기
  - Create View 뷰이름 as + select 문
-- 위 SQL 문을 VIEW 로 만든다.

create view MyView as            -- 이 부분만 새로 들어간다
select EnglishProductName, EnglishProductSubcategoryName, EnglishProductCategoryName 
from DimProduct                  as P
inner join DimProductSubcategory as S
   on P.ProductSubcategoryKey = S.ProductSubcategoryKey
inner join DimProductCategory    as C
   on S.ProductCategoryKey = C.ProductCategoryKey
where EnglishProductCategoryName = 'Bikes' or
      EnglishProductCategoryName = 'Accessories'

 

 

2. 위에서 만들어진 VIEW 를 테이블처럼 사용한다 - SELECT
select * from MyView

 

3. VIEW 수정
  - Alter view 뷰이름 as + select 문
-- 위 VIEW 를 수정한다.

alter view MyView as             -- Alter VIEW
select EnglishProductName, EnglishProductSubcategoryName, EnglishProductCategoryName 
from   DimProduct                as P
inner join DimProductSubcategory as S
        on P.ProductSubcategoryKey = S.ProductSubcategoryKey
inner join DimProductCategory    as C
        on S.ProductCategoryKey    = C.ProductCategoryKey
where EnglishProductCategoryName = 'Clothing' or
      EnglishProductCategoryName = 'Components'
--
select * from MyView

 

 

4. VIEW 삭제
  - Drop view 뷰이름
drop view MyView

 

 

◆◆◆◆◆◆◆◆◆◆◆◆◆

 

◆ User-Defined Function

1. 함수 만들기
  - create function 함수명(@파라미터  데이터타입)
    returns 데이터타입
    as
    begin
      함수수식
      return
    end

-- 테스트를 위한 base 쿼리를 만든다.

select EnglishProductName as [Product], round(ListPrice,2) as [Price],
       ceiling(ListPrice * 0.15) as [Tax] 
from   DimProduct
where  ListPrice is not null

** 여기서 Tax 구하는 부분을 함수로 만들어본다.

 

use AdventureWorksDW2008R2
go

create function Tax (@price money)
returns int
as
begin
  return ceiling(@price * 0.15)
end

-- 입력 파라미터 의 데이터타입 : money

    리턴 파라미터의 데이터타입 : int  (그대로 money 써도 되지만 연습을 위해)

 

2. 함수 사용
  - select 스키마명.함수(입력파라미터)
select dbo.Tax(20)

-- select Tax(20)  : error

 

select EnglishProductName as [Product], round(ListPrice,2) as [Price],
        dbo.Tax(ListPrice) as Tax 
from   DimProduct         -- 함수 사용
where  ListPrice is not null

 

3. 함수 삭제
  - Drop function 함수명
drop function Tax   -- 위 예제에서 사용한 Tax 함수를 삭제한다

 

 

4. 예제]  정수 3개를 받아서 모두 곱한 값을 리터하는 함수(CalculateVolume) 작성
create function CalculateVolume (@x int, @y int, @z int)
returns int
as
begin
  return @x * @y * @z
end
--
select dbo.CalculateVolume(12,5,3) as Volume       -- 결과 1
--
select dbo.CalculateVolume(12.9,5,3) as Volume     -- 결과 2

-- [모두 확인후 함수 삭제]
drop function CalculateVolume

 

위 결과 1 과 결과 2 는 모두 180 이다.

 - 12.9 * 5 * 3 = 193.5

 - 입력값 데이터 타입이 모두 int 이므로 소수점 이하는 모두 잘라서 입력된다.

 

 

◆◆◆◆◆◆◆◆◆◆◆◆◆

◆◆◆◆◆◆◆◆◆◆◆◆◆

 

Chapter 13  Stored Procedure / Programming

 

1. WHILE 문
  - END WHILE 은 없다
  - Begin ... End 는 가능
-- begin ... end 없는 경우

declare @counter int

set @counter = 1
while @counter < 3
set @counter = @counter + 1
print @counter

-- 결과

   3

-- 설명  : 아래 두 문장이 한 세트 / print @counter 는 while 에 속하지 않는다.
   while @counter < 3
   set @counter = @counter + 1

 

 

-- begin ... end 있는 경우

declare @counter int
set @counter = 1
print @counter

while @counter < 3
begin
  set @counter = @counter + 1
  print @counter
end

-- 결과

   1

   2

   3

 

2. RETURN
  - 프로그램실행을 끝낸다.
print 'starting'
return
print 'finished'

-- 결과

  starting

 

 

3. If ... Else
  - Then / End If 없음
프로그램실행을 끝낸다.
if 1 <> 0
print 'true'
print 'got here'
else
print 'false'

-- 결과

   error

-- 설명

   if 1 <> 0 에는 print 'true' 만 적용된다.  따라서 else 부분이 문제가 된다. 

   위 프로그램을 실행시키려면 Begin ... End 를 사용한다.

 

if 1 <> 0
begin
  print 'true'
  print 'got here'
end
else
  print 'false'
  
-- 결과
-- true
-- got here     
if 1 <> 0
begin
print 'true'
print 'got here'
end
else
print 'false'
print 'got here too'


/* ----------------------
결과
true
got here
got here too
---------------------- */

여기서 if 문은 begin ~ print 'false' 까지.

 

if 1 <> 0
begin
print 'true'
print 'got here'
end
else
begin
print 'false'
print 'got here too'
end

/* ----------------------
결과
true
got here
---------------------- */

 

 

4. Case ... When ... End
  - Then 사용 가능
declare @minute tinyint
set @minute = datepart(mi,getdate())
declare @OddEven bit
set @OddEven = @minute %2
select
case @OddEven
when 0 then 'Even'
when 1 then 'Odd'
else 'Unknown'
end as [Minute]

/* ----------------------
결과
Odd  (or Even)
---------------------- */

 

 

5. Select 문에서 변수 사용
nchar(1) 타입으로 maritalstatus 라는 변수를 선언하고
이 변수에 'M' 을 넣고
DimCustomer 테이블에서 MaritalStatus 가  변수 maritalstatus 의 값을 가지는 데이터 select
declare @maritalstatus nchar(1)
set @maritalstatus = 'M'
select * from DimCustomer
where MaritalStatus = @maritalstatus

 

 

Stored Procedure 로 넘어가기 위한 Base 쿼리

 

select FirstName + ' ' + LastName as [Full Name], MaritalStatus, Gender
from DimCustomer

 

1. Create Proc 프로시저명 as
   Begin
     ...
   End
-- first stored procedure
create proc GetCustomers as
select FirstName + ' ' + LastName as [Full Name], MaritalStatus, Gender
from DimCustomer


-- better
create proc GetCustomers as
begin
select FirstName + ' ' + LastName as [Full Name], MaritalStatus, Gender
from DimCustomer
end

 

 

2. Alter Proc 프로시저명 as
   Begin
     ...
   End
alter proc GetCustomers as
begin
-- [Full Name] 을 Fullname 으로 변경
select FirstName + ' ' + LastName as [Fullname], MaritalStatus, Gender
from DimCustomer
end

 

3. 실행 : exec procedure명
alter proc GetCustomers as
begin
select FirstName + ' ' + LastName as [Full Name], MaritalStatus, Gender
from DimCustomer
where MaritalStatus = 'S' and Gender = 'F'
end

--------------------------

-- 실행
exec GetCustomers

 

 

4. procedure 에 내부 변수 사용
-- where 절에 변수 사용
alter proc GetCustomers as
begin

declare @maritalstatus nchar(1)  -- 내부 변수
declare @gender nvarchar(1)      -- 내부 변수
set @maritalstatus = 'S'
set @gender = 'F'

select FirstName + ' ' + LastName as [Full Name], 
       MaritalStatus, 
       Gender
from   DimCustomer
where  MaritalStatus = @maritalstatus 
       and Gender = @gender
end


-- 실행
exec GetCustomers

/* --------------------------------
결과 :
MaritalStatus = S 이고 Gender = F 인 row 출력
-------------------------------- */

 

 

5. procedure 에 input parameter 사용
alter proc GetCustomers
@maritalstatus nchar(1),  -- 외부 변수
@gender nvarchar(1)       -- 외부 변수
as
begin

select FirstName + ' ' + LastName as [Full Name], 
       MaritalStatus, 
       Gender
from   DimCustomer
where  MaritalStatus = @maritalstatus 
       and Gender = @gender
	   
end


-- 실행 1
exec GetCustomers         -- error

-- 실행 2
exec GetCustomers 'S', 'F'

/* ------------------------
실행 2 의 결과 :
MaritalStatus = S  Gender = F 인 row select
/* ------------------------

 

 

6. procedure 에 input parameter 사용  +  변수에 default 값 주기
alter proc GetCustomers
@maritalstatus nchar(1) = 'S',
@gender nvarchar(1) = 'F'
as
begin

select FirstName + ' ' + LastName as [Full Name], 
       MaritalStatus, 
       Gender
from   DimCustomer
where  MaritalStatus = @maritalstatus 
       and Gender = @gender

end


-- 실행 1
exec GetCustomers


-- 실행 2
exec GetCustomers 'M', 'M'

/* ---------------------------
실행 1 결과 :
default 로 설정된 F / S 값이 들어간 row select

실행 2 결과 :
입력된 파라미터 M /M 값이 들어간 row select
--------------------------- */

 

 

7.  Output Parameter 사용 
alter proc GetCustomers
@maritalstatus nchar(1) = 'S',
@gender nvarchar(1) = 'F',
@count int out                  -- out parameter 선언
as
begin

select FirstName + ' ' + LastName as [Full Name], 
       MaritalStatus, 
       Gender
from   DimCustomer
where  MaritalStatus = @maritalstatus 
       and Gender = @gender

set @count = @@ROWCOUNT

end


/*******************************

-- 실행 1
exec GetCustomers

-- 실행 2
exec GetCustomers 'M', 'F'

-- 실행 3
exec GetCustomers @count out

-- 실행 4
declare @count int
exec GetCustomers 'M', 'M', @count out
select @count as [Records]

-- 실행 5  p. 265
declare @count int
exec GetCustomers , , @count out
select @count as [Records]

-- 실행 6  p. 265
declare @count int
exec GetCustomers default, default, @count out
select @count as [Records]

-- 실행 7  p. 265
declare @count int
declare @return int
exec @return = GetCustomers default, default, @count out
select @count as [Records]
select @return as [Return]

*******************************/

 

실행 결과

-- 결과 1
프로시저 또는 함수 'GetCustomers'에 매개 변수 '@count'이(가) 필요하지만 제공되지 않았습니다.

-- 결과 2
프로시저 또는 함수 'GetCustomers'에 매개 변수 '@count'이(가) 필요하지만 제공되지 않았습니다.

-- 결과 3
스칼라 변수 "@count"을(를) 선언해야 합니다.

-- 결과 4
   Records
----------
1  5266

-- 결과 5
',' 근처의 구문이 잘못되었습니다.

-- 결과 6
   Records 
---------- 
1  4388 


-- 결과 7
   Records 
------------ 
1  4388 

   Return 
------------ 
1  0 

 

 

8.  Output Parameter + Return  in Procedure
-- p. 267
alter proc GetCustomers
@maritalstatus nchar(1) = 'S',
@gender nvarchar(1) = 'F',
@count int out
as
begin

declare @err int

select FirstName + ' ' + LastName as [Full Name], 
       MaritalStatus, 
       Gender
from   DimCustomer
where  MaritalStatus = @maritalstatus 
       and Gender = @gender

set    @count = @@ROWCOUNT

if @count = 0
  return -99
else
  return 0

end


/****************************************
declare @count int
declare @return int
exec @return = GetCustomers 'X', 'Y', @count out
-- X, Y 를 넣으면 row 는 0개 select
select @count as [Records]
select @return as [Return]
****************************************/

 

 

결과 :

   Records 
------------ 
1  0 

   Return 
------------ 
1  -99

 

 

9. Procedure 에서 다른 Procdure 호출
-- p. 268
create proc CallGetCustomers as
begin

declare @count int
declare @return int

exec @return = GetCustomers 'S', 'F', @count out

select @count as [Records]
select @return as [Return]

end


/***********************************
실행 :
exec CallGetCustomers
***********************************/

 

결과 :

   Records 
------------ 
1  4388

   Return 
------------ 
1  0

 

 

10. Try ... Catch  

 

-- p. 269
alter proc CallGetCustomers as
begin

declare @count int
declare @return int

begin try
  exec @return = GetCustomers 'S', 'F', @count out
  // GetCustomers 'S', 'F', @count out 호출하면 리턴값 = 0
end try

begin catch
  set @count = 0
  set @return = -100

  print ERROR_MESSAGE()
end catch

select @count as [Records]
select @return as [Return]

end


/******************************
exec CallGetCustomers
******************************/

 

결과 :

   Records 
------------ 
1  4388

   Return 
------------ 
1  0

설명 : Try block 에서 문제가 발생하면 Catch block 이 trigger 된다

 

만일 GetCustomers 에서 from   DimCustomer 을  from   XXXX 라고 하고 실행하면

결과 :

   Return 
------------ 
1  -100

 

 

11. Procedure 호출부분에 input parameter 넣어주기

 

-- p. 271

alter proc CallGetCustomers
@maritalstatus nchar(1)    = 'S',
@gender        nvarchar(1) = 'F'
as
begin

declare @count int
declare @return int

begin try
  -- input parameter 에 상수가 아닌 변수를 넣어준다
  exec @return = GetCustomers @maritalstatus, @gender, @count out
end try

begin catch
  set @count = 0
  set @return = -100

  print ERROR_MESSAGE()
end catch

select @count as [Records]
select @return as [Return]

end


/*****************************************
-- 실행 1
exec CallGetCustomers 'X', 'Y'  -- 결과 row 개수 = 0

-- 실행 2
exec CallGetCustomers default, default

-- 실행 3
exec CallGetCustomers 'S', 'M'
*****************************************/

 

결과 1 :
   Records 
------------ 
1  0

   Return 
------------ 
1  -99
--------------------------------

결과 2 :
   Records 
------------ 
1  4388

   Return 
------------ 
1  0
--------------------------------

결과 3 :
   Records 
------------ 
1  4085

   Return 
------------ 
1  0


 

Posted by dawnawaker
[prog] SQL - MSSQL2020. 10. 15. 15:30

SQL SERVER / MS SQL / SSMS / SQL SERVER MANAGEMENT STUDIO / tips (v. 18.5)

 

  • 에디터
  1. 라인 번호 넣기 :
    도구  >  옵션  >  텍스트 편집기  >  Transact-SQL  >  일반  >  ☑  줄 번호 
  2. 블럭 선택 : Alt 누른채 마우스 드래그

 

 

Posted by dawnawaker
[prog] SQL - MSSQL2020. 10. 11. 02:26

From blog.naver.com/diceworld/220156570441

 

- 기준날짜

​   2000년 01월 02일 13시 14분 15.678초 기준 날짜 형변환

 

 - MSSQL 날짜 변환표 (기준날짜를 대상으로 CONVERT 실행하여 날짜 형변환)

번호

쿼리

 결과 코드

 0

 CONVERT(CHAR(19), DATETIME, 0)

 01 02 2000 1:14PM  MM DD YYYY H:MM

 1

 CONVERT(CHAR(10), DATETIME, 1)

 01/02/2000

 MM/DD/YYYY

 2

 CONVERT(CHAR(8), DATETIME, 2)

 00.01.02

 YY.MM.DD

 3

 CONVERT(CHAR(8), DATETIME, 3)

 02/01/00

 DD/MM/YY

 4

 CONVERT(CHAR(8), DATETIME, 4)

 02.01.00

 DD.MM.YY

 5

 CONVERT(CHAR(8), DATETIME, 5)

 02-01-00

 DD-MM-YY

 6

 CONVERT(CHAR(8), DATETIME, 6)

 02 01 00

 DD MM YY

 7

 CONVERT(CHAR(9), DATETIME, 7)

 01 02, 00

 MM DD, YY

 8

 CONVERT(CHAR(8), DATETIME, 8)

 13:14:15

 HH:MM:SS

 9

 CONVERT(CHAR(26), DATETIME, 9)

 01 02 2000 1:14:15.678PM

 NN DD YYYY H:MM:SS.MS

 10

 CONVERT(CHAR(8), DATETIME, 10)

 01-02-00

 MM-DD-YY

 11

 CONVERT(CHAR(8), DATETIME, 11)

 02/01/00

 DD/MM/YY

 12

 CONVERT(CHAR(6), DATETIME, 12)

 000102

 YYMMDD

 13

 CONVERT(CHAR(24), DATETIME, 13)

 02 01 2000 13:14:15.678

 DD MM YYYY HH:MM:SS.MS

 14

 CONVERT(CHAR(12), DATETIME, 14)

 13:14:15.678

 HH:MM:SS.MS

 20

 CONVERT(CHAR(19), DATETIME, 20)

 2000-01-02 13:14:15

 YYYY-MM-DD HH:MM:SS

 21

 CONVERT(CHAR(23), DATETIME, 21)

 2000-01-02 13:14:15.678

YYYY-MM-DD HH:MM:SS.MS

 22

 CONVERT(CHAR(20), DATETIME, 22)

 01/02/00 1:14:15 PM

 MM/DD/YY H:M:S

 23

 CONVERT(CHAR(10), DATETIME, 23)

 2000-01-02

 YYYY-MM-DD

 24

 CONVERT(CHAR(8), DATETIME, 24)

 13:14:15

 HH:MM:SS

 25

 CONVERT(CHAR(23), DATETIME, 25)

 2000-01-02 13:14:15.678

 YYYY-MM-DD HH:MM:SS.MS

 100

 CONVERT(CHAR(19), DATETIME, 100)

 01 02 2000 1:02PM

 MM DD YYYY H:MM

 101

 CONVERT(CHAR(10), DATETIME, 101)

 01/02/2000

 MM/DD/YYYY

 102

 CONVERT(CHAR(10), DATETIME, 102)

 2000.01.02

 YYYY.MM.DD

 103

 CONVERT(CHAR(10), DATETIME, 103)

 02/01/2000

 DD/MM/YYYY

 104

 CONVERT(CHAR(10), DATETIME, 104)

 02/01/2000

 DD/MM/YYYY

 105

 CONVERT(CHAR(10), DATETIME, 105)

 02-01-2000

 DD-MM-YYYY

 106

 CONVERT(CHAR(11), DATETIME, 106)

 02 01 2000

 DD MM YYYY

 107

 CONVERT(CHAR(12), DATETIME, 107)

 01 02, 2000

 MM DD, YYYY

 108

 CONVERT(CHAR(8), DATETIME, 108)

 13:14:15

 HH:MM:SS

 109

 CONVERT(CHAR(26), DATETIME, 109)

 01 02 2000 1:14:15.678PM

 MM DD YYYY H:MM:DD.MS

 110

 CONVERT(CHAR(10), DATETIME, 110)

 01-02-2000

 MM-DD-YYYY

 111

 CONVERT(CHAR(10), DATETIME, 111)

 2000/01/02

 YYYY/MM/DD

 112

 CONVERT(CHAR(8), DATETIME, 112)

 20000102

 YYYYMMDD

 113

 CONVERT(CHAR(24), DATETIME, 113)

 02 01 2000 13:14:15.678

 DD MM YYYY HH:MM:DD.MS

 114

 CONVERT(CHAR(12), DATETIME, 114)

 13:14:15:678

 HH:MM:DD:MS

 120

 CONVERT(CHAR(19), DATETIME, 120)

 2000-01-02 13:14:15

 YYYY-MM-DD HH:MM:SS

 121

 CONVERT(CHAR(23), DATETIME, 121)

 2000-01-02 13:14:15.678

 YYYY-MM-DD HH:MM:SS.MS

 126

 CONVERT(CHAR(23), DATETIME, 126)

 2000-01-02T13:14:15.678

 YYYY-MM-DDT HH:MM:SS.MS

 127

 CONVERT(CHAR(23), DATETIME, 127)

 2000-01-02T13:14:15.678

 YYYY-MM-DDT HH:MM:SS.MS

 131

 CONVERT(CHAR(25), DATETIME, 131)

 1/06/1421 1:13:14:678PM

 

 

 [참고사항] 자주 쓰이는 변환표

 

번호

쿼리

 결과 코드

 21

 CONVERT(CHAR(23), DATETIME, 21)

 2000-01-02 13:14:15.678 YYYY-MM-DD HH:MM:SS.MS

 23

 CONVERT(CHAR(10), DATETIME, 23)

 2000-01-02

 YYYY-MM-DD

 112

 CONVERT(CHAR(8), DATETIME, 112)

 20000102

 YYYYMMDD

 

 - 예제 1

오늘 날짜를 YYYY-MM-DD 형태로 CONVERT를 이용하여 표시하시오. (2000년 01월 02일 13시 14분 15초 기준)

 

 - 예제 1 쿼리

SELECT CONVERT(CHAR(10), GETDATE(), 23) AS 날짜

 

날짜

2000-01-02

 

 - 예제 2

오늘 날짜를 YYYY-MM-DD HH:MM:SS.MS 형태로 CONVERT를 이용하여 표시하시오.   

(2000년 01월 02일 13시 14분 15초 기준)

 

 - 예제 2 쿼리

 SELECT CONVERT(CHAR(23), GETDATE(), 21) AS 날짜

 

날짜

2000-01-02 13:14:15.678

  

 [참고사항] GETDATE()

 GETDATE()함수는 오늘날짜를 연, 월, 일, 시, 분, 초, 밀리세컨드까지 갖고 오는 함수입니다.

 


 

  • 생일을 기준으로 한 만나이 계산

    도움 :  ponyozzang.tistory.com/413

 

-- 생년월일
DECLARE @BIRTHDAY DATE = '2000-11-05';

-- 나이를 알고 싶은 날짜
DECLARE @DATE DATE = GETDATE();   -- '2020-11-05'

-- 년수 계산
DECLARE @AGE  INT = (SELECT DATEDIFF(YEAR, @BIRTHDAY, @DATE));

-- 생년월일 자르기
DECLARE @D  VARCHAR(8) = CONVERT(VARCHAR(8), @DATE,     112);
DECLARE @B  VARCHAR(8) = CONVERT(VARCHAR(8), @BIRTHDAY, 112);
DECLARE @D2 VARCHAR(8) = LEFT(@D, 4) + RIGHT(@B, 4);

-- 만나이 계산
SET @AGE = CASE
	WHEN @D < @D2 THEN
		@AGE - 1
	ELSE
		@AGE
	END

-- 만나이 출력
SELECT @AGE

 

 

 

 

 

Posted by dawnawaker
[prog] SQL - MSSQL2020. 9. 17. 15:37
table comment  테이블 코멘트 설명 주석
SELECT OBJTYPE, OBJNAME, NAME, VALUE 
FROM   ::FN_LISTEXTENDEDPROPERTY(NULL, 'SCHEMA', 'DBO', 'TABLE', '테이블이름', DEFAULT, DEFAULT) ;

-- OR

SELECT OBJTYPE, OBJNAME, NAME, VALUE 
FROM   ::FN_LISTEXTENDEDPROPERTY(NULL, 'SCHEMA', 'DBO', 'TABLE', '테이블이름', 'column', DEFAULT) ;

 

 

프로시저 / 함수 내용 조회

1] sp_helptext 이용 (추천)

sp_helptext 프로시저 or 함수 이름

 

2] select 문 이용

SELECT B.text 
FROM       sys.sysobjects  AS A WITH (NOLOCK) 
INNER JOIN sys.syscomments AS B WITH (NOLOCK) ON A.id = B.id 
WHERE A.name = '프로시져 or 함수 이름' ;

 

 

칼럼명으로 테이블 찾기

 

SELECT T.name AS table_name, C.name AS column_name
FROM       sys.tables  as T
INNER JOIN sys.columns as C on T.object_id = C.object_id
WHERE  C.name = '칼럼명' ;

 

 

alternative to using COUNT(*)
 - indid : clustered index 의 개수

 

SELECT rows 
FROM   sysindexes 
WHERE  id=OBJECT_ID ('테이블명') AND indid < 2 ;

 

 

테이블의 칼럼명 추출

 

SELECT LOWER(A.COLUMN_NAME) + ','         -- 대문자로 받으려면 UPPER
FROM   INFORMATION_SCHEMA.COLUMNS A 
WHERE  A.TABLE_NAME = '테이블명' 

 

 

테이블별 데이터 건수 및 사이즈 확인

도움 : windtrap.tistory.com/7

 

SELECT Schema_name(tbl.schema_id)                                 AS [Schema],
       tbl.name,
       Coalesce((SELECT pr.name
                 FROM   sys.database_principals pr WITH (nolock)
                 WHERE  pr.principal_id = tbl.principal_id),
                 Schema_name(tbl.schema_id))                      AS [Owner],
       tbl.max_column_id_used                                     AS [Columns],
       CAST(CASE idx.index_id
              WHEN 1 THEN 1
              ELSE 0
            END AS BIT)                                           AS [HasClusIdx],
       Coalesce((SELECT SUM (spart.ROWS)
                 FROM   sys.partitions spart WITH (nolock)
                 WHERE  spart.object_id = tbl.object_id
                        AND spart.index_id < 2), 0)               AS [RowCount],
       Coalesce((SELECT CAST(v.low / 1024.0 AS FLOAT) * SUM(a.used_pages - CASE
                                         WHEN a.TYPE <> 1 THEN a.used_pages
                                         WHEN p.index_id < 2 THEN a.data_pages
                                         ELSE 0
                        END)
                 FROM   sys.indexes AS i WITH (nolock)
                        JOIN sys.partitions AS p WITH (nolock)
                          ON p.object_id = i.object_id
                             AND p.index_id = i.index_id
                        JOIN sys.allocation_units AS a WITH (nolock)
                          ON a.container_id = p.partition_id
                 WHERE  i.object_id = tbl.object_id), 0.0) / 1024 AS [IndexMB],
       Coalesce((SELECT CAST(v.low / 1024.0 AS FLOAT) * SUM(CASE
                        WHEN a.TYPE <> 1 THEN a.used_pages
                                 WHEN p.index_id < 2 THEN a.data_pages
                                 ELSE 0
                        END)
                 FROM   sys.indexes AS i WITH (nolock)
                        JOIN sys.partitions AS p WITH (nolock)
                          ON p.object_id = i.object_id
                             AND p.index_id = i.index_id
                        JOIN sys.allocation_units AS a WITH (nolock)
                          ON a.container_id = p.partition_id
                 WHERE  i.object_id = tbl.object_id), 0.0) / 1024 AS [DataMB],
       tbl.create_date,
       tbl.modify_date
FROM   sys.tables AS tbl WITH (nolock)
       INNER JOIN sys.indexes AS idx WITH (nolock)
         ON (     idx.object_id = tbl.object_id
              AND idx.index_id < 2 )
       INNER JOIN MASTER.dbo.spt_values v WITH (nolock)
         ON (     v.NUMBER = 1
              AND v.TYPE = 'E' )
--WHERE tbl.Name like '%tablename%' 
ORDER  BY 8, 2 DESC

 

 

 

일련번호 작업
-- 실행 1 : Base Query

DECLARE @P_SNUMBER varchar(11)
DECLARE @P_STRING  varchar(11) = 'myTest_'

	SET @P_SNUMBER = @P_STRING + '0001';

select @P_SNUMBER as P_SNUMBER

---------------------------------------

-- 실행 2 : 시리얼번호 증가 테스트

DECLARE @P_SNUMBER1 varchar(11)
DECLARE @P_SNUMBER2 varchar(11)
DECLARE @P_STRING   varchar(7) = 'myTest_'

-- 전체 11 자리 / 숫자 4 자리
SET @P_SNUMBER1 = 'myTest_0011';
SET @P_SNUMBER2 = 'myTest_2222';

-- 숫자부분만 끊어서 1 증가
SET @P_SNUMBER1 = SUBSTRING(@P_SNUMBER1, 8, 11) + 1;
SET @P_SNUMBER2 = SUBSTRING(@P_SNUMBER2, 8, 11) + 1;

select @P_SNUMBER1 as P_SNUMBER1,
       @P_SNUMBER2 as P_SNUMBER2;

-- (숫자 전체 자리수 - 숫자수) 만큼 앞에 0 을 붙여서
-- 앞에 @P_STRING 문자열(myTest_) 을 붙여준다
-- REPLICATE(문자(열), 숫자) : 문자(열) 을 숫자 개수 만큼 복제하여 붙여넣기
SET @P_SNUMBER1 = @P_STRING
                + (REPLICATE(0, 4 - LEN(@P_SNUMBER1))
                +  CONVERT(VARCHAR, @P_SNUMBER1));

SET @P_SNUMBER2 = @P_STRING
                + (REPLICATE(0, 4 - LEN(@P_SNUMBER2))
                +  CONVERT(VARCHAR, @P_SNUMBER2));

select @P_SNUMBER1 as P_SNUMBER1,
       @P_SNUMBER2 as P_SNUMBER2;

---------------------------------------

-- 실행 3 : 시리얼번호가 없으면 문자열_0001
--               이미 존재하면 문자열 + 1

DECLARE @P_SNUMBER1 varchar(11)
DECLARE @P_SNUMBER2 varchar(11)
DECLARE @P_STRING   varchar(7) = 'myTest_'


-- 전체 11 자리 / 숫자 4 자리
SET @P_SNUMBER2 = 'myTest_0011';


-- @P_SNUMBER1 처리  (= 시리얼번호가 NULL 인 경우)
IF @P_SNUMBER1 IS NULL
BEGIN
  SET @P_SNUMBER1 = @P_STRING + '0001';
END
ELSE
BEGIN
  SET @P_SNUMBER1 = SUBSTRING(@P_SNUMBER1, 8, 11) + 1;
  SET @P_SNUMBER1 = @P_STRING
                + (REPLICATE(0, 4 - LEN(@P_SNUMBER1))
                +  CONVERT(VARCHAR, @P_SNUMBER1));
END


-- @P_SNUMBER2 처리  (= 시리얼번호가 NULL 이 아닌 경우)
IF @P_SNUMBER2 IS NULL
BEGIN
  SET @P_SNUMBER2 = @P_STRING + '0001';
END
ELSE
BEGIN
  SET @P_SNUMBER2 = SUBSTRING(@P_SNUMBER2, 8, 11) + 1;
  SET @P_SNUMBER2 = @P_STRING
                + (REPLICATE(0, 4 - LEN(@P_SNUMBER2))
                +  CONVERT(VARCHAR, @P_SNUMBER2));
END


select @P_SNUMBER1 as P_SNUMBER1,
       @P_SNUMBER2 as P_SNUMBER2;

 

결과 1

   P_SNUMBER
-----------------
1  myTest_0001

결과 2

    P_SNUMBER1        P_SNUMBER1
-----------------------------------------
    12                      2223


    P_SNUMBER1        P_SNUMBER1
-----------------------------------------
    myTest_0012        myTest_2223
결과 3

    P_SNUMBER1        P_SNUMBER1
-----------------------------------------
    myTest_0001         myTest_0012

 

int 순번 칼럼 insert 할때

 

INSERT 테이블명 (
       칼럼1
     , 칼럼2
)
values (
       칼럼1값
     , (SELECT ISNULL(MAX(EXPE_SEQNO), 0)  + 1
        FROM   TEV_EVENT_EXPENS (NOLOCK)
        WHERE  EVENT_MANAGE_SN = @EVENT_MANAGE_SN)
)

 

 

 

------------------------------------------ 

Posted by dawnawaker
[prog] SQL - MSSQL2020. 9. 9. 15:03

ODBC 연결에 문제가 있어 주는 쪽, 받는 쪽 모두 SQL Server Native Client 로 설정.

 

1. SSMS 에서 해당 DB 선택 ➔ 마우스 오른쪽 버튼 ➔ 속성 ➔ 연결속성보기

 

   서버이름(서버 IP) 을 복사해둔다.

 

 

2. SSMS 에서 해당 DB 선택 ➔ 마우스 오른쪽 버튼 ➔ 태스크 ➔ 데이터 내보내기

 

3. 데이터 원본 선택

    1) 데이터 원본 : SQL Server Native Client 11.0

    2) 서버이름     : 복사해둔 서버이름

    3) 사용자 ID, 패스워드 입력

    4) 해당 데이터베이스 선택  (제대로 연결되었다면, 콤보버튼 클릭하면 해당 서버내의 모든 DB 가 보인다)

 

 

4. 대상선택

   1) 대상 : SQL Server Native Client 11.0

   2) 서버 : localhost  ← 직접입력

   3) 인증 : windows 인증 사용

   4) 데이터베이스 : [새로만들기] 클릭한 후 새로 만들자

 * 새로만들기 버튼이 보이지 않는다면, 윈도우창을 아래로 쭉 당기면 버튼이 보인다.

  진행과정 중, 아래 둘 중 하나를 고르는 화면에서는 Run Immediately 를 선택한다

  • Run Immediately
  • save  SSIS Package

 

5) 이렇게 해서 모든 작업이 끝나면, 해당 DB 는 SQL server 가 설치된 폴더에 들어간다.

   예) C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\dev_app01_Data.mdf

    C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\dev_app01_Log.ldf

 

 

6) 내려받은 DB 를 다른 컴퓨터로 복사하기

   로컬에서 sql server 가 돌아가고 있다면, 복사가 불가능하니,

   일단 sql server 부터 중지시킨다.

 

  6-1) 실행중인 SQL server 중지

       프로그램 ▶ windows 관리 도구 ▶ 서비스  or

       제어판   ▶ 관리 도구           ▶ 서비스  선택

  6-2) SQL server(MSSQLSERVER) 중지

  6-3) 이제 복사가 가능하니, 다른 곳에 복사한 후

         중지시킨 SQL Server 를 다시 실행

 

 

Posted by dawnawaker