CREATE PROCEDURE Usp_load_survey_data
AS
BEGIN
SET DATEFORMAT DMY;
CREATE TABLE #temp_rowdata
(
sno BIGINT,
item VARCHAR(256) NULL,
colsequence INT
);
INSERT INTO #temp_rowdata
(sno,
item,
colsequence)
SELECT LEFT(t.rowdata, Charindex(',', t.rowdata) - 1)
AS
sno,
s.item,
Row_number()
OVER (
partition BY LEFT(t.rowdata, Charindex(',', t.rowdata) - 1)
ORDER BY LEFT(t.rowdata, Charindex(',', t.rowdata) - 1) ASC )
AS
ColSequence
FROM stg_rowdata
AS t
CROSS apply dbo.Delimitedsplit8k(t.rowdata, ',') AS s;
INSERT INTO stg_survey
(sno,
voter_name,
voter_add,
constituency_name,
constituency_state,
pre_poll_date,
question_1,
answer_1,
question_2,
answer_2,
question_3,
answer_3,
question_4,
answer_4,
question_5,
answer_5)
SELECT RowData.item AS sno,
RowData2.item AS Voter_Name,
RowData3.item AS Voter_Add,
RowData4.item AS Constituency_Name,
RowData5.item AS Constituency_State,
RowData6.item AS Pre_Poll_date,
Question_1.item AS Question_1,
Answer_1.item AS Answer_1,
Question_2.item AS Question_2,
Answer_2.item AS Answer_2,
Question_3.item AS Question_3,
Answer_3.item AS Answer_3,
Question_4.item AS Question_4,
Answer_4.item AS Answer_4,
Question_5.item AS Question_5,
Answer_5.item AS Answer_5
FROM
#temp_rowdata AS RowData
INNER JOIN (SELECT sno,
item,
colsequence
FROM
#temp_rowdata) AS RowData2
ON RowData2.sno = RowData.sno
AND RowData2.colsequence = 2
INNER JOIN (SELECT sno,
item,
colsequence
FROM
#temp_rowdata) AS RowData3
ON RowData3.sno = RowData.sno
AND RowData3.colsequence = 3
INNER JOIN (SELECT sno,
item,
colsequence
FROM
#temp_rowdata) AS RowData4
ON RowData4.sno = RowData.sno
AND RowData4.colsequence = 4
INNER JOIN (SELECT sno,
item,
colsequence
FROM
#temp_rowdata) AS RowData5
ON RowData5.sno = RowData.sno
AND RowData5.colsequence = 5
INNER JOIN (SELECT sno,
item,
colsequence
FROM
#temp_rowdata) AS RowData6
ON RowData6.sno = RowData.sno
AND RowData6.colsequence = 6
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 7
AND sno = RowData.sno) AS Question_1
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 8
AND sno = RowData.sno) AS Answer_1
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 9
AND sno = RowData.sno) AS Question_2
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 10
AND sno = RowData.sno) AS Answer_2
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 11
AND sno = RowData.sno) AS Question_3
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 12
AND sno = RowData.sno) AS Answer_3
OUTER apply (SELECT sno,
item,
colsequence
FROM #temp_rowdata
WHERE colsequence = 13
AND sno = RowData.sno) AS Question_4
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 14
AND sno = RowData.sno) AS Answer_4
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 15
AND sno = RowData.sno) AS Question_5
OUTER apply (SELECT sno,
item,
colsequence
FROM
#temp_rowdata
WHERE colsequence = 16
AND sno = RowData.sno) AS Answer_5
WHERE RowData.colsequence = 1;
--update the existing records in the main table.
UPDATE sur
SET Voter_Name
= stg.voter_name,
Voter_Add = stg.voter_add,
Constituency_Name = stg.constituency_name,
Constituency_State = stg.constituency_state,
Pre_Poll_date
= stg.pre_poll_date,
Question_1 = stg.question_1,
Answer_1 = stg.answer_1,
Question_2 = stg.question_2,
Answer_2 = stg.answer_2,
Question_3 = stg.question_3,
Answer_3 = stg.answer_3,
Question_4 = stg.question_4,
Answer_4 = stg.answer_4,
Question_5 = stg.question_5,
Answer_5 = stg.answer_5
FROM survey sur
JOIN stg_survey stg
ON stg.sno = sur.sno
WHERE Isnull(sur.voter_name, '') <> Isnull(stg.voter_name, '')
OR Isnull(sur.voter_add, '') <> Isnull(stg.voter_add, '')
OR Isnull(sur.constituency_name, '') <>
Isnull(stg.constituency_name, '')
OR Isnull(sur.constituency_state, '') <>
Isnull(stg.constituency_state, '')
OR Isnull(sur.pre_poll_date, '') <> Isnull(stg.pre_poll_date, '')
OR Isnull(sur.question_1, '') <> Isnull(stg.question_1, '')
OR Isnull(sur.answer_1, '') <> Isnull(stg.answer_1, '')
OR Isnull(sur.question_2, '') <> Isnull(stg.question_2, '')
OR Isnull(sur.answer_2, '') <> Isnull(stg.answer_2, '')
OR Isnull(sur.question_3, '') <> Isnull(stg.question_3, '')
OR Isnull(sur.answer_3, '') <> Isnull(stg.answer_3, '')
OR Isnull(sur.question_4, '') <> Isnull(stg.question_4, '')
OR Isnull(sur.answer_4, '') <> Isnull(stg.answer_4, '')
OR Isnull(sur.question_5, '') <> Isnull(stg.question_5, '')
OR Isnull(sur.answer_5, '') <> Isnull(stg.answer_5, '');
--inserting the records
INSERT INTO survey
(sno,
voter_name,
voter_add,
constituency_name,
constituency_state,
pre_poll_date,
question_1,
answer_1,
question_2,
answer_2,
question_3,
answer_3,
question_4,
answer_4,
question_5,
answer_5)
SELECT stg.sno,
stg.voter_name,
stg.voter_add,
stg.constituency_name,
stg.constituency_state,
stg.pre_poll_date,
stg.question_1,
stg.answer_1,
stg.question_2,
stg.answer_2,
stg.question_3,
stg.answer_3,
stg.question_4,
stg.answer_4,
stg.question_5,
stg.answer_5
FROM stg_survey
stg
LEFT JOIN survey sur
ON sur.sno = stg.sno
WHERE sur.sno IS NULL;
DROP TABLE #temp_rowdata;
END;
go
|