Sql server 更改所有行的列值
我有以下表格:Sql server 更改所有行的列值,sql-server,tsql,Sql Server,Tsql,我有以下表格: create table dbo.Files ( Id int identity not null constraint PK_Files_Id primary key (Id), Content varbinary (max) null, Name nvarchar (280) null ) create table dbo.BookFiles ( BookId int not null, FileId int not null, constr
create table dbo.Files (
Id int identity not null constraint PK_Files_Id primary key (Id),
Content varbinary (max) null,
Name nvarchar (280) null
)
create table dbo.BookFiles (
BookId int not null,
FileId int not null,
constraint PK_BookFiles_Id primary key (BookId, FileId)
)
alter table dbo.BookFiles
add constraint FK_BookFiles_BookId foreign key (BookId) references Books(Id) on delete cascade on update cascade,
constraint FK_BookFiles_FileId foreign key (FileId) references Files(Id) on delete cascade on update cascade;
一些数据如下:
BookId FileId Id Name Content
1 1 1 image
1 2 1 image
2 3 2 image
3 4 3 image
有些书有两个图像(BookdId=1),有些书有一个图像(BookId=2,3)
我需要将所有文件的名称重命名为:
UPDATE files SET name = "image1";
但我不知道如何设置“image1”和“image2”
有人能帮我吗?从您的问题中提取数据,下面的代码片段将能够根据您的需要提供结果-
declare @t table (BookId int, FileId int, Id int, Name nvarchar(280), Content varbinary (max))
insert into @t values
(1, 1, 1, 'image',null),
(1, 2, 1, 'image',null),
(2, 3, 2, 'image',null),
(3, 4, 3, 'image',null)
;with cte as
(
select *, ROW_NUMBER() OVER(PARTITION BY BookId ORDER BY BookId) rn
from @t
)
select BookId,FileId,Id,(Name+cast(rn as nvarchar(1))) AS Name,Content from cte
使用子查询筛选出具有多个图像的书籍
update t set t.name = case when fileid = 1 then 'ima1' else 'ima2' end
from table t
where bookid in (
select bookid from table
group by bookid
having count(*) > 1
)
为了更新所有图像文件,您可以使用排名函数行号()
您可以尝试使用CTE
。它不特定于只有2个相同id的图像,它将适用于任何数量的图像,您有一本书,数字将相应地追加
;WITH CT AS
(
SELECT BOOKID, FILEID, ROW_NUMBER() OVER
(PARTITION BY BookId ORDER BY BOOKID) AS RN , NAME
FROM BookFiles BF INNER JOIN Files F ON F.Id = BF.FileId
)
UPDATE CT
SET NAME= 'image' + CAST(RN AS VARCHAR(10))
注意:您对CTE
所做的更新将级联到源表
;WITH CT AS
(
SELECT BOOKID, FILEID, ROW_NUMBER() OVER
(PARTITION BY BookId ORDER BY BOOKID) AS RN , NAME
FROM BookFiles BF INNER JOIN Files F ON F.Id = BF.FileId
)
UPDATE CT
SET NAME= 'image' + CAST(RN AS VARCHAR(10))