Stored Procedure with two raiserrors












1















I use SQL Server.



I want to write a stored procedure that looks if a questionid and employeeid exists (questionid is in the table question, same for employeeid, is in the table employee) AND looks if they not already existing in the table (you don't get a duplicate in the table contentment). I want raiserror's for the user if so.



In my case it is possible to have a duplicate but not on the same DATE!



Contentment table has columns:



employeeid, questionid, date, score


employeeid, questionid, date make up the primary key.



So I want something like this:



1,1, 18-11-2018, null
1,1, 19-11-2018, null


and not something like this:



1,1, 18-11-2018, null
1,1, 18-11-2018, null


I already made something but it is not working (1-1-1900 is a standard date, because it is primary key it needs to be inserted, score is not inserted because the user needs to do this):



    @employeeid int,
@questionid int
as
begin
if exists (select * from question where questionid = @questionid)
and exists (select * from employee where employeeid= @employeeid)
begin
insert into contentment (employeeid, questionid, date, score)
values (@employeeid, @questionid, '1-1-1900', null)
end

if (select count(*)
from contentment
where employeeid = @employeeid
and questionid = @questionid
and date = date) = 0
raiserror ('@employeeid or @questionid already existing', 16, 1)
else
raiserror ('@employeeid or @questionid are not existing', 16, 1, null)
end









share|improve this question




















  • 1





    Hi there, tag your database to get proper attention. Different RDBMs have different syntax and even commands

    – Jorge Campos
    Nov 19 '18 at 17:49






  • 1





    @JorgeCampos Sorry I forgot to mention, just did.

    – DutchFatBoys
    Nov 19 '18 at 17:53
















1















I use SQL Server.



I want to write a stored procedure that looks if a questionid and employeeid exists (questionid is in the table question, same for employeeid, is in the table employee) AND looks if they not already existing in the table (you don't get a duplicate in the table contentment). I want raiserror's for the user if so.



In my case it is possible to have a duplicate but not on the same DATE!



Contentment table has columns:



employeeid, questionid, date, score


employeeid, questionid, date make up the primary key.



So I want something like this:



1,1, 18-11-2018, null
1,1, 19-11-2018, null


and not something like this:



1,1, 18-11-2018, null
1,1, 18-11-2018, null


I already made something but it is not working (1-1-1900 is a standard date, because it is primary key it needs to be inserted, score is not inserted because the user needs to do this):



    @employeeid int,
@questionid int
as
begin
if exists (select * from question where questionid = @questionid)
and exists (select * from employee where employeeid= @employeeid)
begin
insert into contentment (employeeid, questionid, date, score)
values (@employeeid, @questionid, '1-1-1900', null)
end

if (select count(*)
from contentment
where employeeid = @employeeid
and questionid = @questionid
and date = date) = 0
raiserror ('@employeeid or @questionid already existing', 16, 1)
else
raiserror ('@employeeid or @questionid are not existing', 16, 1, null)
end









share|improve this question




















  • 1





    Hi there, tag your database to get proper attention. Different RDBMs have different syntax and even commands

    – Jorge Campos
    Nov 19 '18 at 17:49






  • 1





    @JorgeCampos Sorry I forgot to mention, just did.

    – DutchFatBoys
    Nov 19 '18 at 17:53














1












1








1








I use SQL Server.



I want to write a stored procedure that looks if a questionid and employeeid exists (questionid is in the table question, same for employeeid, is in the table employee) AND looks if they not already existing in the table (you don't get a duplicate in the table contentment). I want raiserror's for the user if so.



In my case it is possible to have a duplicate but not on the same DATE!



Contentment table has columns:



employeeid, questionid, date, score


employeeid, questionid, date make up the primary key.



So I want something like this:



1,1, 18-11-2018, null
1,1, 19-11-2018, null


and not something like this:



1,1, 18-11-2018, null
1,1, 18-11-2018, null


I already made something but it is not working (1-1-1900 is a standard date, because it is primary key it needs to be inserted, score is not inserted because the user needs to do this):



    @employeeid int,
@questionid int
as
begin
if exists (select * from question where questionid = @questionid)
and exists (select * from employee where employeeid= @employeeid)
begin
insert into contentment (employeeid, questionid, date, score)
values (@employeeid, @questionid, '1-1-1900', null)
end

if (select count(*)
from contentment
where employeeid = @employeeid
and questionid = @questionid
and date = date) = 0
raiserror ('@employeeid or @questionid already existing', 16, 1)
else
raiserror ('@employeeid or @questionid are not existing', 16, 1, null)
end









share|improve this question
















I use SQL Server.



I want to write a stored procedure that looks if a questionid and employeeid exists (questionid is in the table question, same for employeeid, is in the table employee) AND looks if they not already existing in the table (you don't get a duplicate in the table contentment). I want raiserror's for the user if so.



In my case it is possible to have a duplicate but not on the same DATE!



Contentment table has columns:



employeeid, questionid, date, score


employeeid, questionid, date make up the primary key.



So I want something like this:



1,1, 18-11-2018, null
1,1, 19-11-2018, null


and not something like this:



1,1, 18-11-2018, null
1,1, 18-11-2018, null


I already made something but it is not working (1-1-1900 is a standard date, because it is primary key it needs to be inserted, score is not inserted because the user needs to do this):



    @employeeid int,
@questionid int
as
begin
if exists (select * from question where questionid = @questionid)
and exists (select * from employee where employeeid= @employeeid)
begin
insert into contentment (employeeid, questionid, date, score)
values (@employeeid, @questionid, '1-1-1900', null)
end

if (select count(*)
from contentment
where employeeid = @employeeid
and questionid = @questionid
and date = date) = 0
raiserror ('@employeeid or @questionid already existing', 16, 1)
else
raiserror ('@employeeid or @questionid are not existing', 16, 1, null)
end






sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:44









marc_s

574k12811091256




574k12811091256










asked Nov 19 '18 at 17:42









DutchFatBoysDutchFatBoys

487




487








  • 1





    Hi there, tag your database to get proper attention. Different RDBMs have different syntax and even commands

    – Jorge Campos
    Nov 19 '18 at 17:49






  • 1





    @JorgeCampos Sorry I forgot to mention, just did.

    – DutchFatBoys
    Nov 19 '18 at 17:53














  • 1





    Hi there, tag your database to get proper attention. Different RDBMs have different syntax and even commands

    – Jorge Campos
    Nov 19 '18 at 17:49






  • 1





    @JorgeCampos Sorry I forgot to mention, just did.

    – DutchFatBoys
    Nov 19 '18 at 17:53








1




1





Hi there, tag your database to get proper attention. Different RDBMs have different syntax and even commands

– Jorge Campos
Nov 19 '18 at 17:49





Hi there, tag your database to get proper attention. Different RDBMs have different syntax and even commands

– Jorge Campos
Nov 19 '18 at 17:49




1




1





@JorgeCampos Sorry I forgot to mention, just did.

– DutchFatBoys
Nov 19 '18 at 17:53





@JorgeCampos Sorry I forgot to mention, just did.

– DutchFatBoys
Nov 19 '18 at 17:53












1 Answer
1






active

oldest

votes


















0














If you want to date validation you need to provide @date as well. I have created a sample stored procedure with detail that you provide:



DROP PROCEDURE P_ContentmentInsert
GO
CREATE PROCEDURE P_ContentmentInsert
@employeeid int,
@questionid int,
@date DATE
AS
BEGIN
--Check if exists Employee, Question and Contentment
DECLARE @IsEmployeeExists INT=ISNULL((SELECT COUNT(1) FROM employee WHERE employeeid= @employeeid),0)
DECLARE @IsQuestionExists INT=ISNULL((SELECT COUNT(1) FROM question WHERE questionid = @questionid),0)
DECLARE @IsContentmentExists INT=ISNULL((SELECT COUNT(1) FROM contentment WHERE questionid = @questionid and employeeid= @employeeid and [date]=@date),0)
DECLARE @ErrorMessage VARCHAR(1000)=''

--If one of the validation not passed give error message
IF (@IsEmployeeExists=0 OR @IsQuestionExists=0 OR @IsContentmentExists=0)
BEGIN
IF @IsEmployeeExists=0
SET @ErrorMessage='-EmployeeId Not exists'
IF @IsQuestionExists=0
SET @ErrorMessage=@ErrorMessage+'-QuesitonId Not exists'
IF @IsContentmentExists=0
SET @ErrorMessage=@ErrorMessage+'-Contentment already exists'
RETURN
END

--If there is no problem insert it.
IF @IsEmployeeExists>0 and @IsQuestionExists>0 and @IsContentmentExists>0
BEGIN
INSERT INTO contentment (employeeid, questionid, date, score)
VALUES (@employeeid, @questionid, @date, null)
END

END





share|improve this answer
























  • Why I need to provide date as well then?

    – DutchFatBoys
    Nov 19 '18 at 19:02











  • Because you want to validate date as well. You said you have three primary key QuestionId, EmployeeId and Date in contentment table. You need to provide Date to validate it.

    – Zeki Gumus
    Nov 19 '18 at 19:11











  • See my example, can I do it that way?

    – DutchFatBoys
    Nov 19 '18 at 19:20











  • You'd need to clarify under what circumstances you enter an actual date, because the code you show always uses the same date, and therefore will always cause an error the second time around. You can do the check after the insert, but then you need to rollback the transaction as well. Much better to test first if you can.

    – Dale Burrell
    Nov 19 '18 at 20:42











  • Some more questions: why do you do INT=ISNULL (Select Count(1) ...... )? And @ErrorMessage = @ErrorMessage+ ..... ? Thanks!

    – DutchFatBoys
    Nov 20 '18 at 8:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380016%2fstored-procedure-with-two-raiserrors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you want to date validation you need to provide @date as well. I have created a sample stored procedure with detail that you provide:



DROP PROCEDURE P_ContentmentInsert
GO
CREATE PROCEDURE P_ContentmentInsert
@employeeid int,
@questionid int,
@date DATE
AS
BEGIN
--Check if exists Employee, Question and Contentment
DECLARE @IsEmployeeExists INT=ISNULL((SELECT COUNT(1) FROM employee WHERE employeeid= @employeeid),0)
DECLARE @IsQuestionExists INT=ISNULL((SELECT COUNT(1) FROM question WHERE questionid = @questionid),0)
DECLARE @IsContentmentExists INT=ISNULL((SELECT COUNT(1) FROM contentment WHERE questionid = @questionid and employeeid= @employeeid and [date]=@date),0)
DECLARE @ErrorMessage VARCHAR(1000)=''

--If one of the validation not passed give error message
IF (@IsEmployeeExists=0 OR @IsQuestionExists=0 OR @IsContentmentExists=0)
BEGIN
IF @IsEmployeeExists=0
SET @ErrorMessage='-EmployeeId Not exists'
IF @IsQuestionExists=0
SET @ErrorMessage=@ErrorMessage+'-QuesitonId Not exists'
IF @IsContentmentExists=0
SET @ErrorMessage=@ErrorMessage+'-Contentment already exists'
RETURN
END

--If there is no problem insert it.
IF @IsEmployeeExists>0 and @IsQuestionExists>0 and @IsContentmentExists>0
BEGIN
INSERT INTO contentment (employeeid, questionid, date, score)
VALUES (@employeeid, @questionid, @date, null)
END

END





share|improve this answer
























  • Why I need to provide date as well then?

    – DutchFatBoys
    Nov 19 '18 at 19:02











  • Because you want to validate date as well. You said you have three primary key QuestionId, EmployeeId and Date in contentment table. You need to provide Date to validate it.

    – Zeki Gumus
    Nov 19 '18 at 19:11











  • See my example, can I do it that way?

    – DutchFatBoys
    Nov 19 '18 at 19:20











  • You'd need to clarify under what circumstances you enter an actual date, because the code you show always uses the same date, and therefore will always cause an error the second time around. You can do the check after the insert, but then you need to rollback the transaction as well. Much better to test first if you can.

    – Dale Burrell
    Nov 19 '18 at 20:42











  • Some more questions: why do you do INT=ISNULL (Select Count(1) ...... )? And @ErrorMessage = @ErrorMessage+ ..... ? Thanks!

    – DutchFatBoys
    Nov 20 '18 at 8:27
















0














If you want to date validation you need to provide @date as well. I have created a sample stored procedure with detail that you provide:



DROP PROCEDURE P_ContentmentInsert
GO
CREATE PROCEDURE P_ContentmentInsert
@employeeid int,
@questionid int,
@date DATE
AS
BEGIN
--Check if exists Employee, Question and Contentment
DECLARE @IsEmployeeExists INT=ISNULL((SELECT COUNT(1) FROM employee WHERE employeeid= @employeeid),0)
DECLARE @IsQuestionExists INT=ISNULL((SELECT COUNT(1) FROM question WHERE questionid = @questionid),0)
DECLARE @IsContentmentExists INT=ISNULL((SELECT COUNT(1) FROM contentment WHERE questionid = @questionid and employeeid= @employeeid and [date]=@date),0)
DECLARE @ErrorMessage VARCHAR(1000)=''

--If one of the validation not passed give error message
IF (@IsEmployeeExists=0 OR @IsQuestionExists=0 OR @IsContentmentExists=0)
BEGIN
IF @IsEmployeeExists=0
SET @ErrorMessage='-EmployeeId Not exists'
IF @IsQuestionExists=0
SET @ErrorMessage=@ErrorMessage+'-QuesitonId Not exists'
IF @IsContentmentExists=0
SET @ErrorMessage=@ErrorMessage+'-Contentment already exists'
RETURN
END

--If there is no problem insert it.
IF @IsEmployeeExists>0 and @IsQuestionExists>0 and @IsContentmentExists>0
BEGIN
INSERT INTO contentment (employeeid, questionid, date, score)
VALUES (@employeeid, @questionid, @date, null)
END

END





share|improve this answer
























  • Why I need to provide date as well then?

    – DutchFatBoys
    Nov 19 '18 at 19:02











  • Because you want to validate date as well. You said you have three primary key QuestionId, EmployeeId and Date in contentment table. You need to provide Date to validate it.

    – Zeki Gumus
    Nov 19 '18 at 19:11











  • See my example, can I do it that way?

    – DutchFatBoys
    Nov 19 '18 at 19:20











  • You'd need to clarify under what circumstances you enter an actual date, because the code you show always uses the same date, and therefore will always cause an error the second time around. You can do the check after the insert, but then you need to rollback the transaction as well. Much better to test first if you can.

    – Dale Burrell
    Nov 19 '18 at 20:42











  • Some more questions: why do you do INT=ISNULL (Select Count(1) ...... )? And @ErrorMessage = @ErrorMessage+ ..... ? Thanks!

    – DutchFatBoys
    Nov 20 '18 at 8:27














0












0








0







If you want to date validation you need to provide @date as well. I have created a sample stored procedure with detail that you provide:



DROP PROCEDURE P_ContentmentInsert
GO
CREATE PROCEDURE P_ContentmentInsert
@employeeid int,
@questionid int,
@date DATE
AS
BEGIN
--Check if exists Employee, Question and Contentment
DECLARE @IsEmployeeExists INT=ISNULL((SELECT COUNT(1) FROM employee WHERE employeeid= @employeeid),0)
DECLARE @IsQuestionExists INT=ISNULL((SELECT COUNT(1) FROM question WHERE questionid = @questionid),0)
DECLARE @IsContentmentExists INT=ISNULL((SELECT COUNT(1) FROM contentment WHERE questionid = @questionid and employeeid= @employeeid and [date]=@date),0)
DECLARE @ErrorMessage VARCHAR(1000)=''

--If one of the validation not passed give error message
IF (@IsEmployeeExists=0 OR @IsQuestionExists=0 OR @IsContentmentExists=0)
BEGIN
IF @IsEmployeeExists=0
SET @ErrorMessage='-EmployeeId Not exists'
IF @IsQuestionExists=0
SET @ErrorMessage=@ErrorMessage+'-QuesitonId Not exists'
IF @IsContentmentExists=0
SET @ErrorMessage=@ErrorMessage+'-Contentment already exists'
RETURN
END

--If there is no problem insert it.
IF @IsEmployeeExists>0 and @IsQuestionExists>0 and @IsContentmentExists>0
BEGIN
INSERT INTO contentment (employeeid, questionid, date, score)
VALUES (@employeeid, @questionid, @date, null)
END

END





share|improve this answer













If you want to date validation you need to provide @date as well. I have created a sample stored procedure with detail that you provide:



DROP PROCEDURE P_ContentmentInsert
GO
CREATE PROCEDURE P_ContentmentInsert
@employeeid int,
@questionid int,
@date DATE
AS
BEGIN
--Check if exists Employee, Question and Contentment
DECLARE @IsEmployeeExists INT=ISNULL((SELECT COUNT(1) FROM employee WHERE employeeid= @employeeid),0)
DECLARE @IsQuestionExists INT=ISNULL((SELECT COUNT(1) FROM question WHERE questionid = @questionid),0)
DECLARE @IsContentmentExists INT=ISNULL((SELECT COUNT(1) FROM contentment WHERE questionid = @questionid and employeeid= @employeeid and [date]=@date),0)
DECLARE @ErrorMessage VARCHAR(1000)=''

--If one of the validation not passed give error message
IF (@IsEmployeeExists=0 OR @IsQuestionExists=0 OR @IsContentmentExists=0)
BEGIN
IF @IsEmployeeExists=0
SET @ErrorMessage='-EmployeeId Not exists'
IF @IsQuestionExists=0
SET @ErrorMessage=@ErrorMessage+'-QuesitonId Not exists'
IF @IsContentmentExists=0
SET @ErrorMessage=@ErrorMessage+'-Contentment already exists'
RETURN
END

--If there is no problem insert it.
IF @IsEmployeeExists>0 and @IsQuestionExists>0 and @IsContentmentExists>0
BEGIN
INSERT INTO contentment (employeeid, questionid, date, score)
VALUES (@employeeid, @questionid, @date, null)
END

END






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:15









Zeki GumusZeki Gumus

1,372212




1,372212













  • Why I need to provide date as well then?

    – DutchFatBoys
    Nov 19 '18 at 19:02











  • Because you want to validate date as well. You said you have three primary key QuestionId, EmployeeId and Date in contentment table. You need to provide Date to validate it.

    – Zeki Gumus
    Nov 19 '18 at 19:11











  • See my example, can I do it that way?

    – DutchFatBoys
    Nov 19 '18 at 19:20











  • You'd need to clarify under what circumstances you enter an actual date, because the code you show always uses the same date, and therefore will always cause an error the second time around. You can do the check after the insert, but then you need to rollback the transaction as well. Much better to test first if you can.

    – Dale Burrell
    Nov 19 '18 at 20:42











  • Some more questions: why do you do INT=ISNULL (Select Count(1) ...... )? And @ErrorMessage = @ErrorMessage+ ..... ? Thanks!

    – DutchFatBoys
    Nov 20 '18 at 8:27



















  • Why I need to provide date as well then?

    – DutchFatBoys
    Nov 19 '18 at 19:02











  • Because you want to validate date as well. You said you have three primary key QuestionId, EmployeeId and Date in contentment table. You need to provide Date to validate it.

    – Zeki Gumus
    Nov 19 '18 at 19:11











  • See my example, can I do it that way?

    – DutchFatBoys
    Nov 19 '18 at 19:20











  • You'd need to clarify under what circumstances you enter an actual date, because the code you show always uses the same date, and therefore will always cause an error the second time around. You can do the check after the insert, but then you need to rollback the transaction as well. Much better to test first if you can.

    – Dale Burrell
    Nov 19 '18 at 20:42











  • Some more questions: why do you do INT=ISNULL (Select Count(1) ...... )? And @ErrorMessage = @ErrorMessage+ ..... ? Thanks!

    – DutchFatBoys
    Nov 20 '18 at 8:27

















Why I need to provide date as well then?

– DutchFatBoys
Nov 19 '18 at 19:02





Why I need to provide date as well then?

– DutchFatBoys
Nov 19 '18 at 19:02













Because you want to validate date as well. You said you have three primary key QuestionId, EmployeeId and Date in contentment table. You need to provide Date to validate it.

– Zeki Gumus
Nov 19 '18 at 19:11





Because you want to validate date as well. You said you have three primary key QuestionId, EmployeeId and Date in contentment table. You need to provide Date to validate it.

– Zeki Gumus
Nov 19 '18 at 19:11













See my example, can I do it that way?

– DutchFatBoys
Nov 19 '18 at 19:20





See my example, can I do it that way?

– DutchFatBoys
Nov 19 '18 at 19:20













You'd need to clarify under what circumstances you enter an actual date, because the code you show always uses the same date, and therefore will always cause an error the second time around. You can do the check after the insert, but then you need to rollback the transaction as well. Much better to test first if you can.

– Dale Burrell
Nov 19 '18 at 20:42





You'd need to clarify under what circumstances you enter an actual date, because the code you show always uses the same date, and therefore will always cause an error the second time around. You can do the check after the insert, but then you need to rollback the transaction as well. Much better to test first if you can.

– Dale Burrell
Nov 19 '18 at 20:42













Some more questions: why do you do INT=ISNULL (Select Count(1) ...... )? And @ErrorMessage = @ErrorMessage+ ..... ? Thanks!

– DutchFatBoys
Nov 20 '18 at 8:27





Some more questions: why do you do INT=ISNULL (Select Count(1) ...... )? And @ErrorMessage = @ErrorMessage+ ..... ? Thanks!

– DutchFatBoys
Nov 20 '18 at 8:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380016%2fstored-procedure-with-two-raiserrors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?