SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How To Include Second Field in A Function?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Palermo
Starting Member

25 Posts

Posted - 11/16/2013 :  13:29:00  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 11/16/2013 :  23:07:48  Show Profile  Reply with Quote
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 - 11/17/2013 :  07:53:58  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 11/17/2013 :  11:41:21  Show Profile  Reply with Quote
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 - 11/23/2013 :  08:19:56  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 11/23/2013 :  09:34:59  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000