【发布时间】:2009-06-22 15:15:41
【问题描述】:
我必须处理一个表,其中有一组字段,每个字段后跟第二个字段,该字段将保存建议的新值,直到确认此更改。
看起来有点像这样:
refID field1 newField1 field2 newField2 ...
refID 是链接到主表的 ID 值。主表中的一行在我的明细表中可以有 n 行。数据类型包括整数、字符串和日期时间。
现在,我希望有一个查询告诉我,给定一个 refID,如果在详细信息表中有任何建议的更改。
我正在玩一些 UNION 选择,使用 COALESCE() 和 ISNULL() ......但所有这些尝试充其量看起来有点奇怪。数据库是 MS-SQL-Server 2005。
为了澄清我的问题:
--this is a simplification of the details table in question
CREATE TABLE [dbo].[TEST_TABLE](
[ID] [int] IDENTITY(1,1) NOT NULL,
[refID] [int] NOT NULL,
[firstName] [varchar](50) NULL,
[newFirstName] [varchar](50) NULL,
[lastName] [varchar](50) NULL,
[newLastName] [varchar](50) NULL
)
--here we insert a detail row ... one of many that might exist for the master table (e.g. data about the company)
insert into TEST_TABLE(refID, firstName, lastName) values(666, 'Bill', 'Ballmer')
--this is what happens when a user saves a suggested change
update TEST_TABLE SET newLastName = 'Gates' where ID = 1
--and this is what happens when this suggestion is accepted by a second user
update TEST_TABLE set lastName=newLastName, newLastName = NULL where ID = 1
【问题讨论】:
-
您可以为小样本发布一些 DDL 吗?我不清楚你到底想要什么。