2014-10-29 50 views
-1

在我的C#应用​​程序中,我正在执行多个更新查询来处理数据库表中的数据。例如。将特定字符集替换为不同的字符集,插入新字符并删除字符。当这样的查询已经执行时,我想做两件事。获取受影响行的总行数并获取受影响行的row_number()结果集。第一件事很简单,已经在工作。然而,第二件事是我还没有弄清楚的。在UPDATE语句后返回受影响行的row_number()

下面是一个查询的例子,我可以使用,当我操纵数据:

UPDATE myTable 
SET myColumn = STUFF(myColumn, fromCharPos, toCharPos, 
REPLACE(SUBSTRING(myColumn, fromCharPos, toCharPos), charToReplace, charReplacement)) 
WHERE LEN(myColumn) >= fromCharPos; 

这个查询替代(在列的所有细胞)一个字符与另一个字符中的一组设置指定的字符范围。

当执行此查询时,我想从受影响的行中获取行号的结果集。任何人都知道我能够如何实现这一点?

有些事情要考虑:

  • 它必须在2005年和高达ATLEAST服务器版本。
  • 的UPDATE语句交易

如果有不清楚的地方,请评论下面,所以我能够提高我的问题中执行。

编辑: 我注意到,它不是很清楚我想实现什么。 比方说我们有一组数据,看起来像这样:

34.56.44.12.33 
32.44.68 
45.22.66.33.77 
44.42.44 
66.44.22.44.45 
00.22.78 
43.98.34.65.33 

现在我想用字符位置9之间的下划线替换点至12。这意味着,只有这些行会被查询的影响:

34.56.44.12.33 <-- 
32.44.68 
45.22.66.33.77 <-- 
44.42.44 
66.44.22.44.45 <-- 
00.22.78 
43.98.34.65.33 <-- 

我想实现的目标是获取受影响行的行号结果集。在我的例子,这将是设置这样的结果:

Row_number() 
1 
3 
5 
7 
+0

SELECT @@ ROWCOUNT – 2014-10-29 09:04:01

+0

@ t-clausen.dk - 他提到“第一件事是相当简单的,并已经工作“ – 2014-10-29 09:06:02

+0

你是什么意思”我想从受影响的行中得到行号的结果集“可以解释更多一点。 – 2014-10-29 09:11:37

回答

0

更新

declare @table table(val varchar(500)) 

insert into @table values 
('34.56.44.12.33'), 
('32.44.68'), 
('45.22.66.33.77'), 
('44.42.44'), 
('66.44.22.44.45'), 
('00.22.78'), 
('43.98.34.65.33') 


--select * from @table 

declare @temp table(rowid int,val varchar(500), createdate datetime) 
insert into @temp 
select ROW_NUMBER() over(order by val), val, GETDATE() from @table 

declare @rowEffectedCount int = 0 
--select ROW_NUMBER() over(order by val), val, GETDATE() from @table WHERE CHARINDEX('.',val,9) > 0 

UPDATE @table 
SET val = 
REPLACE(SUBSTRING(val, CHARINDEX('.',val,9), LEN(val)), ',', '_') 
WHERE CHARINDEX('.',val,9) > 0 
set @rowEffectedCount = @@ROWCOUNT 

select @rowEffectedCount roweffected ,* from @temp t1 
where val not in (
select val from @table) 

它很为我的理解简单。

您只需为您的更新查询添加一个选择查询。阅读评论更多的了解

declare @rowEffectedCount int = 0 

--you can use a temp table or permanet history table to take each work of portion. 
--one thing to be carefull as structure is same or only save the pk , then no issue 

--create table tempRowcount and insert the statement with the same where query, which filter the same data. 
declare @t table(id int, createdate datetime) 

select * into @t from myTable WHERE LEN(myColumn) >= fromCharPos 
--or only pk 
select id into @t from myTable WHERE LEN(myColumn) >= fromCharPos 

--now update the 
UPDATE myTable 
SET myColumn = STUFF(myColumn, fromCharPos, toCharPos, 
REPLACE(SUBSTRING(myColumn, fromCharPos, toCharPos), charToReplace, charReplacement)) 
WHERE LEN(myColumn) >= fromCharPos; 


select * from @t 
--finally delete or truncate or drop the table(if you use permanent table) 
0

这可能会帮助你..

CREATE TABLE #updatetablename 
    (excolumn VARCHAR(100)) 

INSERT INTO #updatetablename 
VALUES  ('34.56.44.12.33'), 
      ('32.44.68'), 
      ('45.22.66.33.77'), 
      ('44.42.44'), 
      ('66.44.22.44.45'), 
      ('00.22.78'), 
      ('43.98.34.65.33') 

DECLARE @temp TABLE 
    (excolumn VARCHAR(100)) 
DECLARE @temp1 TABLE 
    (row_num INT,excolumn VARCHAR(100)) 

INSERT INTO @temp1 
SELECT Row_number()OVER (ORDER BY excolumn),* 
FROM #updatetablename 

UPDATE #updatetablename 
SET excolumn = Replace(excolumn, '.', '_') 
output deleted.excolumn 
INTO @temp 
WHERE Len(excolumn) > 12 

SELECT b.row_num AS updatedrows, 
     a.excolumn 
FROM @temp a 
     JOIN @temp1 b 
     ON a.excolumn = b.excolumn 
相关问题