使用Update命令更新SQL Server中的列
我想将使用Update命令更新SQL Server中的列,sql,sql-server,tsql,Sql,Sql Server,Tsql,我想将原始内容(下面的url)更新为更新内容(下面的url)。我不确定这方面的T-SQL 原始表格: 首选更新表: 如果你只有几个,你可以做施德荣的建议 如果多次出现尝试列为空的情况,可以首先生成所有更新命令。 实际上,您使用了两个步骤 第一步:生成所有更新命令 select 'update original_table set systemID = ''' + SystemID + ''' where StudentID = ' + convert(varchar, t.studentID)
原始内容(下面的url)更新为更新内容(下面的url)。我不确定这方面的T-SQL
原始表格:
首选更新表:
如果你只有几个,你可以做施德荣的建议
如果多次出现尝试
列为空的情况,可以首先生成所有更新命令。
实际上,您使用了两个步骤
第一步:生成所有更新命令
select 'update original_table set systemID = ''' + SystemID + ''' where StudentID = ' + convert(varchar, t.studentID)
from original_table t
where Attempts is null
步骤2:执行所有命令
select 'update original_table set systemID = ''' + SystemID + ''' where StudentID = ' + convert(varchar, t.studentID)
from original_table t
where Attempts is null
现在,您将获得一个包含所有需要执行的更新命令的记录集。
只需复制它并执行它们
最后删除空的尝试
delete from original_Table where Attempts is null
不要忘了检查生成的更新命令是否正确…如果您只有几个命令,您可以执行D-Shih的建议
如果多次出现尝试
列为空的情况,可以首先生成所有更新命令。
实际上,您使用了两个步骤
第一步:生成所有更新命令
select 'update original_table set systemID = ''' + SystemID + ''' where StudentID = ' + convert(varchar, t.studentID)
from original_table t
where Attempts is null
步骤2:执行所有命令
select 'update original_table set systemID = ''' + SystemID + ''' where StudentID = ' + convert(varchar, t.studentID)
from original_table t
where Attempts is null
现在,您将获得一个包含所有需要执行的更新命令的记录集。
只需复制它并执行它们
最后删除空的尝试
delete from original_Table where Attempts is null
不要忘记检查生成的更新命令是否正确…我假设您的表中有额外的systemId,这样您就可以在一次更新中完成整个表的更新
DECLARE @systemId NVARCHAR(10)
DECLARE @tb table (StudentId int , attempts int , systemId nvarchar(10))
INSERT INTO @tb
VALUES (105,0,'CRU877'),
(105,1,NULL),
(105,2,NULL),
(105,3,NULL),
(106,0,'AUR145'),
(106,1,NULL),
(106,2,NULL),
(106,3,NULL),
(106,4,NULL)
/*Before*/
SELECT *
FROM @tb
UPDATE @tb
SET @systemId = systemId = CASE WHEN systemId IS NULL THEN @systemId ELSE systemId END
/*After*/
SELECT *
FROM @tb
WHERE attempts != 0
我假设您的表中有额外的systemId,因此您可以在一次更新中对整个表执行此操作
DECLARE @systemId NVARCHAR(10)
DECLARE @tb table (StudentId int , attempts int , systemId nvarchar(10))
INSERT INTO @tb
VALUES (105,0,'CRU877'),
(105,1,NULL),
(105,2,NULL),
(105,3,NULL),
(106,0,'AUR145'),
(106,1,NULL),
(106,2,NULL),
(106,3,NULL),
(106,4,NULL)
/*Before*/
SELECT *
FROM @tb
UPDATE @tb
SET @systemId = systemId = CASE WHEN systemId IS NULL THEN @systemId ELSE systemId END
/*After*/
SELECT *
FROM @tb
WHERE attempts != 0
试试这个:
数据生成:
declare @x table(studentId int, attempts varchar(2), systemId varchar(10))
insert into @x values
(105, '1', ''),
(105, '2', ''),
(105, '3', ''),
(105, '', 'CRU877'),
(106, '1', ''),
(106, '2', ''),
(106, '3', ''),
(106, '4', ''),
(106, '', 'AUR145')
更新查询:
update @x set systemId = sysId from (select studentId [stdId], systemId [sysId] from @x where attempts = '') [x] where studentId = stdId
delete @x where attempts = ''
select * from @x
您所附的图片非常模糊,我不知道空白单元格是NULL
s还是空字符串。以防万一,考虑到这一点,有一个解决方案:
declare @x table(studentId int, attempts int, systemId varchar(10))
insert into @x values
(105, 1, null),
(105, 2, null),
(105, 3, null),
(105, null, 'CRU877'),
(106, 1, null),
(106, 2, null),
(106, 3, null),
(106, 4, null),
(106, null, 'AUR145')
update @x set systemId = sysId from (select studentId [stdId], systemId [sysId] from @x where attempts is null) [x] where studentId = stdId
delete @x where attempts is null
select * from @x
试试这个:
数据生成:
declare @x table(studentId int, attempts varchar(2), systemId varchar(10))
insert into @x values
(105, '1', ''),
(105, '2', ''),
(105, '3', ''),
(105, '', 'CRU877'),
(106, '1', ''),
(106, '2', ''),
(106, '3', ''),
(106, '4', ''),
(106, '', 'AUR145')
更新查询:
update @x set systemId = sysId from (select studentId [stdId], systemId [sysId] from @x where attempts = '') [x] where studentId = stdId
delete @x where attempts = ''
select * from @x
您所附的图片非常模糊,我不知道空白单元格是NULL
s还是空字符串。以防万一,考虑到这一点,有一个解决方案:
declare @x table(studentId int, attempts int, systemId varchar(10))
insert into @x values
(105, 1, null),
(105, 2, null),
(105, 3, null),
(105, null, 'CRU877'),
(106, 1, null),
(106, 2, null),
(106, 3, null),
(106, 4, null),
(106, null, 'AUR145')
update @x set systemId = sysId from (select studentId [stdId], systemId [sysId] from @x where attempts is null) [x] where studentId = stdId
delete @x where attempts is null
select * from @x
声明一个表变量,使用update+join组合更新原始表,然后在原始表上运行delete语句。
声明@systemIDs表(studentID int,systemID varchar(50))
插入@systemIDs
从原始文件中选择studentID、systemID,其中systemID不为空
在ot.studentID=si.studentID上更新ot集合ot.systemID=si.systemID,从原始的内部联接@systemID-si更新ot集合ot.studentID
从原始表中删除(尝试次数为null)声明一个表变量,使用update+join组合更新原始表,然后在原始表上运行delete语句。
声明@systemIDs表(studentID int,systemID varchar(50))
插入@systemIDs
从原始文件中选择studentID、systemID,其中systemID不为空
在ot.studentID=si.studentID上更新ot集合ot.systemID=si.systemID,从原始的内部联接@systemID-si更新ot集合ot.studentID
从原始文件中删除尝试为空的内容您可以执行where-isnull(尝试),=”
来修复您提到的模糊问题您可以执行where-isnull(尝试),=”
来修复您提到的模糊问题