如何创建临时主键 Azure SQL Server
How to create a temporary Primary Key Azure TSQL Server
我需要检查一个名为 User 的 table 中可能存在的重复地址。为此,我使用 JOIN,然后比较地址字符串中第一组街道号码的 tables。例如“123 First Street”和“123 First St.”需要被识别为可能 match/duplicate。
不过,我还需要显示记录的 Telerik RadGrid 有一个唯一的 DataKeyName 来标识行,以便双击 javascript 功能正常工作。
由于 JOIN,我的正常主键在 "both" tables a 和 b 中重复。如何使用 SQL 创建一个临时伪主键,以便我的数据网格可以识别双击的行?
try
{
//Select Query to populate the RadGrid.
string selectQuery =
"SELECT " +
//We rename the dbo.User table as "a" then rename it again as "b" so we can look for duplicate Street Address numbers
"a.Id AS LeftID,a.DateSubmitted AS LeftDateSubmitted,a.Updated AS LeftUpdated," +
"a.Status AS LeftStatus,a.StreetAddress AS LeftStreetAddress," +
"b.Id AS RightID,b.DateSubmitted AS RightDateSubmitted,b.Updated AS RightUpdated," +
"b.Status AS RightStatus,b.StreetAddress AS RightStreetAddress " +
//We join the 2 virtual dbo.User tables where table b Id's are greater than table a meaning b records are newer
"FROM [User] a JOIN [User] b ON b.Id > a.Id AND " +
//LEFT selects the left most characters (usually numbers) in the StreetAddress field string before the space ' '
//and eliminates the rest of the address isolating just the street address numbers for matching
"LEFT(a.StreetAddress,CHARINDEX(' ',a.StreetAddress)) = LEFT(b.StreetAddress,CHARINDEX(' ',b.StreetAddress)) " +
//Don't show orange or blue status records
"AND b.Status != 'Orange' AND a.Status != 'Orange' AND a.Status != 'Blue' AND b.Status != 'Blue' " +
//If a b record (newer) is red then ignore because it is completed and ignore a records (oldest) older than 90 days
"WHERE a.DateSubmitted >= (GetDate() - 90) AND b.Status != 'Red' " +
//Show newest records first
"ORDER BY b.DateSubmitted DESC"
;
SqlDataAdapter.SelectCommand = new SqlCommand(selectQuery, SqlConnection);
SqlDataAdapter.Fill(dtTable);
RadGrid1.DataSource = dtTable;
如果您只想为每一行生成一个随机 ID,请将 NEWID() 函数添加到列列表中。
例如:
SELECT NEWID() 作为伪密钥,* 来自 ...
我需要检查一个名为 User 的 table 中可能存在的重复地址。为此,我使用 JOIN,然后比较地址字符串中第一组街道号码的 tables。例如“123 First Street”和“123 First St.”需要被识别为可能 match/duplicate。
不过,我还需要显示记录的 Telerik RadGrid 有一个唯一的 DataKeyName 来标识行,以便双击 javascript 功能正常工作。
由于 JOIN,我的正常主键在 "both" tables a 和 b 中重复。如何使用 SQL 创建一个临时伪主键,以便我的数据网格可以识别双击的行?
try
{
//Select Query to populate the RadGrid.
string selectQuery =
"SELECT " +
//We rename the dbo.User table as "a" then rename it again as "b" so we can look for duplicate Street Address numbers
"a.Id AS LeftID,a.DateSubmitted AS LeftDateSubmitted,a.Updated AS LeftUpdated," +
"a.Status AS LeftStatus,a.StreetAddress AS LeftStreetAddress," +
"b.Id AS RightID,b.DateSubmitted AS RightDateSubmitted,b.Updated AS RightUpdated," +
"b.Status AS RightStatus,b.StreetAddress AS RightStreetAddress " +
//We join the 2 virtual dbo.User tables where table b Id's are greater than table a meaning b records are newer
"FROM [User] a JOIN [User] b ON b.Id > a.Id AND " +
//LEFT selects the left most characters (usually numbers) in the StreetAddress field string before the space ' '
//and eliminates the rest of the address isolating just the street address numbers for matching
"LEFT(a.StreetAddress,CHARINDEX(' ',a.StreetAddress)) = LEFT(b.StreetAddress,CHARINDEX(' ',b.StreetAddress)) " +
//Don't show orange or blue status records
"AND b.Status != 'Orange' AND a.Status != 'Orange' AND a.Status != 'Blue' AND b.Status != 'Blue' " +
//If a b record (newer) is red then ignore because it is completed and ignore a records (oldest) older than 90 days
"WHERE a.DateSubmitted >= (GetDate() - 90) AND b.Status != 'Red' " +
//Show newest records first
"ORDER BY b.DateSubmitted DESC"
;
SqlDataAdapter.SelectCommand = new SqlCommand(selectQuery, SqlConnection);
SqlDataAdapter.Fill(dtTable);
RadGrid1.DataSource = dtTable;
如果您只想为每一行生成一个随机 ID,请将 NEWID() 函数添加到列列表中。
例如: SELECT NEWID() 作为伪密钥,* 来自 ...