本文介紹了SQl:從文本文件更新表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
這是我必須做的:
我有一個包含 3 列的文本文件:PID、X、Y
.
I have a text file which has 3 columns: PID, X, Y
.
現在我的數據庫中有兩個表:
Now I have two tables in my database:
表1
包含4列:UID, PID, X, Y
Table 2
包含多列,需要的是UID, X, Y
Table 1
contains 4 columns:UID, PID, X, Y
Table 2
contains multiple columns, required ones beingUID, X, Y
我需要用相應的 X 和 Y 值更新表 2
.
I need to update Table 2
with corresponding X and Y values.
我認為我們可以使用 BULK INSERT
來更新 table 1
,然后使用一些 WHILE
循環或其他東西.
I think we can use BULK INSERT
for updating table 1
, then some WHILE
loop or something.
但我無法弄清楚確切的事情.
But I can't figure out exact thing.
推薦答案
CREATE PROCEDURE [dbo].[BulkInsert]
(
@PID int ,
@x int,
@y int,
)
AS
BEGIN
SET NOCOUNT ON;
declare @query varchar(max)
CREATE TABLE #TEMP
(
[PID] [int] NOT NULL ,
[x] int NOT NULL,
[y] int NOT NULL,
)
SET @query = 'BULK INSERT #TEMP FROM ''' + PathOfYourTextFile + ''' WITH ( FIELDTERMINATOR = '','',ROWTERMINATOR = ''\n'')'
--print @query
--return
execute(@query)
BEGIN TRAN;
MERGE TableName AS Target
USING (SELECT * FROM #TEMP) AS Source
ON (Target.YourTableId = Source.YourTextFileFieldId)
-- In the above line we are checking if the particular row exists in the table(Table1) then update the Table1 if not then insert the new row in Table-1.
WHEN MATCHED THEN
UPDATE SET
Target.PID= Source.PID, Target.x= Source.x, Target.y= Source.y
WHEN NOT MATCHED BY TARGET THEN
-- Insert statement
您可以使用上述方法來解決您的問題.希望這可以幫助.:)
You can use this above approach to solve your problem. Hope this helps. :)
這篇關于SQl:從文本文件更新表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!