Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2003-12-12 : 08:27:33
|
| Tony writes "I have an UDF that should return a simple 0 or 1 depending upon the result of the SQL; REGARDLESS of whether the condition is met the function returns 0. Please advise/help...I'm new to UDF's so I would appreciate any help - UDF is as follows:CREATE FUNCTION fnTSCreated(@Peoplesoft_Id nvarchar)RETURNS intASBEGIN--// 0 - Not Created--// 1 - CreatedDECLARE @Created int --Created or NotIf (Select Count(*) FROM TS_Timesheet_Header WHERE Peoplesoft_Id =@Peoplesoft_Id) > 0 -- Created SET @Created = 1ELSE -- Not Created SET @Created = 0RETURN (@Created)END" |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-12-12 : 08:44:52
|
| s.b. If exists (Select * FROM TS_Timesheet_Header WHERE Peoplesoft_Id =@Peoplesoft_Id)but that won't make any difference.Are there any trainking spaces?try the query manually and check the parameter passed in.What you have looks OK.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-12-12 : 08:48:21
|
| [code]CREATE FUNCTION dbo.fnTSCreated(@Peoplesoft_Id nvarchar)RETURNS intASBEGIN--// 0 - Not Created--// 1 - CreatedDECLARE @Created int --Created or NotSELECT @Created = CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM TS_Timesheet_HeaderWHERE Peoplesoft_Id =@Peoplesoft_IdRETURN @CreatedEND[/code] |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-12-12 : 09:54:34
|
| Looks to me like any of these UDFs should work. I'm thinking the WHERE is FALSE for some reason.Don't UDFs have owners too? Should you be creating dbo.fnTSCreated to avoid ambiguity?Try executing in Query AnalyzerPrint dbo.fnTSCreated(N'MyValue')Where MyValue should return a known 1Sam |
 |
|
|
|
|
|