这是我必须做的:
我有一个包含 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, YTable 2 包含多列,需要的是UID, X, YTable 1 contains 4 columns: UID, PID, X, YTable 2 contains multiple columns, required ones being UID, 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模板网!
将每个子标记转换为具有多个分隔符的单列-SQLConverting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(将每个子标记转换为具有多个分隔符的单列-SQ
如何从多个表创建视图?How can I create a view from more than one table?(如何从多个表创建视图?)
根据前一行内的计算值创建计算值Create calculated value based on calculated value inside previous row(根据前一行内的计算值创建计算值)
如何将表格的前两列堆叠成一列,但也仅将第三How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何将表格的前两列堆
递归 t-sql 查询Recursive t-sql query(递归 t-sql 查询)
将月份名称转换为日期/月份编号(问题和答案的组Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(将月份名称转换为日期/月份编号(问题和答案的组合