Solved

im trying to create a user defined function, but cant execute it as an error message appears

Posted on 2008-10-28
6
277 Views
Last Modified: 2008-11-28
when trying to run an sql 2005 user defined function, it wont execute as the following error message occurs:

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

How do i resolve this?

THanks

Julia
USE [MembershipDB]
GO
/****** Object:  UserDefinedFunction [dbo].[getUpgradeProductIdInPackage]    Script Date: 10/27/2008 14:09:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[InsertMailingPreferences] (
			
			@PackageID int,
			@AddressID int,
			@UserName nvarchar(100)
								
			)
returns nvarchar(50)--int	
as
BEGIN
 
 
DECLARE @Counter int
SET @Counter = 0
 
DECLARE @TotalRows int
DECLARE @ServiceID int
DECLARE @MailingMethodID int
DECLARE @MaxServiceID int
DECLARE @NowDate datetime
--------------------------------------------------------------
DECLARE @DefaultMailingsTable	table
(
		ServiceID int,
		ServiceName nvarchar(50),
		DefaultMailingMethodID int,
		MethodDesc nvarchar(50)
		)
 
 
--SET @DefaultMailingsTable = dbo.getDefaultMailingPreferencesForPackage(@PackageID)
--SET @DefaultMailingsTable = (SELECT * FROM dbo.getDefaultMailingPreferencesForPackage(@PackageID))
 
Insert Into @DefaultMailingsTable
SELECT * FROM dbo.getDefaultMailingPreferencesForPackage(@PackageID)
 
SET @TotalRows = (SELECT Count(*) FROM @DefaultMailingsTable)
SET @MaxServiceID = (SELECT Max (ServiceID) FROM dbo.getDefaultMailingPreferencesForPackage(@PackageID))
 
 
WHILE (@Counter < @MaxServiceID+1)
BEGIN
 
--Select ServiceID, DefaultMailingMethodID FROM @DefaultMailingsTable WHERE   exists
--		(Select ServiceID, DefaultMailingMethodID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)
 
	If  (Select ServiceID FROM @DefaultMailingsTable WHERE   serviceID in  (@Counter)) is NOT null
		--(Select ServiceID, DefaultMailingMethodID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)) = @Counter -- is not null
 
	BEGIN
	Set @ServiceID = (Select ServiceID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)
	Set @MailingMethodID = (Select  DefaultMailingMethodID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)
	Set @NowDate = getDate()
--convert(datetime,getDate(),103)
 
 
 
 
	
	INSERT INTO dbo.MailingUserPreferences(UserName, DateSelected, MailingMethodID, AddressID, ServiceID)
	VALUES(@UserName, convert(datetime,24/05/2008,103),  1, @AddressID, @ServiceID);
	SET @Counter = @Counter + 1	
	END
 
	Else 
 
	Begin
	SET @Counter = @Counter + 1	
	end
 
 
 
 
END
 
 
 
 
Return 'Success'
--Return @PackageID
 
 
 
 
END

Open in new window

0
Comment
Question by:Juliafrazer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 22824681
You can't run DML statements on user tables inside functions like that.
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 250 total points
ID: 22824747
Chap is right... but what you really want is a stored procedure.
USE [MembershipDB]
GO
/****** Object:  procedure[dbo].[getUpgradeProductIdInPackage]    Script Date: 10/27/2008 14:09:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[InsertMailingPreferences] 
                        
                        @PackageID int,
                        @AddressID int,
                        @UserName nvarchar(100)
                        @output nvarchar(50) output                                        
as
BEGIN
 
 
DECLARE @Counter int
SET @Counter = 0
 
DECLARE @TotalRows int
DECLARE @ServiceID int
DECLARE @MailingMethodID int
DECLARE @MaxServiceID int
DECLARE @NowDate datetime
--------------------------------------------------------------
DECLARE @DefaultMailingsTable   table
(
                ServiceID int,
                ServiceName nvarchar(50),
                DefaultMailingMethodID int,
                MethodDesc nvarchar(50)
                )
 
 
--SET @DefaultMailingsTable = dbo.getDefaultMailingPreferencesForPackage(@PackageID)
--SET @DefaultMailingsTable = (SELECT * FROM dbo.getDefaultMailingPreferencesForPackage(@PackageID))
 
Insert Into @DefaultMailingsTable
SELECT * FROM dbo.getDefaultMailingPreferencesForPackage(@PackageID)
 
SET @TotalRows = (SELECT Count(*) FROM @DefaultMailingsTable)
SET @MaxServiceID = (SELECT Max (ServiceID) FROM dbo.getDefaultMailingPreferencesForPackage(@PackageID))
 
 
WHILE (@Counter < @MaxServiceID+1)
BEGIN
 
--Select ServiceID, DefaultMailingMethodID FROM @DefaultMailingsTable WHERE   exists
--              (Select ServiceID, DefaultMailingMethodID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)
 
        If  (Select ServiceID FROM @DefaultMailingsTable WHERE   serviceID in  (@Counter)) is NOT null
                --(Select ServiceID, DefaultMailingMethodID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)) = @Counter -- is not null
 
        BEGIN
        Set @ServiceID = (Select ServiceID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)
        Set @MailingMethodID = (Select  DefaultMailingMethodID FROM @DefaultMailingsTable WHERE ServiceID = @Counter)
        Set @NowDate = getDate()
--convert(datetime,getDate(),103)
 
 
 
 
        
        INSERT INTO dbo.MailingUserPreferences(UserName, DateSelected, MailingMethodID, AddressID, ServiceID)
        VALUES(@UserName, convert(datetime,24/05/2008,103),  1, @AddressID, @ServiceID);
        SET @Counter = @Counter + 1     
        END
 
        Else 
 
        Begin
        SET @Counter = @Counter + 1     
        end
 
 
 
 
END
 
 
 
 
set @Output= 'Success'   -- to set the output parameter
print @Output            -- to print it to screen
--Return @PackageID
 
 
 
 
END

Open in new window

0
 

Author Comment

by:Juliafrazer
ID: 22830179
Thanks,

but this function is the second call, after a stored procedure, which then calls another function as you can see.

The problem is, when i convert the above function to a strored procecure, i cant call it from another stored procedure...or can i?

Thanks

Julia
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22830303
absolutely....you can call another proc from one proc


create proc allprocs
as
exec proc1
exec proc2
exec proc3
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL page split per second is high 19 137
Query to return total 6 26
How come this XML node is not read? 3 51
Applying Roles in Common Scenarios 3 42
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question