Hi, I am using Stored procedure to enter data in the table. Following is my code Of table definition and stored procedure.
CREATE TABLE [StarBazar].[ProductSale] (
[ProductSaleId] INT IDENTITY (1, 1) NOT NULL,
[ProductId] INT NOT NULL,
[C_Id] INT NOT NULL,
[Quantity] INT NOT NULL,
[DateOfSale] DATETIME2 (7) NOT NULL,
PRIMARY KEY CLUSTERED ([ProductSaleId] ASC),
FOREIGN KEY ([ProductId]) REFERENCES [StarBazar].[Products] ([Id]),
FOREIGN KEY ([C_Id]) REFERENCES [StarBazar].[ClientDetails] ([ClientId])
);
CREATE PROCEDURE [dbo].[usp_InsertProductSale]
@schemaname nvarchar(20),
@productid int,
@clientid int,
@quantity int
AS
declare @sql as nvarchar(max)
set @sql=N'insert into [' +@schemaname + N'].[ProductSale](ProductId,C_Id,Quantity,DateOfSale)
values(' +CONVERT(NVARCHAR,@productid) + N','+CONVERT(NVARCHAR,@clientid) + N','
+ CONVERT(NVARCHAR,@quantity)+ N','''+CONVERT(nvarchar,SYSDATETIME()) +N''')'
print (@sql)
exec (@sql)
RETURN 0
When I execute the procedure, it gives the following error.
insert into [StarBazar].[ProductSale](ProductId,C_Id,Quantity,DateOfSale)
values(1,1,1,'2015-06-25 10:30:35.0210666')
(1 row(s) affected)
Msg 547, Level 16, State 0, Line 3
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__ProductSa__Clien__56B3DD81". The conflict occurred in database "ReportSystem", table "StarBazar.ClientDetails", column 'ClientId'.
The statement has been terminated.
Help me to solve this.