Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to write insert query for this two table with only one parameter?


how to write insert query for this two table with only one parameter?

Author
Message
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1399
here iam having a parameter @questionIdandchoiceid varchar(8000)='1-2,2-3,3-5' from this parameter i just want o insert into two table

DECLARE @questionIdandchoiceid varchar(8000)='1-2,2-3,3-5'

DECLARE @question table
(
alid uniqueidentifier,
QuestionId int,
crdate datetime,
IsActive bit
)




DECLARE @Choice table
(
imid int,
alid uniqueidentifier,
Choiceid int
)


in that parameter 1-2,2-3

the first one will be question id and the second one will be choice id for the question

and in the @choice table we want to insert alid as a foregin key also

and all the other column will be null (crdate ,IsActive )
i am trying to insert like this

alid QuestionId crdate IsActive
BB83F8E9 1 null null
9EB1E08563 2 null null
SB83F8ERE 3 null null

imid alid Choiceid
1 BB83F8E9 2
2 9EB1E08563 3
3 SB83F8ERE 5



Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37929
Personally, I'd like to know what you have done so far, but here is a solution anyway. I know you have been give some of the code used already, if not you can find here on ssc.



DECLARE @questionIdandchoiceid varchar(8000)='1-2,2-3,3-5' ;

DECLARE @Qout table(alid uniqueidentifier, QuestionID int);

DECLARE @question table
(
alid uniqueidentifier default newid(),
QuestionID int,
crdate datetime null,
IsActive bit null
);

DECLARE @Choice table
(
imid int identity(1,1),
alid uniqueidentifier,
ChoiceID int
);

with basedata as (
select
max(case ds2.ItemNumber when 1 then cast(ds2.Item as int) else null end) as QuestionID,
max(case ds2.ItemNumber when 2 then cast(ds2.Item as int) else null end) as ChoiceID
from
dbo.DelimitedSplit8K(@questionIdandchoiceid,',') ds1
cross apply dbo.DelimitedSplit8K(ds1.Item,'-') ds2
group by
ds1.ItemNumber,
ds1.Item
)
insert into
@Question(QuestionID)
output
inserted.alid, inserted.QuestionID
into
@Qout
select
bd.QuestionID
from
basedata bd;

with basedata as (
select
max(case ds2.ItemNumber when 1 then ds2.Item else null end) as QuestionID,
max(case ds2.ItemNumber when 2 then ds2.Item else null end) as ChoiceID
from
dbo.DelimitedSplit8K(@questionIdandchoiceid,',') ds1
cross apply dbo.DelimitedSplit8K(ds1.Item,'-') ds2
group by
ds1.ItemNumber,
ds1.Item
)
insert into
@Choice(alid, ChoiceID)
select
q.alid,
bd.ChoiceID
from
basedata bd
inner join @Qout q
on q.QuestionID = bd.QuestionID;

select * from @Question;
select *from @Choice;





Any questions, check Books Online. If still confused, post back.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ard5karthick
ard5karthick
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 162
Hi Sivaji,

The below procedure must solve your problem...

Use SSISTest
GO
-- Exec SplitStringInsert '1-2,2-3,3-5,4-4,14-14,10-9684'
Alter Procedure SplitStringInsert (@String Varchar(8000))
AS
Begin

Declare @BeginingPosition int;
Declare @TrunPosition int;
Declare @Length int

--Set @String = '1-2,2-3,3-5,4-4,14-14,10-9684'
Set @BeginingPosition = 0
Set @Length = LEN(@String)

Create Table ##SetString(QuestionAnswer Varchar(8000))

DECLARE @question table
(
alid uniqueidentifier,
QuestionId int,
crdate datetime,
IsActive bit
)


DECLARE @Choice table
(
imid int,
alid uniqueidentifier,
Choiceid int
)


While (CHARINDEX(',',@String,0) <> 0)
Begin

Set @TrunPosition = CHARINDEX(',',@String,@BeginingPosition)
--Select @TrunPosition

Insert into ##SetString (QuestionAnswer)
Select SUBSTRING(@String,@BeginingPosition,@TrunPosition)

Set @String = SUBSTRING(@String,@TrunPosition+1,@Length)
--Select @String

Set @Length = LEN(@String)

END

Insert into ##SetString (QuestionAnswer)
Select @String


SELECT NEWID() ID, SUBSTRING(QuestionAnswer,1,CHARINDEX('-',QuestionAnswer,0)-1) Question
into ##Question
FROM ##SetString

Select SUBSTRING(QuestionAnswer,1,CHARINDEX('-',QuestionAnswer,0)-1) Ques,
SUBSTRING(QuestionAnswer,CHARINDEX('-',QuestionAnswer,0)+1,@Length) Answer
into ##Answer
From ##SetString

Insert INTO @question (alid ,
QuestionId ,
crdate ,
IsActive )
SELECT ID,Question, NULL,NULL
FROM ##Question

Insert INTO @Choice
(imid ,
alid ,
Choiceid )
Select Question, ID,Answer
From ##Question join ##Answer on Ques = Question

Select * From @Choice

Select * From @question


--Drop Table ##SetString
--Drop table ##Question

--Drop table ##Answer
End
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37929
ard5karthick (9/26/2012)
Hi Sivaji,

The below procedure must solve your problem...

Use SSISTest
GO
-- Exec SplitStringInsert '1-2,2-3,3-5,4-4,14-14,10-9684'
Alter Procedure SplitStringInsert (@String Varchar(8000))
AS
Begin

Declare @BeginingPosition int;
Declare @TrunPosition int;
Declare @Length int

--Set @String = '1-2,2-3,3-5,4-4,14-14,10-9684'
Set @BeginingPosition = 0
Set @Length = LEN(@String)

Create Table ##SetString(QuestionAnswer Varchar(8000))

DECLARE @question table
(
alid uniqueidentifier,
QuestionId int,
crdate datetime,
IsActive bit
)


DECLARE @Choice table
(
imid int,
alid uniqueidentifier,
Choiceid int
)


While (CHARINDEX(',',@String,0) <> 0)
Begin

Set @TrunPosition = CHARINDEX(',',@String,@BeginingPosition)
--Select @TrunPosition

Insert into ##SetString (QuestionAnswer)
Select SUBSTRING(@String,@BeginingPosition,@TrunPosition)

Set @String = SUBSTRING(@String,@TrunPosition+1,@Length)
--Select @String

Set @Length = LEN(@String)

END

Insert into ##SetString (QuestionAnswer)
Select @String


SELECT NEWID() ID, SUBSTRING(QuestionAnswer,1,CHARINDEX('-',QuestionAnswer,0)-1) Question
into ##Question
FROM ##SetString

Select SUBSTRING(QuestionAnswer,1,CHARINDEX('-',QuestionAnswer,0)-1) Ques,
SUBSTRING(QuestionAnswer,CHARINDEX('-',QuestionAnswer,0)+1,@Length) Answer
into ##Answer
From ##SetString

Insert INTO @question (alid ,
QuestionId ,
crdate ,
IsActive )
SELECT ID,Question, NULL,NULL
FROM ##Question

Insert INTO @Choice
(imid ,
alid ,
Choiceid )
Select Question, ID,Answer
From ##Question join ##Answer on Ques = Question

Select * From @Choice

Select * From @question


--Drop Table ##SetString
--Drop table ##Question

--Drop table ##Answer
End





I will put my solution up againsts this one any day. Guess tonight I work on a 1,000,000 row test to test them both.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1399
thanks
Lynn Pettis,
ard5karthick

for sharing your knowledges and idea
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1399
even my friend gave me a idea to do like this using a split function or DelimitedSplit8K function also


DECLARE @questionIdandchoiceid varchar(8000)='1-2,2-3,3-5'

DECLARE @question table(
alid uniqueidentifier,
QuestionId int,
crdate datetime,
IsActive bit
)

DECLARE @Choice table(
imid int,
alid uniqueidentifier,
Choiceid int
)

SELECT LEFT(val, CHARINDEX('-', val)-1) AS Ques
, STUFF(val, 1, CHARINDEX('-', val), '') AS Choice
INTO #temp
FROM dbo.split(@questionIdandchoiceid, ',')

INSERT INTO @question(alid, QuestionId)
SELECT NEWID(), Ques FROM #temp

INSERT INTO @Choice
SELECT QuestionId, alid, t.Choice
FROM @question q
inner join #temp t on q.QuestionId = t.Ques

SELECT * FROM @question
SELECT * FROM @Choice

DROP TABLE #temp


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search