help with logic flow for database updates
I have a database table with the following fields...
key,
db_name,
string_ID_in_DB,
text1,
text2
text1 is full of duplicates and I would like to show开发者_如何学C it once, as the users need to populate text2 based on text1 content.
I then need to update text2 in each db_name database. If I only display the non duplicate text1 fields I eliminate 20,000 records from display, but I need to figure out how to keep track of these so that I can then update them in the corresponding db_name databases.
Any suggestions on how to do this would be greatly appreciated.
thanks,
I'm really not sure what you are after, the question is not very clear. Also, not all SQL is the same (OP doesn't say what their using), so here is a way to do it in SQL Server:
DECLARE @YourTable table (KeyID int, text1 varchar(10))
INSERT @YourTable VALUES (1,'aaa')
INSERT @YourTable VALUES (2,'bbb')
INSERT @YourTable VALUES (3,'aaa')
INSERT @YourTable VALUES (4,'ccc')
INSERT @YourTable VALUES (5,'ccc')
INSERT @YourTable VALUES (6,'ccc')
INSERT @YourTable VALUES (7,'ddd')
INSERT @YourTable VALUES (8,'aaa')
--just display the first occurrence rows of each text1 value
SELECT
*
FROM (SELECT KeyID,text1,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber FROM @YourTable) dt
WHERE RowNumber=1
--delete all duplicates
;WITH NumberedRows AS
(SELECT
KeyID,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber
FROM @YourTable
)
DELETE NumberedRows
WHERE RowNumber>1
select * from @YourTable --<<display remaining rows
OUTPUT:
KeyID text1
----------- ---------- <<from 1st select
1 aaa
2 bbb
4 ccc
7 ddd
(4 row(s) affected)
KeyID text1
----------- ---------- <<from second select after delete
1 aaa
2 bbb
4 ccc
7 ddd
(4 row(s) affected)
For your UI, where you want to display unique records only...
SELECT DISTINCT
text1,
text2
FROM table
For your update...
UPDATE table
SET text2 = @text2
WHERE
text1 = @text1
As I interpret your question, your source table (for your SELECT in the UI) is only coming from one database (?). And then your update should be pushed out to every database (?). If so, then use the update above. If not -- maybe this:
For your UI, where you want to display unique records only...
SELECT DISTINCT
db_name,
text1,
text2
FROM table
For your update...
UPDATE table
SET text2 = @text2
WHERE
text1 = @text1
AND db_name = @db_name
精彩评论