SQL Function that returns a message then deletes the row

Ask the few things google does not know

Moderator: Dictators in Training

SQL Function that returns a message then deletes the row

Postby Naethyn » Fri May 05, 2006 2:30 pm

SQL Server 2005
I'm trying to create a function that will return a value from a field in a table then delete that row. Below is the code I'm testing with. When i try to create the fuction it gives the error:

Msg 443, Level 16, State 15, Procedure RecieveMessage, Line 21
Invalid use of side-effecting or time-dependent operator in 'DELETE' within a function.

Any help would be greatly appreciated.

Thanks,
Nathan

USE MobileData
GO
CREATE FUNCTION RecieveMessage(@Company NVARCHAR(100), @Technician NVARCHAR(100))
RETURNS NVARCHAR(1000)
AS
BEGIN
DECLARE @UniqueID uniqueidentifier
SELECT @UniqueID =
(
SELECT TOP(1) UniqueID
FROM mobiledata.dbo.Connection
WHERE company = @company AND technician = @technician
)

DECLARE @MessageString NVARCHAR(1000)
SELECT @MessageString =
(
SELECT MessageString
FROM mobiledata.dbo.Connection
WHERE UniqueID = @UniqueID
)

DELETE FROM MobileData.dbo.Connection
WHERE @UniqueID = UniqueID
RETURN
@MessageString
END
User avatar
Naethyn
NT Traveller
NT Traveller
 
Posts: 2085
Joined: Wed May 04, 2005 12:13 pm

Postby Gidan » Fri May 05, 2006 5:27 pm

MSSQL doesn't allow editing of tables from within user defined functions.
For to win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
User avatar
Gidan
Admin Abuse Squad
Admin Abuse Squad
 
Posts: 2892
Joined: Tue Jan 04, 2005 11:01 am

Postby Lueyen » Sat May 06, 2006 10:23 pm

Call the fuction from a stored procedure that returns the information then deletes the record?
Raymond S. Kraft wrote:The history of the world is the history of civilizational clashes, cultural clashes. All wars are about ideas, ideas about what society and civilization should be like, and the most determined always win.

Those who are willing to be the most ruthless always win. The pacifists always lose, because the anti-pacifists kill them.
User avatar
Lueyen
Dictator in Training
Dictator in Training
 
Posts: 1793
Joined: Tue Mar 09, 2004 2:57 pm


Return to Tech Support

Who is online

Users browsing this forum: No registered users and 20 guests

cron