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 |
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-08-22 : 11:29:02
|
| [code]CREATE FUNCTION dbo.BlogCountComments(@postPK char(35))RETURNS VARCHAR(1000) ASBEGIN DECLARE @a_variable varchar(1000) SELECT @a_variable = 0 FROM dbo.BlogComments comments WHERE comments.commentPost = @postPK RETURN @@rowcountEND[/code]this UDF returns the number of comments that exist for a postthe UDF is working like i want, but it is inefficient because i am using @a_variablehow can i get rid of the @a_variable from the SELECT?i have tried to fix the situation like this:[code]CREATE FUNCTION dbo.BlogCountComments(@postPK char(35))RETURNS VARCHAR(1000) ASBEGIN SELECT * FROM dbo.BlogComments comments WHERE comments.commentPost = @postPK RETURN @@rowcountEND[/code]This returns:Error 444: Select statements included within a function cannot return data to a client.any idea of what i can do?thanks for your help |
|
|
sshelper
Posting Yak Master
216 Posts |
Posted - 2007-08-22 : 11:43:06
|
| Try it this way:CREATE FUNCTION dbo.BlogCountComments(@postPK char(35))RETURNS VARCHAR(1000) ASBEGIN RETURN (SELECT COUNT(*) FROM dbo.BlogComments comments WHERE comments.commentPost = @postPK)ENDSQL Server Helperhttp://www.sql-server-helper.com |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-22 : 11:44:47
|
[code]CREATE FUNCTION dbo.BlogCountComments( @postPK char(35))RETURNS INT ASBEGIN RETURN ( SELECT COUNT(*) FROM dbo.BlogComments WHERE commentPost = @postPK )END[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-08-22 : 11:50:52
|
| ahh that makes senseawesome, thanks for the fast response! |
 |
|
|
|
|
|