Author |
Topic |
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-20 : 15:36:21
|
Hi I am trying to produce an IF statement that returns a value if the item is true or false. It is calling a function. The syntax looks like:DECLARE @inDATE DAETIMESET @inDate = '2008-05-11'IFdbo.udf_FormatDate(@inDATE) IN (SELECT dbo.udf_FormatDate(ID) FROM DatePeriods)RETURN 0ELSERETURN 1However when I run the script I get the following error:Msg 178, Level 15, State 1, Line 8A RETURN statement with a return value cannot be used in this context.Msg 178, Level 15, State 1, Line 10A RETURN statement with a return value cannot be used in this context.Does anyone know what I could do?Thanks |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-20 : 15:43:45
|
Is udf_FormatDate a scalar function?You'll need to use IF EXISTS (SELECT...) where ... will probably include a JOINTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-20 : 15:50:35
|
HiYes it is a scalar function. How would the syntax go then?Thanking you in advance! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-20 : 15:58:47
|
Try this. I didn't need a join after all.DECLARE @inDATE datetimeSET @inDATE = '2008-05-11'IF EXISTS (SELECT * FROM DatePeriods WHERE dbo.udf_FormatDate(ID) = dbo.udf_FormatDate(@inDATE)) SELECT 0ELSE SELECT 1 Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-20 : 16:55:31
|
Hey, thats GREAT!!!Thanks for all your help!!! |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-20 : 16:59:45
|
Just a quickie! Is the parts stating SELECT 0 and SELECT 1 the same as RETURN statement I was trying to use originally?Thanks |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-21 : 04:46:10
|
Hi GuysI've tried to put the IF statement into a function with the syntax:CREATE FUNCTION CheckDateReturn(@InDATE DATETIME)RETURNS BITASBEGINDECLARE @Output BITSET @Output = IF EXISTS(SELECT * FROM DatePeriods WHERE dbo.udf_FormatDate(ID) = dbo.udf_FormatDate(@InDATE)) RETURN 0ELSE RETURN 1RETURN @OutputBut I keep on getting syntax errors:Msg 156, Level 15, State 1, Procedure CheckDateReturn, Line 14Incorrect syntax near the keyword 'IF'.Msg 102, Level 15, State 1, Procedure CheckDateReturn, Line 18Incorrect syntax near '@Output'.I'm totally stuck on this as I haven't really used IF statements in functions before!Any ideas?Thanks |
|
|
bjoerns
Posting Yak Master
154 Posts |
Posted - 2008-05-21 : 05:28:07
|
if exists(...)return 0elsereturn 1no @output needed |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-21 : 05:28:42
|
quote: Originally posted by rcr69er Hi GuysI've tried to put the IF statement into a function with the syntax:CREATE FUNCTION CheckDateReturn(@InDATE DATETIME)RETURNS BITASBEGINDECLARE @Output BITIF EXISTS(SELECT * FROM DatePeriods WHERE dbo.udf_FormatDate(ID) = dbo.udf_FormatDate(@InDATE))SET @Output = 0ELSESET @Output = 1RETURN @OutputENDGOBut I keep on getting syntax errors:Msg 156, Level 15, State 1, Procedure CheckDateReturn, Line 14Incorrect syntax near the keyword 'IF'.Msg 102, Level 15, State 1, Procedure CheckDateReturn, Line 18Incorrect syntax near '@Output'.I'm totally stuck on this as I haven't really used IF statements in functions before!Any ideas?Thanks
Change like this |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-21 : 05:46:14
|
Hey thanks yet again!!! |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-21 : 08:44:06
|
HiIs there anyway of showing an error message if the return value is 1?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-21 : 11:27:05
|
quote: Originally posted by rcr69er HiIs there anyway of showing an error message if the return value is 1?Thanks
Error message? like what? |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-21 : 14:04:00
|
something like 'record will not been inserted' |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-21 : 15:03:44
|
You can use RAISERROR.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-05-22 : 03:46:25
|
HiI'm trying to include it in the function using the following:CREATE FUNCTION [dbo].[udf_CheckDateReturnERROR](@InDATE DATETIME)RETURNS BITASBEGINDECLARE @Output BITIF EXISTS (SELECT ID FROM DatePeriods WHERE dbo.udf_FormatDate(ID) = dbo.udf_FormatDate(@InDATE)) SET @Output = 0ELSE SET @Output = 1RAISERROR ('Cannot Insert into Table',10,1)RETURN @OutputENDBut I keep on getting the following error:Msg 443, Level 16, State 14, Procedure udf_ECG_CheckDateReturnERROR, Line 19Invalid use of side-effecting or time-dependent operator in 'RAISERROR' within a function.Anyone know what I could do? |
|
|
|