T-SQL과 관련하여 간단한 질문이 있습니다. 날짜를 반환하는 함수를 호출하는 저장 프로 시저가 있습니다. IF 조건을 사용하여 오늘 날짜를 함수가 반환 한 날짜와 비교하려고합니다. IF를 true로 설정하면 데이터를 반환합니다.T-SQL IF 조건 날짜 평가
이 문제를 해결하는 가장 좋은 방법에 대한 아이디어. 나는 현재 t-SQL을 배우고 있으며 나는 C#을 사용하여 논리적 인 조건에 익숙하다.
ALTER FUNCTION [dbo].[monday_new_period](@p_date as datetime) -- Parameter to find current date
RETURNS datetime
BEGIN
-- 1 find the year and period given the current date
-- create parameters to store period and year of given date
declare @p_date_period int, @p_date_period_year int
-- assign the values to the period and year parameters
select
@p_date_period=period,
@p_date_period_year = [year]
from client_week_uk where @p_date between start_dt and end_dt
-- 2 determine the first monday given the period and year, by adding days to the first day of the period
-- this only works on the assumption a period lasts a least one week
-- create parameter to store the first day of the period
declare @p_start_date_for_period_x datetime
select @p_start_date_for_period_x = min(start_dt)
from client_week_uk where period = @p_date_period and [year] = @p_date_period_year
-- create parameter to store result
declare @p_result datetime
-- add x days to the first day to get a monday
select @p_result = dateadd(d,
case datename(dw, @p_start_date_for_period_x)
when 'Monday' then 0
when 'Tuesday' then 6
when 'Wednesday' then 5
when 'Thursday' then 4
when 'Friday' then 3
when 'Saturday' then 2
when 'Sunday' then 1 end,
@p_start_date_for_period_x)
Return @p_result
END
ALTER PROCEDURE [dbo].[usp_data_to_retrieve]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF monday_new_period(dbo.trimdate(getutcdate()) = getutcdate()
BEGIN
-- SQL GOES HERE --
END
감사합니다.
도움이되었습니다. 감사. –