Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all

I created this table

SQL
a | b | c |  total  | evaluation  
1 | 2 | 3 |    6    | 
5 | 2 | 8 |    15   |      


--------------> " Total" is computed column is result for sum (a+b+c)

I want to check constraint on Total column
if total = 12 set evaluation ='Perfect'
else set evaluation = 'weak'

Posted
Comments
shms_rony 29-Sep-13 6:44am    
please help me
Azee 29-Sep-13 9:21am    
Please post your stored procedure where you want to apply the above statements.
shms_rony 29-Sep-13 9:39am    
ALTER PROCEDURE dbo.ManageLaborEval
(

@a int,
@b int,
@c int



)

AS


INSERT INTO Table1(a, b, c) VALUES (@a,@b,@c)




RETURN
shms_rony 29-Sep-13 9:40am    
total column is computed column

Formula --> (([a]+[b])+[c]))

Try this:

SQL
ALTER PROCEDURE dbo.ManageLaborEval
	(
@a int,
@b int,
@c int
)

AS
BEGIN

	Declare @sum bigint;
	Set @sum = @a + @b + @c;

	Declare @Evaluation NVARCHAR(30);
	IF @sum = 12
	BEGIN
		Set @Evaluation = 'Perfect';
	END
	ELSE
	BEGIN
		Set @Evaluation = 'weak';
	END
		
	INSERT INTO Table1(a, b, c, total, evaluation) VALUES (@a,@b,@c, @sum, @Evaluation)

END

Hope it helps,

Azee...
 
Share this answer
 
This solution will work whether one or multiple rows are updated or inserted in a transaction. It is a trigger that will run automaticaly whenever one or more rows are inserted or one or more rows are updated.


USE [YourDatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].CheckTheTotal]
ON [dbo].[YourTableName]
AFTER INSERT, UPDATE
AS
BEGIN
UPDATE YourTableName SET Evaluation = CASE WHEN total=12 THEN 'Perfect' ELSE 'Weak' END WHERE YourTableName.YourPrimaryKeyName IN (SELECT YourPrimaryKeyName FROM INSERTED);
END
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900