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

Select in scalar function - what am I doing wrong? Expand / Collapse
Author
Message
Posted Tuesday, February 19, 2013 3:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 19, 2013 9:10 AM
Points: 1, Visits: 2
Hi

I'm trying to create the function below

CREATE FUNCTION checkLock (@van_id varchar , @nextdel DATETIME)
RETURNS varchar
AS
BEGIN
declare @locked as varchar = '0'
declare @count as int

select @van_id + replace(convert(varchar(11), @nextdel, 113), ' ' ,'') datekey from replicationLock
set @count = @@rowcount

if (@count = 0)
begin
set @locked = '0'
end
else
begin
set @locked = '1'
end

return @locked
END

I just keep getting the error

Msg 444, Level 16, State 2, Procedure checkLock, Line 8
Select statements included within a function cannot return data to a client.

Problem is I'm not very knowledgable about sql server functions etc and I've been at this yesterday afternoon and all morning and can not see where I am going wrong or find any solution on the net. I won't bother explaining what my function does as its about as straight forward as it gets. I want to use this function inside an update statement being called from a c# app.

How can I get the row count of the query without using a select? or am I missing something here ?

Any help would be greatly appreciated here as its really holding me up. Thanks.
Post #1421511
Posted Tuesday, February 19, 2013 3:34 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 10:26 AM
Points: 507, Visits: 2,229
Replace


select @van_id + replace(convert(varchar(11), @nextdel, 113), ' ' ,'') datekey from replicationLock
set @count = @@rowcount

with :

SELECT @count = COUNT(@van_id + replace(convert(varchar(11), @nextdel, 113), ' ' ,'')) from replicationLock



-----------------------------------
http://www.SQL4n00bs.com
Post #1421514
Posted Tuesday, February 19, 2013 3:34 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, June 05, 2013 2:40 AM
Points: 5,075, Visits: 4,833
select @van_id + replace(convert(varchar(11), @nextdel, 113), ' ' ,'') datekey from replicationLock

The above line is returning data to the client, which is causing the error

Do

SELECT @count = COUNT(@van_id + replace(convert(varchar(11), @nextdel, 113), ' ' ,'')) from replicationLock

Edit - Abu quick off the draw.




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1421515
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse