数据库内连接更新

时间:2016-11-07 22:13:53

标签: sql sql-server tsql

我正在尝试从另一个表中的表列更新表列,并使用以下代码 - 它会在下面生成错误。

有什么想法吗?

UPDATE URLRecord

SET URLRecord.Slug = aaNewURLSlugss.NewSlugName     来自URLRecord     INNER JOIN aaNewURLSlugs ON URLRecord.Slug = aaNewURLSlugss.OldSlugName

错误:

  

Msg 102,Level 15,State 1,Line 1
  ','。

附近的语法不正确

4 个答案:

答案 0 :(得分:0)

SQL Server不喜欢<div class="col-md-12 no-padding"> <select id="serviceAccounts" kendo-drop-down-list k-ng-model="vm.customer.serviceAccountId" k-value-primitive="true" k-options="vm.setServiceAccountOptions" style="width: 100%"> </select> </div> 中的限定列名。这有用吗?

SET

但是,您收到的错误消息会有所不同。这将是一个额外的错误。

答案 1 :(得分:0)

这是一个使用临时表的工作解决方案。它可能不太理想,我已经包含了一些drop语句,所以不要在你的生产机器上运行它。

-- Build our test tables
CREATE TABLE [URLRecord]
(
    [Id] INTEGER IDENTITY PRIMARY KEY,
    [Slug] CHARACTER VARYING(255) NOT NULL
)

GO

CREATE TABLE [NewSlugs]
(
    [Id] INTEGER IDENTITY PRIMARY KEY,
    [NewSlugName] CHARACTER VARYING(255) NULL,
    [OldSlugName] CHARACTER VARYING(255) NULL
)

GO

-- Insert test data
INSERT INTO [URLRecord] VALUES ('Original Name');
INSERT INTO [NewSlugs] VALUES ('New Name', 'Original Name');

-- Populate work table with the records we want to modify
SELECT [URLRecord].[Id], [NewSlugs].[NewSlugName] INTO #SlugWork
FROM [URLRecord]
INNER JOIN [NewSlugs] ON [URLRecord].[Slug] = [NewSlugs].[OldSlugName]

-- We're just echoing here
SELECT * FROM #SlugWork

-- Pull everything from our temporary table and update modified records
UPDATE [URLRecord]
SET [URLRecord].[Slug] = [T].[NewSlugName]
FROM #SlugWork AS [T]
WHERE [T].[Id] = [URLRecord].[Id]

-- Proving it's updated
SELECT * FROM [URLRecord]

-- Drop our example stuff, for rerunnability
DROP TABLE #SlugWork
DROP TABLE [NewSlugs]
DROP TABLE [URLRecord]

答案 2 :(得分:-1)

尝试使用这样的嵌入式SQL:

UPDATE URLRecord
  SET Slug = (
     SELECT NewSlugName 
     FROM aaNewURLSlugs 
     WHERE URLRecord.Slug = aaNewURLSlugs.OldSlugName )
WHERE Slug IN (SELECT OldSlugName FROM aaNewURLSlugs)

答案 3 :(得分:-1)

尝试使用别名

NSMeasurementFormatter