Kategori arşivi: MSSQL

MSSQL DateTime toplama-çıkarma

mssql datetime

Mssql ile datetime toplama çıkarma nasıl yapılır?

Mssql veritabanında datetime alanı üzerinde toplama ve çıkarma yapabilmek için hazırladığım kod aşağıdadır

Datetime kolonlarını dakika bazında birbirinden çıkarıyor ve ilgili tabloya yerleştiriyor.


select

servis_personelcalismasuresi.sureid,

servis_personelcalismasuresi.personelid,

(personel_kimlik.ad + ' ' + personel_kimlik.soyad) personel,

sum((DATEDIFF(n, CAST('00:00' AS DATETIME), servis_personelcalismasuresi.mesaibitis)
-DATEDIFF(n, CAST('00:00' AS DATETIME), servis_personelcalismasuresi.mesaibaslangic))) mesai

from servis_personelcalismasuresi

inner join personel_kimlik on personel_kimlik.personelid=servis_personelcalismasuresi.personelid

group by servis_personelcalismasuresi.sureid, servis_personelcalismasuresi.personelid, personel_kimlik.ad, personel_kimlik.soyad

order by servis_personelcalismasuresi.sureid desc

MSSQL Stored Procedure ile çok parametreli raporlama

mssql

Bir projemde kullandığım stored procedure kodları aşağıdadır, ad soyad ve tcno isimli 3 adet giriş parametremiz var, yazdığım kodlar bunların hangisinin boş olup olmadığını algılayıp ona göre where şartını oluşturuyor

örnek kullanım:


exec sp_personel_puantajlistele @ad='mehmetcan', @soyad='yegen', @tcno=''

USE [ttsproje2]
GO
/****** Object: StoredProcedure [dbo].[sp_personel_puantajlistele] Script Date: 09.05.2013 10:28:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Mehmetcan Yegen
-- Create date: 08.05.2013
-- Description: Personel puantaj bilgilerinin select sorgusu
-- =============================================
ALTER PROCEDURE [dbo].[sp_personel_puantajlistele]

@ad as nvarchar(max),
@soyad as nvarchar(max),
@tcno as nvarchar(max)
AS

declare @anasorgu nvarchar(max)
declare @wheresarti nvarchar(max)
declare @andeki nvarchar(max)

declare @adkontrol int
declare @soyadkontrol int
declare @tcnokontrol int

declare @andkontrol int

BEGIN
SET NOCOUNT ON;

set @anasorgu = 'select * from personel_kimlik'
set @wheresarti = ' where'
set @andeki = ' and'
set @andkontrol = '0'

--ad ek sorgusu tanımlandıysa kontrolü etkinleştir
if len(@ad) > 0
begin
set @adkontrol = '1'
end
else
begin
set @adkontrol = '0'
end

--soyad ek sorgusu tanımlandıysa kontrolü etkinleştir
if len(@soyad) > 0
begin
set @soyadkontrol = '1'
end
else
begin
set @soyadkontrol = '0'
end

--tcno ek sorgusu tanımlandıysa kontrolü etkinleştir
if len(@tcno) > 0
begin
set @tcnokontrol = '1'
end
else
begin
set @tcnokontrol = '0'
end

--eğer ek sorguların toplamı 0 dan büyükse
if @adkontrol + @soyadkontrol + @tcnokontrol = 0
begin
set @wheresarti = ''
end

--eğer ek sorgularım toplamı 1 den küçük veya 1 e eşitse
if @adkontrol + @soyadkontrol + @tcnokontrol <= 1
begin
set @andeki = ''
end

&nbsp;

-- ************************* EK SORGULAR BAŞLANGIÇ *************************

--ad ek sorgusu tanımlandıysa
if len(@ad) > 0
begin
--and kontrolü
set @andkontrol = @andkontrol + 1
if @andkontrol <= 1
begin
set @andeki = ''
end
else
begin
set @andeki = ' and'
end
--ek sorgu
set @ad = @andeki + ' ad like ''' + @ad + ''''
end
else begin
set @ad = ''
end

--soyad ek sorgusu tanımlandıysa
if len(@soyad) > 0
begin
--and kontrolü
set @andkontrol = @andkontrol + 1
if @andkontrol <= 1
begin
set @andeki = ''
end
else
begin
set @andeki = ' and'
end
--ek sorgu
set @soyad = @andeki + ' soyad like ''' + @soyad + ''''
end
else begin
set @soyad = ''
end

--tcno ek sorgusu tanımlandıysa
if len(@tcno) > 0
begin
--and kontrolü
set @andkontrol = @andkontrol + 1
if @andkontrol <= 1
begin
set @andeki = ''
end
else
begin
set @andeki = ' and'
end
--ek sorgu
set @tcno = @andeki + ' tcno like ''' + @tcno + ''''
end
else begin
set @tcno = ''
end

-- ************************* EK SORGULAR BİTİŞ *************************
--son aşama
exec(@anasorgu + @wheresarti + @ad + @soyad + @tcno)

END