Hi All,
Though i work in the oracle platform but recently got one project where we need to write a SP in the sql server, the requirement is like as below:
SP should accept 2 parameters one is event id which is having number datatype and 2nd parameter is a combination of few of the columns such as question no./name/description,ques_dtl,ques_ans which will be comma separated.
The 2nd parameter can contain multiple values which will be separated by #, i have to read these values and get them inserted into some tables.
could you please help me to write some generic SP which can fulfill my above mentioned requirement, I shall be really much thankful for the same.
This design is almost baked and database architects have finalized this approach as well, something if you can give me like inserting the 1 record as below:
1,QuestT,QuestText,QuestDtl,QuestANS
how i will identify the columns here based on comma separation and this record needs to be coming in a single input parameter.
This has nothing to do with architecturing databases. How about sending this as json. The approache recommended by "database architects" is not well thought out
Create proc questions_sp
As
(
@parms varchar(max)
)
Begin
Create table #questions(
eventId int,
QuestT nvarchar(150),
QuestText nvarchar(150),
QuestDtl nvarchar(150),
QuestANS nvarchar(150)
)
Insert into #questions
Select *
From openjson(@questions)
With (
EventId int,
QuestT nvarchar(150),
QuestText nvarchar(150),
QuestDtl nvarchar(150),
QuestANS nvarchsr(150)
)
--then you can do what you need
--with this temp table to manage to destination table
End
Go
yes correct but how declaring table type variable will read the multiple records passed in the input and distinguish the value of the respective column coming as comma separated.
for sure i will go through it but how the data will be inserted into this as the user will only insert data via the stored procedure only?
and when the data will be like as below:
here is one ugly way, that will come to bite you one day
NOT RECOMMENDED
declare @questions nvarchar(max) =
'1,35min,How old are you?,age question,21#2,12min,what is your income level,income,35k#5,1min,where do you live?,location,Kabul'
create table #questions(
eventid int,
QuestT nvarchar(150),
QuestText nvarchar(150),
QuestDtl nvarchar(150),
QuestANS nvarchar(150)
)
;WITH pounds
AS
(
select value
from string_split(@questions, '#') c
)
insert into #questions
select [1] eventId,
[2] as QuestT,
[3] as QuestText,
[4] QuestDtl,
[5] as QuestANS
from (
select p.value as ID, c.value, ROW_NUMBER() OVER(PARTITION BY p.value ORDER BY (SELECT NULL)) as rn
FROM pounds p
cross apply string_split(p.value, ',') c
) x
PIVOT(
MAX(value) FOR rn IN([1],[2],[3],[4],[5])
) as PVT
select * From #questions
drop table #questions
Even table type permission i am not going to have, below is what i tried but failed
CREATE TYPE RsvpTableType
AS TABLE
( QSTN_NBR INT
, QSTN_DESC VARCHAR(255)
, ACTV_QSTN_IND BIT
, MNDTR_QSTN_IND BIT
, ANS_NBR BIT
, ANS_DESC VARCHAR(100)
, ACTV_ANS_IND BIT);
GO
CREATE PROCEDURE dbo.upsert_evnt_rsvp_dtl1 @Rs RsvpTableType READONLY
AS
SET NOCOUNT ON
INSERT INTO dbo.EVNT_RSVP
(
evnt_id
, QSTN_NBR
, ACTV_QSTN_IND
, CRTE_USR_ID
, CRTE_TSTP
)
SELECT QSTN_NBR, QSTN_NBR,ACTV_QSTN_IND,'Test', GETDATE()
FROM @Rs;
GO
Msg 262, Level 14, State 1, Line 20
CREATE TYPE permission denied in database 'SMR_EVT'.