Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
soorma
Yak Posting Veteran
52 Posts |
Posted - 2010-08-02 : 14:27:20
|
I am trying to copy the data into same table. I have 3 tables.There are 3 things i need to do. The first 2 things i was able to do it.1) Insert into the survey table and the get the surveyid as @@identity. 2) Then i used the @@identity to insert into the SurveyQuestions table(Copy the data into the same table) with the new surveyID.3) Which i am not able to do it is copy the data again into surveychoices table. It is how the data is stored in the table.This is how the data looks likeSurveysurveyid= 1 (Auto number)title= test titledescription= test titlesurveystatus=1CreatedBy=testdate= today's dateSurveyQuestionsQuestionsid= 1 (autonumber)SurveyID=1Questions="Good Work"AnswerType= MQuestionsid= 2 (autonumber)SurveyID=1Questions="bad Work"AnswerType= SSurveyChoicesChoiceID=1(autonumber)QuestionID=1Choice=GoodChoiceID=2(autonumber)QuestionID=1Choice=BadChoiceID=3(autonumber)QuestionID=2Choice=ExcellentChoiceID=4(autonumber)QuestionID=2Choice=PoorWhen the user wants to select all the questions form the older survey i am able to create a new survey get the new surveyid and insert all the questions from the older survey with the new surveyid which i created. (i will be passing the old surveyid).DECLARE @NewID INTinsert into survey(title,description, surveystatus,CreatedBy,date )values('Yahoo title','Yahoo title',1,'test',Getdate())SELECT @NewID = SCOPE_IDENTITY()INSERT SurveyQuestions(surveyid, questions,answertype)SELECT @NewID ,questions,answertypeFROM SurveyQuestionsWHERE surveyid='1'The above query result looks like thisSurveysurveyid= 1 (Auto number)title= test titledescription= test titlesurveystatus=1CreatedBy=testdate= today's date The new One Createdsurveyid= 2 (Auto number)title= Yahoo titledescription= Yahoo titlesurveystatus=1CreatedBy=testdate= today's dateSurveyQuestionsQuestionsid= 1 (autonumber)SurveyID=1Questions="Good Work"AnswerType= MQuestionsid= 2 (autonumber)SurveyID=1Questions="bad Work"AnswerType= SThe new One CreatedQuestionsid= 3 (autonumber)SurveyID=2Questions="Good Work"AnswerType= MQuestionsid= 4 (autonumber)SurveyID=2Questions="bad WorkAnswerType= SThis thing i am not able to do it copy the data in the surveychoices table with the new questionID of the surveyquestions. I want to copy the data of questionID 1(Good, Bad), questionID2 (Excellent and Poor) with the new quetsionid(3,4) which i created in the surveySquestions.The Data should look like thisSurveyChoicesChoiceID=1(autonumber)QuestionID=1Choice=GoodChoiceID=2(autonumber)QuestionID=1Choice=BadChoiceID=3(autonumber)QuestionID=2Choice=ExcellentChoiceID=4(autonumber)QuestionID=2Choice=PoorChoiceID=5(autonumber)QuestionID=3Choice=GoodChoiceID=6(autonumber)QuestionID=3Choice=BadChoiceID=7(autonumber)QuestionID=4Choice=ExcellentChoiceID=8(autonumber)QuestionID=4Choice=PoorThe table structure looks like this.SurveyCREATE TABLE [dbo].[Survey]( [SurveyId] [int] IDENTITY(1,1) NOT NULL, [Title] [varchar](max) NULL, [Description] [varchar](max) NULL, [SurveyStatus] [varchar](50) NULL, [CreatedBy] [varchar](250) NULL, [Date] [datetime] NULL, [SurveyCompleted] [datetime] NULL, CONSTRAINT [PK__Survey__0425A276] PRIMARY KEY CLUSTERED ( [SurveyId] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]SurveyQuestionsREATE TABLE [dbo].[SurveyQuestions]( [QuestionId] [int] IDENTITY(1,1) NOT NULL, [SurveyId] [int] NULL, [Questions] [varchar](max) NULL, [AnswerType] [varchar](max) NULL, CONSTRAINT [PK__SurveyQuestions__023D5A04] PRIMARY KEY CLUSTERED ( [QuestionId] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]SurveyChoicesCREATE TABLE [dbo].[SurveyChoices]( [ChoiceId] [int] IDENTITY(1,1) NOT NULL, [QuestionId] [int] NULL, [choice] [varchar](max) NULL, CONSTRAINT [PK__SurveyChoices__7E6CC920] PRIMARY KEY CLUSTERED ( [ChoiceId] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] |
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
|
soorma
Yak Posting Veteran
52 Posts |
Posted - 2010-08-02 : 15:23:34
|
this is how i am inserting for step 1 and 2. I am not using title because there can be two titles with the same name. The user clicks on the gridview that he wants to copy all the questions and options from the this survey(test survey). So i am passing the surevyid of the test survey to copy all the questions of test survey to the new survey. This is how i am doing below. Creating a new survey and copying all the questions with the new surevyIDDECLARE @NewID INTinsert into survey(title,description, surveystatus,CreatedBy,date )values('Yahoo title','Yahoo title',1,'test',Getdate())SELECT @NewID = SCOPE_IDENTITY()INSERT SurveyQuestions(surveyid, questions,answertype)SELECT @NewID ,questions,answertypeFROM SurveyQuestionsWHERE surveyid='1'(TestSurvey ID) |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-08-02 : 16:50:23
|
quote: Originally posted by soormaI am not using title because there can be two titles with the same name.
So then how do you know which Survey is which?The IDENTITY Column is uselessIn any case...is the Survey info all in a single file?I wonder if you can use OUTPUT to collect all the inserted data Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
soorma
Yak Posting Veteran
52 Posts |
Posted - 2010-08-02 : 17:06:28
|
This is how i know which survey is which. I use the createdby to know what survey which person has created.SELECT [SurveyId], [Title], description FROM [Survey] WHERE (([SurveyStatus] = @SurveyStatus) AND ([CreatedBy] = @CreatedBy))This will give me all the survey created by that person. Then i pass the surveyID on the grid.This will give me all the questions for the surveySELECT SurveyQuestions.QuestionId, SurveyQuestions.SurveyId, SurveyQuestions.Questions, SurveyQuestions.AnswerType, Survey.Title FROM SurveyQuestions INNER JOIN Survey ON SurveyQuestions.SurveyId = Survey.SurveyId WHERE (SurveyQuestions.SurveyId = @SurveyId)This will give me all the options related to the questionsSELECT * FROM [SurveyQuestions] WHERE ([QuestionId] = @QuestionId) |
 |
|
X002548
Not Just a Number
15586 Posts |
|
soorma
Yak Posting Veteran
52 Posts |
Posted - 2010-08-03 : 11:06:08
|
I get ur point. But i have designed that way and programmed in asp.net. But my questions is how can i copy the data in the surveychoices table with the new questionID of the surveyquestions. I want to copy the data of questionID 1(Good, Bad), questionID2 (Excellent and Poor) with the new quetsionid(3,4) which i created in the surveySquestions.As i told u i am able to copy the questions with the new surevyid not there options with the new questionid of the new surevyid.Thanks for your help8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam[/quote] |
 |
|
|
|
|
|
|