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.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How To Include Second Field in A Function?

Author  Topic 

Palermo
Starting Member

25 Posts

Posted - 2013-11-16 : 13:29:00
I have created a function that returns the high score from a field in a table :

CREATE FUNCTION AssessSchema.fn_calcHigh(@p bigint)
Returns int
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @high int
SELECT @high = (SELECT MAX(score)
FROM AssessSchema.scores
WHERE score = @p)
RETURN @high
END
GO

Player i.e. the players name is a field in the same table so how do I include it in this function?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-11-16 : 23:07:48
You can't. A scalar function can only return a single value. You'll need to do it another way. a table valued function, or stored procedure, or select statement, or view, or anything other than a scalar function.

Be One with the Optimizer
TG
Go to Top of Page

Palermo
Starting Member

25 Posts

Posted - 2013-11-17 : 07:53:58
quote:
Originally posted by TG

You can't. A scalar function can only return a single value. You'll need to do it another way. a table valued function, or stored procedure, or select statement, or view, or anything other than a scalar function.

Be One with the Optimizer
TG



Ok thank you for the reply. I will approach this another way.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-17 : 11:41:21
you should ideally make it a procedure which will return highest score and also player who got it.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Palermo
Starting Member

25 Posts

Posted - 2013-11-23 : 08:19:56
This is how I did it :

CREATE TRIGGER highscore
ON AssessSchema.scores
AFTER INSERT, UPDATE
AS
DECLARE @score int
SET @score = (SELECT score FROM INSERTED)
IF @score > 100 OR @score <0
BEGIN
DELETE FROM Result
WHERE score = @score
PRINT 'insert error score < 0 or > 100'
END
ELSE
BEGIN
PRINT 'insert success'
END
GO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-23 : 09:34:59
quote:
Originally posted by Palermo

This is how I did it :

CREATE TRIGGER highscore
ON AssessSchema.scores
AFTER INSERT, UPDATE
AS
DECLARE @score int
SET @score = (SELECT score FROM INSERTED)
IF @score > 100 OR @score <0
BEGIN
DELETE FROM Result
WHERE score = @score
PRINT 'insert error score < 0 or > 100'
END
ELSE
BEGIN
PRINT 'insert success'
END
GO


you're assuming only single record will be inserted/updated in one time which may not always be true. So INSERTED table can have multiple records
SO trigger should be like below


CREATE TRIGGER highscore
ON AssessSchema.scores
AFTER INSERT, UPDATE
AS
IF EXISTS(
SELECT score FROM INSERTED
WHERE score > 100
OR score <0
)
BEGIN
DELETE FROM Result r
INNER JOIN INSERTED i
ON i.PK = r.PK
WHERE i.score > 100
OR i.score < 0
PRINT 'insert error score < 0 or > 100'
END
ELSE
BEGIN
PRINT 'insert success'
END
GO


Where PK is the primary key of the table
One thing to note here is that even if you update a score value to be < 0 or > 100 the way its written trigger will cause record to be deleted. is that what you really want to happen?
or you could even make it INSTEAD OF trigger so that INSERT doesnt happen at all


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -