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 |
mrleokarthik
Starting Member
16 Posts |
Posted - 2006-07-19 : 07:59:18
|
Is it possible to create a function that deletes records from a table ?CREATE FUNCTION F_TSImported_Delete()returns intasBegin delete from ts_importedreturn 0endGOThis throws error like this:Invalid use of side-effecting or time-dependent operator in 'DELETE' within a function. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-19 : 08:01:19
|
Nope. This is not allowed KH |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-07-19 : 09:21:22
|
U can create a stored procedure insteadSrinika |
|
|
mrleokarthik
Starting Member
16 Posts |
Posted - 2006-07-19 : 10:09:13
|
quote: Originally posted by mrleokarthik Is it possible to create a function that deletes records from a table ?CREATE FUNCTION F_TSImported_Delete()returns intasBegin delete from ts_importedreturn 0endGOThis throws error like this:Invalid use of side-effecting or time-dependent operator in 'DELETE' within a function.
Thank you for your reply.Yes, i can use Stored procedure |
|
|
JustDaveN
Starting Member
1 Post |
Posted - 2009-12-30 : 10:20:20
|
Just as an FYI, you can call a stored procedure from within your function. If you need to keep what you are doing as a function, you can take the external table changes and put it in a stored proc, then call it.A lot of folks would ask why you would want to do this. For my specific example, I had written a table function that was used in several stored procedures. It worked well as the function ended up changing and rather than having to change this block of code in 10 or more procs, I only had to change it once. Later, a business requirement was added to log some intermediate calculations that were used in determining the data set returned. I added a procedure call to log the value in an external table.HTHDavid |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-30 : 10:40:26
|
quote: Originally posted by JustDaveN Just as an FYI, you can call a stored procedure from within your function. If you need to keep what you are doing as a function, you can take the external table changes and put it in a stored proc, then call it.A lot of folks would ask why you would want to do this. For my specific example, I had written a table function that was used in several stored procedures. It worked well as the function ended up changing and rather than having to change this block of code in 10 or more procs, I only had to change it once. Later, a business requirement was added to log some intermediate calculations that were used in determining the data set returned. I added a procedure call to log the value in an external table.HTHDavid
Eh -- no you cant!At least you can't in 2005 or in 2000. I don't know about 2008. Functions can't change data The procedures and other external objects you can call from inside a function are very limited.ExampleCREATE PROCEDURE cg_write_valAS BEGIN SELECT 'a' AS [Foo] INTO _testENDGOCREATE FUNCTION myFunc (@foo INT) RETURNS INTAS BEGIN EXEC cg_write_val SET @foo = -1 RETURN @fooENDGOSELECT dbo.myFunc(1) Give the error on run:Msg 557, Level 16, State 2, Line 2Only functions and extended stored procedures can be executed from within a function. EDIT : HOLY THREAD RESURRECTION BATMAN!Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
|
|
|
|
|
|
|