我建議學習必要的SQL更新相應的數據您可以使用SELECT語句和ORDER BY子句按照您希望查看它的順序查看數據,然後構建一個查詢來更新該數據。
您可以使用事務來確定您的更新(如果您仍在學習SQL並且不想搞亂數據庫)
BEGIN TRANSACTION -- starts a transaction
ROLLBACK -- stops the transaction and rolls back all changes to the tables
COMMIT -- stops the transaction and commits all changes to the tables
你想完成/更新什麼,也許我們可以幫助你呢?
編輯
你提到你想編輯存儲在表內的一些產品名稱。而且這將是一次性任務。我已經在下面設置了一個小型演示,希望能幫助您指導一個可能適合您情況的解決方案。將其複製並粘貼到SQL Management Studio會話中。
此外,如果你想,你可以導出你當前的數據來說excel,在excel中編輯數據,將它作爲新的臨時表導入並運行SQL更新腳本來更新原始表。
/*
Products Before Update Products After Update
=========================== =============================================
ID ProductName ID ProductName
--------------------------- ---------------------------------------------
1 MSFT 1 Microsoft Corp.
2 APPL 2 Apple Inc.
3 Cisco Systems, Inc. 3 Cisco Systems, Inc.
4 IBM 4 International Business Machines Corp.
5 JAVA 5 Sun Microsystems, Inc.
6 ORCL 6 Oracle Corp.
*/
-- Imagine that this table is a table in your database
DECLARE @products TABLE (
ID INT,
ProductName VARCHAR(255)
)
-- And this table has some product information
-- which you are trying to update with new information
INSERT @products
SELECT 1, 'MSFT' UNION ALL
SELECT 2, 'APPL' UNION ALL
SELECT 3, 'Cisco Systems, Inc.' UNION ALL
SELECT 4, 'IBM' UNION ALL
SELECT 5, 'JAVA' UNION ALL
SELECT 6, 'ORCL'
-- Either build an in-memory temporary table of the product names you wish to update
-- Or do a database task to import data from excel into a temporary table in the database
DECLARE @products_update TABLE (
ID INT,
ProductName VARCHAR(255)
)
INSERT @products_update
SELECT 1, 'Microsoft Corp.' UNION ALL
SELECT 2, 'Apple Inc.' UNION ALL
SELECT 4, 'International Business Machines Corp.' UNION ALL
SELECT 5, 'Sun Microsystems, Inc.' UNION ALL
SELECT 6, 'Oracle Corp.'
-- Update the table in the database with the in-memory table
-- for demo purposes, we use @products to represent the database table
UPDATE p1
SET ProductName = ISNULL(p2.ProductName, p1.ProductName)
FROM @products p1
LEFT JOIN @products_update p2
ON p1.ID = p2.ID
-- Now your products table has been updated
SELECT *
FROM @products
是否有與SQL Server任何集成? – 2009-05-22 20:06:30
是的。它給你一個靈活的表視圖(重新排列列,排序等),但不影響表結構 – gbn 2009-05-23 06:36:47