if object_id ('temp') is not null
drop table temp
go
create table temp
(id varchar(20)
, time time
, product varchar(100)
)
go
insert into temp (id, time, product)
select id, time, product
from
(
values
('A', '08:10', 'pizza' )
, ('A', '09:55', 'noodle' )
, ('B', '12:30', 'pizza' )
) a (id, time, product)
go
-- Consider the pros and cons of both queries.
-- 1
; with a
as
(
select row_number() over (partition by id order by time asc) rn , *
from temp
)
select *
from a
where rn = 1
-- 2
select b.*
from
(
select distinct id
from temp a
) a
cross apply
(
select top 1 *
from temp
where id = a.id
order by time
) b
if object_id ( 'tblx') is not null
drop table tblx
go
select
c1, c2, c3, isnull(sign(c3-lag(c3, 1) over (partition by c1 order by (c2))),0) c4
into tblx
from
(
select char(a.idx % 26 + 65) c1
, cast ('20180101' as datetime) + b.idx c2
, cast(abs(checksum(newid())) % 100000 as int) c3
from
(
select top 26 cast(row_number() over (order by (select 1)) as int) -1 idx
from sys.objects a1
) a,
(
select top 365 cast(row_number() over (order by (select 1)) as int) -1 idx
from sys.objects a1
, sys.objects a2
) b
) a
where
datepart(weekday, c2 ) not in (1,7)
--
select * from tblx
-- 1
; with a as
(
select row_number() over ( partition by c1, c order by c2) rn
, *
from tblx
,
(
select 0 c union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5
) b
)
select
max(c1) c1, min(c2) c2
, max(case when c = 0 then c3 else null end ) d0
, max(case when c = 1 then c3 else null end ) d1
, max(case when c = 2 then c3 else null end ) d2
, max(case when c = 3 then c3 else null end ) d3
, max(case when c = 4 then c3 else null end ) d4
, max(case when c = 5 then c3 else null end ) d5
from a
group by c1, rn-c
having max(case when c = 0 then c3 else null end ) is not null
and sign (max(case when c = 1 then c3 else null end ) - max(case when c = 0 then c3 else null end ))
+ sign (max(case when c = 2 then c3 else null end ) - max(case when c = 1 then c3 else null end ))
+ sign (max(case when c = 3 then c3 else null end ) - max(case when c = 2 then c3 else null end ))
+ sign (max(case when c = 4 then c3 else null end ) - max(case when c = 3 then c3 else null end ))
+ sign (max(case when c = 5 then c3 else null end ) - max(case when c = 4 then c3 else null end ))
= 5
order by c1, c2
-- 2
; with a
as
(
select *
, lag(c3, 1) over (partition by c1 order by (c2)) dm1
, lag(c3, 2) over (partition by c1 order by (c2)) dm2
, lag(c3, 3) over (partition by c1 order by (c2)) dm3
, lag(c3, 4) over (partition by c1 order by (c2)) dm4
, lag(c3, 5) over (partition by c1 order by (c2)) dm5
from tblx
)
select *
from a
where
sign(dm4-dm5)
+ sign(dm3-dm4)
+ sign(dm2-dm3)
+ sign(dm1-dm2)
+ sign(c3-dm1) = 5
-- 3
select a.c1, a.c2, a.c3
from
(
select a.* , count(*) over (partition by c1, rn2) as cn
from
(
select a.*
, row_number() over (partition by c1 order by c2)
- row_number() over (partition by c1, c4 order by c2) as rn2
from tblx a
) a
where c4 = 1
) a
where cn >= 5
order by c1, c2
if object_id ('tblx') is not null
drop table tblx, tbly, tblz
go
create table tblx (id int, c int)
go
create table tbly (id int, c int)
go
create table tblz (idx bigint identity(1,1),etime datetime, emessage nvarchar(max), )
go
create unique nonclustered index nc_tbly_01 on tbly (id)
go
if object_id ('usp_a') is null
exec ('create proc usp_a as select 1')
go
alter proc usp_a
(
@id int,
@c int
)
as
set implicit_transactions on ; -- 암시적 트랜잭션
begin try -- try catch 로 transaction 처리 할 부분 전체를 감싼다.
insert into tblx (id, c) values (@id, @c)
insert into tbly (id, c) values (@id, @c)
commit tran ;
end try
begin catch
if @@trancount > 0
begin
rollback tran;
set implicit_transactions off ;
-- 추가적인 로깅을 하던지
DECLARE @ErrorMessage NVARCHAR(max) = ERROR_MESSAGE()
insert into tblz (etime, emessage) values (getdate(), @ErrorMessage)
end
end catch
go
-- 아래
-- 테스트 시작
select @@trancount
select * from tblx
select * from tbly
select * from tblz
go
-- 오류없는 테스트
exec usp_a 1,1
exec usp_a 2,2
go
-- 데이터 확인
select * from tblx
select * from tbly
select * from tblz
go
-- 에러상황 테스트
-- tbly 는 unique index 가 있어서 중복값이 들어가지 못하는 상황이다.
-- rollback 했을때 전체가 rollback 되어야 하므로 tblx 에도 1,1 이 중복해 들어가지 않기를 바란다.
exec usp_a 1,1
select * from tblz
go
-- 데이터 확인
-- tbly 에서 에러가 생겨 데이터가 들어가지 않았다.
select * from tblx
select * from tbly
select * from tblz
go
-- 따로는 데이터가 잘 들어간다.
insert into tblx (id, c) values (1,1)
go
-- 데이터 확인
select * from tblx
select * from tbly
select * from tblz
go
// example // using System.Threading; // using Helpers; //class Program //{ // class Input // { // public int a { get; set; } // public int b { get; set; } // }
// class Output // { // public Input input { get; set; } // public int result { get; set; } // }
// static void Main(string[] args) // { // var p = new Program();
// new ThreadedHelper<Input, Output>(p.DoWork, p.Callback, p.ErrorCallback) // { // input = new Input { a = 4, b = 0 } // }.Start();
// for (int i = 0; i < 30; i++) // { // Console.WriteLine("wait"); // Thread.Sleep(100); // }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading;
namespace Helpers { class ThreadedHelper<I,O> { public delegate O DoWork(I inValue); public delegate void CallBack(O outValue); public delegate void ErrorCallBack(I input, Exception message); public I input { get; set; }
public ThreadedHelper(DoWork work, CallBack callback, ErrorCallBack errorCallback) { this.work = work; this.callback = callback; this.errorCallback = errorCallback; t = new Thread(Process); }
// example // using System.Threading; // using Helpers; //class Program //{ // class Input // { // public int a { get; set; } // public int b { get; set; } // }
// class Output // { // public Input input { get; set; } // public int result { get; set; } // }
// static void Main(string[] args) // { // var p = new Program();
// new ThreadedHelper<Input, Output>(p.DoWork, p.Callback, p.ErrorCallback) // { // input = new Input { a = 4, b = 0 } // }.Start();
// for (int i = 0; i < 30; i++) // { // Console.WriteLine("wait"); // Thread.Sleep(100); // }
* pfx my, localhost 에 등록 * cer Root, localhost 에 등록 * certlm.mmc 에서 Personal + Trusted Root Certi 저장소에 HOSTNAME pfx 와 cert 파일이 있는지 확인한다. * 자동으로 등록하는 클래스 Certification.cs 확인
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using CsLib;
private void DeleteCertification(string certFile, StoreName storeName, StoreLocation storeLocaion) { X509Store store = new X509Store(storeName, storeLocaion); try { X509Certificate2 cert = new X509Certificate2(certFile); store.Open(OpenFlags.ReadWrite); foreach (X509Certificate2 storeCert in store.Certificates) { if (storeCert.Thumbprint.Equals(cert.Thumbprint, StringComparison.OrdinalIgnoreCase)) { store.Remove(cert); break; } } } catch (Exception) { throw; } finally { store.Close(); } }
private string GetCertHash(string certFileName) { using (X509Certificate2 cert = new X509Certificate2(certFileName)) return cert.Thumbprint.ToLower(); }
private string GetAssemblyGuid() { var assembly = typeof(Program).Assembly; var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; var id = attribute.Value; return id.ToLower(); }