Search code examples
c#sqlsql-updateparameterized-querytable-valued-parameters

Update query (Parameterized query,Table valued parameters)


I have a

SQL TABLE Inventory Having many columns two of which are LocalSKU (pk) varchar(200) NOT NULL QOH int

And an EXCEL DATA having only two columns LocalSKU and QOH

I want to implement a query where I want to match both data according to LocalSKU and if they match , The query should update QOH. If they Do not match , Do Nothing.

I've wriiten this query

                          SqlCommand sqlcmd = new SqlCommand(@"MERGE Inventory AS target
                          USING (select LocalSKU, QOH from @source)  as source
                          ON (source.LocalSKU = target.LocalSKU)
                          WHEN MATCHED THEN
                          UPDATE SET QOH = source.QOH
                                      WHEN NOT MATCHED THEN
                          INSERT (QOH)
                          VALUES (source.QOH);", sqlconn);

It does not work. Please guide me how to implement this.

Examples will be appreciated.


Solution

  •                       SqlCommand sqlcmd = new SqlCommand(@"MERGE Inventory AS target
                              USING (select LocalSKU,QOH, Integer3 from @source)  as source
                              ON (source.LocalSKU = target.LocalSKU)
                             WHEN MATCHED THEN
                              UPDATE SET Integer3 = source.QOH;", sqlconn);