How to edit MULTIPLE rows/data entries in SQL Server Management Studio [closed]

时间秒杀一切 提交于 2019-12-24 04:01:21

问题


I want to update/edit multiple rows in a database using SQL Server Management Studio. I know you can edit one entry using solutions provided here, here or here using Edit Top(200) function. But I want to edit >100 entries so this is not an adequate solution. I am using SSMS 2012.

EDIT:

As an simplified example we can look at the following situation: I have a database of peoples names and their height. But many of the heights are wrong or not updated. So I want to update their heights. This update includes a lot of people and it is tedious to update one by one using the solution I linked too. I have to updated values stored in an Excel file.

I am not sure of how to attack this problem, but is seeking a way that I can copy paste the updated values directly into the tables or do this indirectly using some sql statement.


回答1:


If you select Edit Top 200 Rows first, you can then modify your query to return 250 rows.

In order to do this you need to click on Show SQL Pane button, in the top left corner, just below the New Query button.

In your query window change 200 to 250 and hit Ctrl + R to refresh.

However, if you really need to update this number of rows, you should probably use raw SQL and write proper UPDATE statements.

EDIT:

In the situation you described, I normally create individual UPDATE statements within Excel and then copy the whole lot to execute in SQL:




回答2:


You can actually paste content to and from excel directly into the ssms editable datagrid. And you can easily change the number of rows available for edit in the ssms "tools > options > Sql Server object explorer" section to return the 250 rows you would need to edit.

As jerry said though it might be easier to write t-sql statements, there is a quick primer on sql update statements over at w3schools

EDIT: based on the discussion in the comments, here is how to update a table based on the values from another table. I've left the original answer above

Provided you have access to a table @myTable with the correct values, and this table has two columns (Name, height), you can do an update FROM

UPDATE t
SET Height = temp.Height
FROM TableIWantToUpdate t
JOIN @myTable temp ON temp.Name = t.Name

EDIT2: as requested, here is a breakdown of the code

UPDATE t

means update the table aliased as "t" in the FROM + JOIN clauses

SET Height = temp.Height

Here we are setting the column Height of the tabled aliased "t" to the value of the column Height in the table aliased temp. The "t" alias is implicit because its been specified in the UPDATE statement

JOIN @myTable temp on temp.Name = t.Name

Basically we are creating a list of records which we want to update, saying that each record in table "t" that matches a record in table "temp" will be updated accordingly.

As to the values in @myTable aliased "temp" in the sample, you could probably get it with some SQL like this

DECLARE @myTable TABLE (name varchar, height properNumericType)
INSERT INTO @myTable (name, height)
SELECT 'John', 5.6
UNION
SELECT 'Mary', 5.4
UNION
...


来源:https://stackoverflow.com/questions/28321879/how-to-edit-multiple-rows-data-entries-in-sql-server-management-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!