简体   繁体   中英

How to update a table with list of values in T-SQL?

I have a table that I want to update one-to-many relationship records. The table def is:

CREATE TABLE CT_ProductFailures 
( 
PFID int identity(1,1) PRIMARY KEY NOT NULL, 
Old_Modes varchar(75), 
New_Modes varchar(75) 
) 

Now I want to add records for New_Mode 'N1' with Old_Modes 'A', 'B', 'C'. Other than by doing separate insert statements, how can I achieve this?

If you are using SQL Server 2008 you can do this rather easily using syntax like below

INSERT INTO CT_ProductFaiures (Old_Modes, New_Modes)
VALUES ('N1', 'A'), ('N1', 'B'), ('N1', 'C');

This assumes PFID is auto incrementing.

You can use UNIONS do sort of achieve this in older versions. Check out the link below for great examples.

http://blog.sqlauthority.com/2008/07/02/sql-server-2008-insert-multiple-records-using-one-insert-statement-use-of-row-constructor/

INSERT INTO CT_ProductFailures (New_Modes, Old_Modes)
SELECT n.New_Modes, o.Old_Modes
FROM (SELECT 'N1' New_Modes) n
CROSS JOIN (SELECT 'A' Old_Modes
    UNION ALL SELECT 'B'
    UNION ALL SELECT 'C') o

Alternatively, write a split function using newline as the separator:

INSERT INTO CT_ProductFailures (New_Modes, Old_Modes)
SELECT 'N1' New_Modes, o.value Old_Modes
FROM dbo.split(
'A
B
C') o

Ryan,

You are saying: 'Instead I want to copy this list so that all I have to do is add single quotes around each record'.
It is not clear how your list is formated.

Let's assume you have the list like this (comma separated values)

N1, A
N1, B
N1, C

What you can do is:

  1. Copy/paste the list to SQL Server Management Studio
  2. Run some replacements using regular expressions:

    • hit Ctrl+H to open 'Find and Replace' dialog
    • in Find Options select Use 'Regular expressions'
    • Find what: [^] Replace with: [('] Hit 'Replace All'
      Don't type square parenthesis in. I used them to make sure you could see the spaces!
    • Find what: [$] Replace with: ['),] Hit 'Replace All'
    • Find what: [, ] Replace with: [', '] Hit 'Replace All'
    • Remove the last redundant comma

    You will end up with the list like this:

     ('N1', 'A'), ('N1', 'B'), ('N1', 'C')
  3. Then add insert statement at the top of the script:

     insert into CT_ProductFaiures (Old_Modes, New_Modes) values

So in the result you have this

insert into CT_ProductFaiures (Old_Modes, New_Modes)
values 
('N1', 'A'),
('N1', 'B'),
('N1', 'C')

Hope that this is what you needed.

[Edited]
I'm sorry, didn't pay attention.
So list is formatted like this: A, B, C, D.
Then you just replace [,] with ['), ('N1', '] without any regular expressions.
Also, fix starting and ending value manually.

Create a table called NewTable with the AB C records with another New_Modes field.

Use this table in your insert command.

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