How To Validate in Procedure

  • I Created a one Procedure ..its given Below...

    create procedure [dbo].[USP_Reliance_Master_Upload]

    as

    begin

    declare @dt datetime , @Mnth varchar(10)

    BEGIN TRY

    SELECT @dt = convert(varchar(10),convert(datetime,DCPI_Date),101) from Ram_Reliance_Master

    END TRY

    BEGIN CATCH

    RETURN-1-- The Date Field is In Correct Formate so Please Check Your Data

    END CATCH

    BEGIN TRY

    SELECT @Mnth = Mnth from Ram_Reliance_Master where isnull(Mnth,'')!=''

    END TRY

    BEGIN CATCH

    RETURN-1

    END CATCH -- The Date Field Should Not Be Null

    insert into dbo.Reliance_Master

    (Mnth,DCPI_Date,GRP_OUTS_RIL,Interest_Cost,OverDue_Earned,Qty,EPI,insertDt,Year)

    select

    Mnth,convert(datetime,DCPI_Date),GRP_OUTS_RIL,Interest_Cost,OverDue_Earned,Qty,EPI,cast(getdate() as DATE),DATEPART(YY,DCPI_Date)

    from Ram_Reliance_Master

    end

    The Existing Ram_Reliance_Master Table Data is Given Below,

    Mnth DCPI_Date GRP_OUTS_RIL Interest_Cost OverDue_Earned Qty EPI Year

    ---------- ---------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- -----------

    DEC 2012-12-12 NULL NULL NULL 5 2925 2012

    DEC 2012-12-21 NULL NULL NULL 0.002 0.91 2012

    DEC 2012-12-26 NULL NULL NULL 0.5 NULL 2012

    Null 13-02-2012 NULL NULL NULL 5 NULL 2012

    DEC 2012-12-12 NULL NULL NULL 5 2925 2012

    Requirement is...

    I Build that Stored Procedure in SSIS Package with Job scheduler...

    In that Procedure i want to check the Ram_Reliance_Master table Value

    1)Mnth Column Should be Not Null

    2)The DCPI_Date Field Should be 101 Format ie, (MMDDYYYY)

    Otherwise that Procedure will not Execute ... it should be terminate ...

    should not commit any Rows...

    How To Validate in Procedure ?

    I dont know well the Above Query is working fine or not..

    or any other idea Please Share and Support Me...

    Thanks & Regards

    Saravanan.D

  • sarwaanmca (1/23/2013)


    1)Mnth Column Should be Not Null

    Add in procedure

    IF isnull(@Mnth , '') = ''

    RETURN

    sarwaanmca (1/23/2013)


    The DCPI_Date Field Should be 101 Format ie, (MMDDYYYY)

    Use CONVERT function to set its format to 101

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • sarwaanmca (1/23/2013)


    I Created a one Procedure ..its given Below...

    create procedure [dbo].[USP_Reliance_Master_Upload]

    as

    begin

    declare @dt datetime , @Mnth varchar(10)

    BEGIN TRY

    SELECT @dt = convert(varchar(10),convert(datetime,DCPI_Date),101) from Ram_Reliance_Master

    END TRY

    BEGIN CATCH

    RETURN-1-- The Date Field is In Correct Formate so Please Check Your Data

    END CATCH

    BEGIN TRY

    SELECT @Mnth = Mnth from Ram_Reliance_Master where isnull(Mnth,'')!=''

    END TRY

    BEGIN CATCH

    RETURN-1

    END CATCH -- The Date Field Should Not Be Null

    insert into dbo.Reliance_Master

    (Mnth,DCPI_Date,GRP_OUTS_RIL,Interest_Cost,OverDue_Earned,Qty,EPI,insertDt,Year)

    select

    Mnth,convert(datetime,DCPI_Date),GRP_OUTS_RIL,Interest_Cost,OverDue_Earned,Qty,EPI,cast(getdate() as DATE),DATEPART(YY,DCPI_Date)

    from Ram_Reliance_Master

    end

    <snipped>

    1)Mnth Column Should be Not Null

    2)The DCPI_Date Field Should be 101 Format ie, (MMDDYYYY)

    Otherwise that Procedure will not Execute ... it should be terminate ...

    should not commit any Rows...

    How To Validate in Procedure ?

    I dont know well the Above Query is working fine or not..

    or any other idea Please Share and Support Me...

    Thanks & Regards

    Saravanan.D

    TRY-CATCH will not work for your purpose because control is passed to the CATCH block only when the TRY block code generates an error of sufficient severity. In both of your TRY-CATCH constructs, the CATCH blocks will execute only when the TRY code generates errors. The first TRY-CATCH will execute the CATCH block if for some reason the DCPI_Date value cannot be converted to a valid datetime, but that doesn't ensure that DCPI_Date is in the 101 MMDDYYYY format you want. For example, 10 June 2012 and 6 October 2012 can both be written as '06-10-2012' or '10-06-2012' depending on which convention your users are following. How SQL Server interprets those strings as dates depends on the DATEFORMAT setting.

    If you want to verify that the values of variables or parameters are within specified domains before proceeding, you can use IF statements:

    IF @Mnth IS NULL

    BEGIN

    RETURN

    END

    <some code to run otherwise>

    Jason Wolfkill

  • What datatype are you using for DCPI_Date in Reliance_Master and Ram_Reliance_Master?

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Please try this,

    create procedure [dbo].[USP_Reliance_Master_Upload]

    as

    begin

    --declare @dt datetime , @Mnth varchar(10)

    ------ If the data consist of Mnth column data as null or blank values

    IF EXISTS (SELECT 1 FROM Ram_Reliance_Master where ISNULL(RTRIM(LTRIM(Mnth)), '') = '')

    RETURN -1

    BEGIN TRY

    IF EXISTS (SELECT 1 from Ram_Reliance_Master

    WHERE DCPI_Date = convert(varchar(10),convert(datetime,DCPI_Date),101) )

    RETURN -1-- The Date Field is In Correct Formate so Please Check Your Data

    END TRY

    BEGIN CATCH

    RETURN -1-- The Date Field is In Correct Formate so Please Check Your Data

    END CATCH

    insert into dbo.Reliance_Master

    (Mnth,DCPI_Date,GRP_OUTS_RIL,Interest_Cost,OverDue_Earned,Qty,EPI,insertDt,Year)

    select

    Mnth,convert(datetime,DCPI_Date),GRP_OUTS_RIL,Interest_Cost,OverDue_Earned,Qty,EPI,cast(getdate() as DATE),DATEPART(YY,DCPI_Date)

    from Ram_Reliance_Master

    end

  • Might incorporating this work?

    select case when isnumeric(left(DCPI_Date,4))=1

    then DCPI_Date

    else '19000101'

    END

    when doing your insert into dbo.Reliance_Master

    Then later in your SSIS redirect those rows that have 1900-01-01 as the date to an separate file for visual inspection or other form of importing, for instance.

    ----------------------------------------------------

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

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