简体   繁体   中英

How to split only one row value and insert into multiple columns using stored procedure, parameter value is from C#

ALTER PROCEDURE [dbo].[ProcTeamInfoById]
   (@id INT, 
    @txtName VARCHAR(50), 
    @phone VARCHAR(100), 
    @contactname VARCHAR(100),                    
    @address VARCHAR(300), 
    @email VARCHAR(100),  
    @captain VARCHAR(100), 
    @requirements VARCHAR(50))
AS                    
BEGIN
    IF @id = -1                    
    BEGIN
        DECLARE @teamID VARCHAR(MAX);

        INSERT INTO TeamDetails(TeamName, Phone, ContactName, Address, Email, captain)               
        VALUES (@txtName, @phone, @contactname, @address, @email, @captain)    

        SET @teamID = SCOPE_IDENTITY();    

        // Here now I need to add to different table...

        // checkbox checked values 

        INSERT INTO teamrequirements (teamid, requirement, value) 
        VALUES (@teamID, @requirements, 1)

        // Here I am getting 1, bats, Gloves, 1
    END
    ELSE
       UPDATE TeamDetails 
       SET TeamName = @txtName, Phone = @phone, 
           ContactName = @contactname,                     
           Address = @address, Email = @email, 
       WHERE TeamId = @id  
    END

I need like this how to modify stored procedure

1, bats,gloves,1 

I need to insert as

1 bats 1 
2 gloves1 

If I need to update the above it should be like

1 bats  1 

If I updating with no values it should be like

1  '' '' 

My stored procedure inserts into two tables. While adding into teamrequirements table, I need to split one column value into multiple columns where I commented. I need to modify the stored procedure to insert into multiple columns and records in the teamrequirements table.

How to do it?

While adding a team is added into the teamdetails table, and I have checkbox like Requirements if I check that I can select another two checkboxes, when both checkboxes are checked, two records to be inserted in teamrequirements, example , i wil have teamid, requirement, value...example 1,bats,Gloves,1 this should insert as 2 records Like

12,1 bat , 1 // 12 is record id in teamrequirements
13,1,Gloves , 1 // 13 is record id in teamrequirements

Please help...

What if it is Update in the same procedure .. update teamrequirements set requirement= @reuirement , value = 1 where teamid = @id in this case , if i add those two records bats gloves in database i am adding records , what if i need to clear those added records using update statement in the same procedure

You can use any of the split function over the internet or create your own and use it. Below code has been written according to split function in FnSplitString .

You created a split function in your database. This function will return 1 or more values from your string provided. This is your @requirements variable.

IF(LEN(@requirements) > 0)
BEGIN

    INSERT INTO teamrequirements (teamid, requirement, value) 
    SELECT @teamID, splitdata, '1' FROM dbo.[fnSplitString](@requirements,',')
END
ELSE
BEGIN 
   INSERT INTO teamrequirements (teamid, requirement, value) 
   VALUES (@teamID, '', '') 
END

SQL 2016 :

JSON support is there now. So, you can pass string as json and take 2 values into key value table, and insert it directly. No need to split.

FOR UPDATE: You can take this split string data in a table variable. Update data from table variable as source into main table, with a JOIN condition.

DECLARE @id INT = 40

DECLARE @data TABLE
(
Id INT,
Requirement VARCHAR(50),
Value INT
)

DECLARE @requirements VARCHAR(50) = 'bat,gloves,ball'
INSERT INTO @data
SELECT @id, splitdata, 1 FROM dbo.[fnSplitString](@requirements,',')

SELECT * FROM @data

-- SOMETHING LIKE THIS CAN BE YOUR UPDATE QUERY
UPDATE a SET 
value = b.Value
FROM teamrequirements a INNER JOIN @data b ON a.id = b.Id 
and a.requirement = b.Requirement 

MERGE SAMPLE:

MERGE teamrequirements AS TARGET
USING @data AS SOURCE 
ON TARGET.id = SOURCE.Id AND TARGET.requirement = SOURCE.Requirement
--When records are matched, update 
--the records if there is any change
WHEN MATCHED THEN 
UPDATE SET TARGET.value = SOURCE.Value
--When no records are matched, insert
--the incoming records from source
--table to target table
WHEN NOT MATCHED BY TARGET THEN 
INSERT (teamid, requirement, value) 
VALUES (SOURCE.Id, SOURCE.Requirement, SOURCE.Value);

First, it would be better to use table valued parameters for this.

Without that, you can split the string (this demo uses delimitedsplit8k by Jeff Moden) , and you could use merge to handle changes. It makes sense to move this into its own procedure and call that procedure from your current one.

TL;DR: rextester demo: http://rextester.com/PVX88970

The TeamRequirements_Merge procedure:

create procedure dbo.TeamRequirements_Merge (
    @TeamId int
  , @requirements varchar(8000)
) as
begin;
  set nocount, xact_abort on;
  /* it would be much better to use table valued parameters for this */
  ;with t as (
    select TeamId, Requirement, Value
    from TeamRequirements
    where TeamId = @TeamId
  )
  , s as (
    select
        TeamId = @TeamId
      , Requirement = coalesce(s.item,'') -- since you want blanks
      , Value = 1
    from dbo.[delimitedsplit8K](@requirements,',') s
  )
  merge into t with (holdlock)
  using s
    on t.TeamId = s.TeamId
   and t.Requirement = s.Requirement
  when matched and t.value <> s.value
    then update set t.value = s.value
  when not matched by target
    then insert (TeamId, Requirement, Value)
      values (s.TeamId, s.Requirement, s.Value)
  when not matched by source
    then delete
  --output $Action, inserted.*, deleted.* /* for testing */
  ;
end;
go

The revised ProcTeamInfoById procedure:

create procedure [dbo].[ProcTeamInfoById] (
    @id int
  , @txtName varchar(50)
  , @phone varchar(100)
  , @contactname varchar(100)
  , @address varchar(300)
  , @email varchar(100)
  , @captain varchar(100)
  , @requirements varchar(8000)
  ) as
begin;
  if @id = -1
    begin;
      insert into TeamDetails(TeamName, Phone, ContactName, Address, Email, captain)
      values (@txtName, @phone, @contactname, @address, @email, @captain);

      set @id = scope_identity();
    end;
  else
    begin;
       update TeamDetails
       set TeamName = @txtName, Phone = @phone,
           ContactName = @contactname,
           Address = @address, Email = @email
       where TeamId = @id;
    end;
  exec dbo.TeamRequirements_Merge @id, @requirements;
end;
go

splitting strings reference:

merge reference:


Again, it would be better to use table valued parameters for this:

Table Valued Parameters reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM