Hello All,
I am having three table tblTest,tblTestQuestion,tblAnswers
Each test can have multiple question and each Question can have multiple answers.
Now I am already having records in database. I wants to create clone copy of existing test except testdetails in tblTest because
the test will be unique, and then insert questions and answers into their respective tables.
I was trying to create SP but stuck.
Here is SP
ALTER PROCEDURE [dbo].[usp_CreateDuplicateTest] @testName varchar(500), @testId bigint, @userId bigint, @retValue int OUTPUT AS BEGIN SET NOCOUNT ON; declare @insertedTestId bigint if not EXISTS(SELECT testName from tblTest WHERE UPPER(@testName) = UPPER(@testName)) BEGIN INSERT INTO tblTest SELECT @userId, testName,duration,totalQuestion,termsCondition,0,GETUTCDATE(),GETUTCDATE() from tblTest WHERE id=@testId SET @insertedTestId=@@identity if(SELECT COUNT(*) from tblTestQuestion WHERE testId= @testId)>0 BEGIN INSERT INTO tblTestQuestion SELECT @insertedTestId,question,0,GETUTCDATE(),GETUTCDATE() from tblTestQuestion WHERE testId=@testId END END END
Please find below tables structure
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblAnswer]( [id] [int] IDENTITY(1,1) NOT NULL, [questionId] [int] NOT NULL, [answer] [nvarchar](500) NOT NULL, [point] [nvarchar](20) NOT NULL, [isCorrect] [bit] NOT NULL, [isDeleted] [bit] NOT NULL, [createdDate] [datetime] NOT NULL, [updatedDate] [datetime] NULL, CONSTRAINT [PK_tblAnswer] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[tblTest] Script Date: 5/24/2015 6:19:15 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblTest]( [id] [int] IDENTITY(1,1) NOT NULL, [userId] [bigint] NOT NULL, [testName] [nvarchar](100) NOT NULL, [duration] [nvarchar](20) NOT NULL, [totalQuestion] [int] NOT NULL, [termsCondition] [text] NOT NULL, [isDeleted] [bit] NOT NULL, [createdDate] [datetime] NOT NULL, [updatedDate] [datetime] NULL, CONSTRAINT [PK_tblTest] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO /****** Object: Table [dbo].[tblTestQuestion] Script Date: 5/24/2015 6:19:15 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblTestQuestion]( [id] [int] IDENTITY(1,1) NOT NULL, [testId] [int] NOT NULL, [question] [nvarchar](500) NOT NULL, [isDeleted] [bit] NOT NULL, [createdDate] [datetime] NOT NULL, [updatedDate] [datetime] NULL, CONSTRAINT [PK_tblTestQuestion] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET IDENTITY_INSERT [dbo].[tblAnswer] ON INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (1, 1, N'Event handling', N'1', 0, 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (2, 1, N'Page_Load', N'1', 0, 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (3, 1, N'Rendering', N'2', 0, 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (4, 1, N'Unload', N'4', 1, 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (5, 3, N'answer1', N'1', 0, 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (7, 3, N'answer3', N'1', 0, 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (54, 29, N'dfgd', N'3453', 0, 0, CAST(0x0000A4970116666F AS DateTime), CAST(0x0000A4970116666F AS DateTime)) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (55, 29, N'fdgdfg', N'45435', 0, 0, CAST(0x0000A4970116666F AS DateTime), CAST(0x0000A4970116666F AS DateTime)) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (58, 29, N'dfgd', N'3453', 0, 0, CAST(0x0000A49800BF7636 AS DateTime), CAST(0x0000A49800BF7636 AS DateTime)) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (59, 29, N'fdgdfg', N'45435', 0, 0, CAST(0x0000A49800BF7636 AS DateTime), CAST(0x0000A49800BF7636 AS DateTime)) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (1066, 1039, N'445', N'45', 0, 0, CAST(0x0000A4A200CCC28E AS DateTime), CAST(0x0000A4A200CCC28E AS DateTime)) INSERT [dbo].[tblAnswer] ([id], [questionId], [answer], [point], [isCorrect], [isDeleted], [createdDate], [updatedDate]) VALUES (1067, 1039, N'fghfh', N'435', 0, 0, CAST(0x0000A4A200CCC28E AS DateTime), CAST(0x0000A4A200CCC28E AS DateTime)) SET IDENTITY_INSERT [dbo].[tblAnswer] OFF SET IDENTITY_INSERT [dbo].[tblTest] ON INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (2, 2, N'Asp.Net', N'40', 2, N'Terms', 0, CAST(0x0000A17600000000 AS DateTime), NULL) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (3, 2, N'Quality Assurance', N'3', 4, N'Terms', 0, CAST(0x0000A17600000000 AS DateTime), NULL) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (11, 2, N'ret', N'retre', 0, N'', 1, CAST(0x0000A49700F52D77 AS DateTime), CAST(0x0000A49700F52D77 AS DateTime)) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (12, 2, N'Testwrere', N'32', 0, N'', 1, CAST(0x0000A49700F54921 AS DateTime), CAST(0x0000A49700F54921 AS DateTime)) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (13, 2, N'gfhgfh', N'gfhg', 1, N'', 1, CAST(0x0000A497010555E7 AS DateTime), CAST(0x0000A497010555E7 AS DateTime)) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (15, 2, N'Asp.Net1', N'23', 1, N'', 1, CAST(0x0000A497010BA322 AS DateTime), CAST(0x0000A497010BA322 AS DateTime)) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (16, 2, N'Asp.Net3', N'23', 1, N'', 0, CAST(0x0000A497010E8D83 AS DateTime), CAST(0x0000A497010E8D83 AS DateTime)) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (17, 2, N'Asp.Net4', N'23', 1, N'', 0, CAST(0x0000A497010F27E8 AS DateTime), CAST(0x0000A497010F27E8 AS DateTime)) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (18, 2, N'Asp.Net6', N'23', 1, N'', 0, CAST(0x0000A497011013A9 AS DateTime), CAST(0x0000A497011013A9 AS DateTime)) INSERT [dbo].[tblTest] ([id], [userId], [testName], [duration], [totalQuestion], [termsCondition], [isDeleted], [createdDate], [updatedDate]) VALUES (19, 2, N'Asp.Net7', N'34', 1, N'', 1, CAST(0x0000A4970116CFB3 AS DateTime), CAST(0x0000A4970116CFB4 AS DateTime)) SET IDENTITY_INSERT [dbo].[tblTest] OFF SET IDENTITY_INSERT [dbo].[tblTestQuestion] ON INSERT [dbo].[tblTestQuestion] ([id], [testId], [question], [isDeleted], [createdDate], [updatedDate]) VALUES (1, 3, N'What is last stage of the web forms lifecycle?', 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblTestQuestion] ([id], [testId], [question], [isDeleted], [createdDate], [updatedDate]) VALUES (3, 3, N'What is polymorphism?', 0, CAST(0x0000A48100000000 AS DateTime), NULL) INSERT [dbo].[tblTestQuestion] ([id], [testId], [question], [isDeleted], [createdDate], [updatedDate]) VALUES (4, 3, N'Asp.Net', 0, CAST(0x0000A4930091F83F AS DateTime), CAST(0x0000A4930091F83F AS DateTime)) INSERT [dbo].[tblTestQuestion] ([id], [testId], [question], [isDeleted], [createdDate], [updatedDate]) VALUES (5, 3, N'QA', 0, CAST(0x0000A4930091F83F AS DateTime), CAST(0x0000A4930091F83F AS DateTime)) INSERT [dbo].[tblTestQuestion] ([id], [testId], [question], [isDeleted], [createdDate], [updatedDate]) VALUES (29, 18, N'fdg', 0, CAST(0x0000A4970116656A AS DateTime), CAST(0x0000A4970116656A AS DateTime)) INSERT [dbo].[tblTestQuestion] ([id], [testId], [question], [isDeleted], [createdDate], [updatedDate]) VALUES (1039, 2, N'New', 0, CAST(0x0000A4A200CCC18A AS DateTime), CAST(0x0000A4A200CCC18A AS DateTime)) SET IDENTITY_INSERT [dbo].[tblTestQuestion] OFF