Hi,
I'm trying to create the following function:
CREATE FUNCTION dbo.get_id(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SET @.id = (SELECT id FROM dbo.language WHERE name = @.name)
IF @.@.ROWCOUNT = 0
RAISERROR('Specified item does not exist', 16, 1)
RETURN @.id
END
GO
And I get the following error message:
Invalid use of side-effecting or time-dependent operator in 'RAISERROR' within a function.
I can't find any references to the use of RAISERROR within functions or stored procedures, so I have no idea why this is not working.
What I'm trying to do is check for the existence of the row inside the function and not outside, on all the places I need to call this function. I need to have an exception in my code if this happens, that's why I thought RAISERROR is what I needed...
Any help would be appreciated!
Thanks,
Florin
Thanks, I eventually figured out that I can't use RAISERROR within a function, so I changed it to a stored procedure. My problem now is that RAISERROR doesn't stop the execution of my stored procedure, but continues with the next statement. Is there a way to achieve the same kind of functionality as with primary key or constraint violations? I'm testing some conditions inside my stored procedure and I want to stop the execution under certain circumstances and get an exception in my .NET calling code. Is that at all possible with RAISERROR?
I noticed that when an error is produced by the SQL Server engine, besides the red error message (which I can also create using RAISERROR) there is another message saying "The statement has been terminated". How can I achieve the same? :-)
Thanks, Florin
|||While you can't raise an error in a udf. . .this function:
CREATE FUNCTION dbo.get_id(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SELECT @.id = id FROM dbo.language WHERE name = @.name
RETURN @.id
END
GO
will return a null if name is not found.
Now, I contend, trying to select an id for a name that doesn't exist shouldn't be an error. . . It should return a null. Trying to use the null in a place that requires non-null will raise the error. . . and that might make perfect logical sense.
Just a thought.
On the other hand, if you want to use a procedure, look up @.@.ERROR in the help file.|||
Well, in the application logic, the foreign key can be null, which means that record has no references, but in this particular case I need to throw an error because the calling client has given a foreign key value that does not exist. Null is a valid case, non-existing referenced record is not. That's why I need the error. I want to do as much as I can on the database in one stored procedure call, to avoid multiple database calls/roundtrips...
Florin
|||huh? I think you misunderstood. . .
run this script. . .
-- BEGIN SCRIPT =========================
create table Song(ID int not null identity(1,1) primary key, name varchar(255))
go
create table Band(ID int not null identity(1,1) primary key, name varchar(255))
go
create table Record(BandID int not null references Band(ID), SongID int not null references Song(ID) )
go
CREATE FUNCTION dbo.getSongID(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SELECT @.id = id FROM dbo.Song WHERE name = @.name
RETURN @.id
END
go
CREATE FUNCTION dbo.getBandID(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SELECT @.id = id FROM dbo.Band WHERE name = @.name
RETURN @.id
END
go
Insert into Song(Name) values('Love Will Tear Us Apart')
Insert into Band(Name) values('Joy Division')
-- This works
Insert into Record values(dbo.getBandID('Joy Division'), dbo.getSongID('Love Will Tear Us Apart'))
-- This doesn't and the error is on the database
Insert into Record values(dbo.getBandID('Captain and Tennille'), dbo.getSongID('Love Will Tear Us Apart'))
-- This doesn't and the error is on the database
Insert into Record values(dbo.getBandID('Joy Division'), dbo.getSongID('Love Will Keep Us Together'))
-- END SCRIPT =========================
No comments:
Post a Comment