Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How To Validate in Procedure Expand / Collapse
Author
Message
Posted Wednesday, January 23, 2013 12:06 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, March 15, 2014 10:37 AM
Points: 46, Visits: 182
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
Post #1410366
Posted Wednesday, January 23, 2013 3:41 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 6, 2014 5:46 AM
Points: 2,840, Visits: 3,969
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
Post #1410447
Posted Thursday, January 24, 2013 2:01 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:00 PM
Points: 1,061, Visits: 2,572
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1411333
Posted Friday, January 25, 2013 1:48 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 8:18 AM
Points: 938, Visits: 2,928
What datatype are you using for DCPI_Date in Reliance_Master and Ram_Reliance_Master?




The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1411524
Posted Saturday, January 26, 2013 11:28 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 12:57 AM
Points: 329, Visits: 285
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

Post #1412104
Posted Thursday, April 11, 2013 11:33 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, October 17, 2014 12:20 PM
Points: 373, Visits: 909
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.
Post #1441395
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse