User987
User987

Reputation: 3823

MSSQL update multiple records all at once by one stored procedure

I have a stored procedure that currently looks like this:

create procedure zsp_updateTyp5Graph
(
    @DayList nvarchar(1000),
    @Sales int,
    @SearchedUserId int
)
as 
update SearchedUserGraphData
    set SalesForDay=@Sales
where Day in (SELECT * FROM dbo.SplitString(@DayList)) and SearchedUserId=@SearchedUserId

The daylist parameter looks like following:

0,1,2,3,4,5,6

I have in my SearchedUserGraphData table two columns and 7 records(7 days) that should be updated. For example:

   Day    Sales
    0       5
    1       6
    2       4
    3       3
    4       7
    5       9 
    6      11

I have "partially" solved this by passing list of days... But I'm unable to find out how can I pair up this @DayList parameter with sales data...

I have a SplitString function that I've created and looks like this for matching the records in DB:

ALTER FUNCTION [dbo].[splitstring] ( @stringToSplit VARCHAR(MAX) )
RETURNS
    @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN

    DECLARE @name NVARCHAR(255)
    DECLARE @pos INT

    WHILE CHARINDEX(',', @stringToSplit) > 0
    BEGIN
        SELECT @pos  = CHARINDEX(',', @stringToSplit)  
        SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

        INSERT INTO @returnList 
        SELECT @name

        SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
    END

    INSERT INTO @returnList
    SELECT @stringToSplit

    RETURN
END

Now the issue is that I need to somehow pass from my C# application combined list of Days and their corresponding sales, and then update the records all at once, without calling the procedure 7 times to update 7 records (that would be catastrophical from performance standpoint in my eyes)...

Can someone help me out with this?

P.S. Guys, the "best" idea that I get on this one is:

The daylist parameter looks like following:

    0,1,2,3,4,5,6

And then sales:

    11,22,44,55,66,77,88

This is the way I can pass the sales and days parameters... But then what? I cannot know what sale corresponds to which day...

I could form a key-value pair dictionary string in my C# application maybe like:

{ Day: 1 Sales: 44, Day: 2 Sales: 55 } 

this is how I could form the string.. But then I need to break down this string in MSSQL to match => day 1 => update with sales 44 ...

Upvotes: 0

Views: 200

Answers (1)

hardkoded
hardkoded

Reputation: 21695

You can create a SalesPerDay type

CREATE TYPE [dbo].[SalesPerDay] AS TABLE (
    day INT NOT NULL,
    sales INT NOT NULL
);

Then, your proc would look like this

create procedure zsp_updateTyp5Graph
(
@DayList SalesPerDay READONLY,
@SearchedUserId int
)
as 
update SearchedUserGraphData
set SalesForDay=dayList.sales
FROM SearchedUserGraphData baseTable
   INNER JOIN @DayList dayList ON dayList.day = baseTable.day
WHERE SearchedUserId=@SearchedUserId

And from the C#, you can build a DataTable and send it to the proc.

var table = new DataTable();
table.Columns.Add("day", typeof(int));
table.Columns.Add("sales", typeof(int));

//Add data
table.Rows.Add(new []{1, 200});
table.Rows.Add(new []{2, 200});

//More code
Command.Parameters.AddWithValue("@DayList", table);

Upvotes: 2

Related Questions