sql - What does this not like about my AddPartner procedure? -
so i'm getting slew of errors
(1 row(s) affected) msg 2812, level 16, state 62, line 48 not find stored procedure 'addpartner'. msg 2812, level 16, state 62, line 49 not find stored procedure 'addpartner'. msg 2812, level 16, state 62, line 50 not find stored procedure 'addpartner'.
(1 row(s) affected)
(1 row(s) affected) msg 547, level 16, state 0, procedure addanswer, line 114 insert statement conflicted foreign key constraint "fk__answers__partner__145c0a3f". conflict occurred in database "surveydb", table "dbo.partners", column 'id'. statement has been terminated. msg 547, level 16, state 0, procedure addanswer, line 114 insert statement conflicted foreign key constraint "fk__answers__partner__145c0a3f". conflict occurred in database "surveydb", table "dbo.partners", column 'id'. statement has been terminated. msg 547, level 16, state 0, procedure addanswer, line 114 insert statement conflicted foreign key constraint "fk__answers__partner__145c0a3f". conflict occurred in database "surveydb", table "dbo.partners", column 'id'. statement has been terminated. msg 547, level 16, state 0, procedure addanswer, line 114 insert statement conflicted foreign key constraint "fk__answers__partner__145c0a3f". conflict occurred in database "surveydb", table "dbo.partners", column 'id'. statement has been terminated.
and think originate first 3 errors addpartner
, cannot figure out exact source of problem i've looked on syntax plenty of times. on related note, there exist sort of online tool can 1 hints debugging sql errors?
-- create database information -- needed in application, set context create database surveydb; go use surveydb; -- create surveys table go create table surveys ( id int identity(1,1), title nvarchar(100) not null, primary key (id) ); go -- create sprocs adding , deleting surveys create procedure addsurvey @title nvarchar(100) insert surveys (title) values (@title) go create procedure deletesurvey @id int delete surveys id=@id go -- seed surveys table 1 sample survey exec addsurvey @title = "survey numero uno"; go -- create partners table create table partners ( id int identity(1,1), name nvarchar(50) not null, primary key (id) ); go -- create sprocs adding , deleting partners create procedure addparter @name nvarchar(50) insert partners (name) values (@name) go create procedure deletepartner @id int delete partners id=@id go -- seed partners table few samples exec addpartner @name = 'haliburton'; exec addpartner @name = 'berkshite hathaway'; exec addpartner @name = 'infosys'; go -- create questions table. questions associated -- particular survey. if survey deleted -- associated questions. create table questions ( id int identity(1,1), survey_id int, qtext nvarchar(300) not null, primary key (id), foreign key (survey_id) references surveys(id) on delete cascade ); go -- create sprocs adding , deleting questions. create procedure addquestion @survey_id int, @qtext nvarchar(300) insert questions (survey_id, qtext) values (@survey_id, @qtext) go create procedure deletequestion @id int delete questions id=@id go -- seed questions table sample questions. exec addquestion @survey_id = 1, @qtext = 'what average velocity of african swallow?'; exec addquestion @survey_id = 1, @qtext = 'how hot she, on scale of 1.0-100.0?'; go -- create table answers. answers associated both -- question , partner. if either question or partner -- deleted associated answers. create table answers ( id int identity (1,1), question_id int, partner_id int, val decimal not null, primary key (id), foreign key (question_id) references questions(id) on delete cascade, foreign key (partner_id) references partners(id) on delete cascade ); go -- create sprocs adding , deleting answers. create procedure addanswer @question_id int, @partner_id int, @val decimal insert answers (question_id, partner_id, val) values (@question_id, @partner_id, @val) go create procedure deleteanswer @id int delete answers id=@id go -- seed questions table sample answers. exec addanswer @question_id = 1, @partner_id = 1, @val = 23.3; exec addanswer @question_id = 2, @partner_id = 1, @val = 99.5; exec addanswer @question_id = 1, @partner_id = 2, @val = 0.12345; exec addanswer @question_id = 2, @partner_id = 2, @val = 0.19;
the problem in addanswer
procedure. you're adding partner id's don't exist in partner table, referential integrity fails. in partner table ( select * partners
) , see id's are.
other that, i'd 1) need add checking before inserting rows. if try add 'berkshire hathaway' twice, it's going error. check first , don't have problem:
if not exists ( select * partners [name] = @name ) begin insert partners (name) values (@name) end
also: should consider not using column named name
- call partnername or something, sure, having random name
columns doesn't readability , may cause syntax-checking problems down road.
edit: or, jamie points out, correct typo in stored procedure name.
Comments
Post a Comment