Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to write insert query for this two table with only one parameter? Expand / Collapse
Author
Message
Posted Tuesday, September 25, 2012 11:42 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
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



Post #1364441
Posted Wednesday, September 26, 2012 12:41 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 20,739, Visits: 32,524
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.



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)
Post #1364456
Posted Wednesday, September 26, 2012 3:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 27, 2014 6:05 AM
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


Post #1364520
Posted Wednesday, September 26, 2012 7:16 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 20,739, Visits: 32,524
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.



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)
Post #1364651
Posted Wednesday, September 26, 2012 10:49 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
thanks
Lynn Pettis,
ard5karthick

for sharing your knowledges and idea
Post #1364835
Posted Wednesday, September 26, 2012 10:54 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
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

Post #1364841
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse