T-SQL - some HELP

  • T_SQL is wrong, some help is much appreciated.

    IF EXISTS (SELECT DATEPART(hh, scheduledtime)

    FROM intakesource_daily

    WHERE DATEPART(hh, GETDATE()) > DATEPART(hh,scheduledtime))

    BEGIN

    IF EXISTS (SELECT DATEPART(mi, scheduledtime)

    FROM intakesource_daily

    WHERE DATEPART(mi, GETDATE()) > DATEPART(mi,scheduledtime))

    THEN

    PRINT 'File is late'

    ELSE

    END

  • just syntax...there is no IF<condition>THEN<statement>, only IF <condition><statement>

    IF EXISTS (SELECT DATEPART(hh, scheduledtime)

    FROM intakesource_daily

    WHERE DATEPART(hh, GETDATE()) > DATEPART(hh,scheduledtime))

    BEGIN

    IF EXISTS (SELECT DATEPART(mi, scheduledtime)

    FROM intakesource_daily

    WHERE DATEPART(mi, GETDATE()) > DATEPART(mi,scheduledtime))

    PRINT 'File is late'

    ELSE

    PRINT 'File is on time'

    END

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Also, you probably don't want those two nested if statements. (unless you do...)

    The way you have it now, if there's a intake scheduled for 5:45, and another scheduled for 3:11 and the current time is 4:15, you're going to say the file is late.

    What you probably want is to have both criteria true on a single record. unless you don't.

    -a.



    Dan Guzman - Not the MVP (7/22/2010)
    All questions have to be prefaced by Server version and 'according to MS Docs' or 'my own personal opinion based on how much detail I felt like digging into at the time.'

  • --declare a table variable

    declare @intakesource_daily table

    (scheduledTime datetime)

    --insert a testing value

    insert into @intakesource_daily(scheduledTime)

    values ('2010-04-27 14:56:40.993')

    --if the scheduledtime < getdate(),then print 'file is late'

    IF EXISTS

    (SELECT DATEPART(hh, scheduledtime) FROM @intakesource_daily WHERE DATEPART(hh, GETDATE()) > DATEPART(hh,scheduledTime)

    or

    (DATEPART(hh,GETDATE())=DATEPART(hh,scheduledTime) and DATEPART(mi, GETDATE()) > DATEPART(mi,scheduledTime)))

    BEGIN

    PRINT 'File is late'

    END

  • Thank you all for the replies! Great resources on this site!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply