본문 바로가기

Database/MSSQL work

(47)
@myVar 변수선언, 할당, 추출 --변수선언하기 declare @myVar1 int; declare @myVar2 smallint, @myVar3 decimal(5,2); declare @myVar4 nchar(20); --값을 할당하기 set @myVar1=5; set @myVar2=3; set @myVar3=2.34; set @myVar4='고객이름=> '; --위의 것을 기준으로 값을 추출 select @myVar1; select @myVar2+@myVar3; select @myVar4, custName from customersTbl where regDate > '2018-01-01'; --변수 선언 후 값을 지정하고 함수에 사용 declare @myVar1 int; set @myVar1 = 3; select top(@myVar..
Merge table memberTbl을 M, changeTbl을 C로 잡은 후 M. ,C. 으로 사용함 on을 통해 조건을 준 다음 when으로 상황을 나누어 그 상황에 맞는 값을 씀 merge memberTbl as M using changeTbl as C on M.custId = C.userId when not matched and changeType = '신규가입' then insert(custId, custName, addr) values (C.userId, C.name, C.addr) when matched and changeType ='주소변경' then update set M.addr = C.addr when matched and changeType = '회원탈퇴' then delete; 하기전 테이..
UPDATE, DELETE update update test6 set Lname = '병만' where Fname = 'Kim'; --select * from test6 where Fname = 'Kim'; delete --DELETE delete from test6 where Fname = 'Kim'; --select* from test6 where Fname='Kim';
테이블에서 테이블 만들기 다른 table에서 값 가져오기 방법1. --1. insert into test5 select BusinessEntityID, FirstName, LastName from AdventureWorks2019.Person.Person 2. --2. select BusinessEntityID as id, FirstName as Fname, LastName as Lname into test6 from AdventureWorks2019.Person.Person
sequence , default Sequence * 중간에 seq를 지정해 버렸을 경우 다시 시작하기 --sequence를 지정해서 넣어버림 insert into test3 values(10, '이말년', 30, default); --10 다음숫자부터 넣고싶음으로 sequence를 재정비해줌 alter sequence idSeq restart with 11; 방법1. table 생성시 int identity로 설정해 놓으면 값을 비워놓아도 +1씩 자동 증가가 된다. (따로 create seq할 필요 없음) create table test2 ( idint identity,--1씩 증가한다 userNamenvarchar(5), ageint, addrnvarchar(5) default'서울'--값에 default라고 적으면 '서울'이 들어감..
TOP 수식 -- TOP에 수식하기 use AdventureWorks2019 select CreditCardID from sales.CreditCard where CardType='Vista' order by ExpYear, ExpMonth; -- 위와 동일~ TOP 사용하여 상위 10개만 조회 select top(10) CreditCardID from sales.CreditCard where CardType='Vista' order by ExpYear, ExpMonth; -- TOP에 수식하기 select top(select count(*)/100 from Sales.CreditCard) CreditCardID from sales.CreditCard where CardType='Vista' order by ExpY..
판매, 고객, 상품 테이블 연습 --1. 기간 내 판매내역 조회 select * from salesTbl where saleDate >= '2018-09-01' and saleDate (select regDate from customersTbl where custName='박지송'); --(any / all) --6. 고객중에 김씨보다 나중에 등록한 사람 (첫번쨰김씨) select * from customersTbl where regDate > any (select regDate from customersTbl where custName like '김%'); --6-1. 가장 나중에 등록한 김씨를 뽑아 비교함 select * from customersTbl where regDate > all (select regDate from cus..