0

enter image description here

Those are my diagrams - I create procedure

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[hasta]
    @Ad nchar(50),
    @Soyadı nvarchar(100),
    @TcKimlik nchar(11), 
    @DogumTarihi varchar(8),
    @TelefonNo nvarchar(11) 
AS 
BEGIN 
    SET NOCOUNT ON;
   
    INSERT INTO [dbo].Hastalar ([Ad], [Soyadı], [TcKimlik], [DogumTarihi], [TelefonNo])
    VALUES (@Ad, @Soyadı, @TcKimlik, @DogumTarihi, @TelefonNo)
END

After I want add data in Hastalar table, I write

exec  hasta  'Emir','Yılmaz','35635993564','1995.11.19','05347331085'

but I get this error

Msg 547, Level 16, State 0, Procedure hasta, Line 13
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Hastalar_Testler". The conflict occurred in database "hastane", table "dbo.Testler", column 'TestID'. The statement has been terminated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Emir Yılmaz
  • 5
  • 1
  • 4

2 Answers2

1

In your table [dbo].Hastalar, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.

If you have SQL Server Management Studio, open it up and sp_help '[dbo].Hastalar'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.

Thiyagu
  • 1,260
  • 1
  • 5
  • 14
-2

What part of the error do you not understand?

The message is saying that one of the columns -- "TestId" -- is referring to another table. And the value being inserted is not already in that table.

You are not explicitly inserting a value for TestId, so that value is being set using a default constraint or trigger. I would suggest that you explicit provide the value in the insert -- even if that value is NULL.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786