【问题标题】:How to create a temporary Primary Key Azure TSQL Server如何创建临时主键 Azure SQL Server
【发布时间】:2015-09-23 18:53:52
【问题描述】:

我需要在一个名为 User 的表中检查可能的重复地址。为此,我使用 JOIN 然后比较地址字符串中第一组街道号码的表。例如“第一街 123 号”和“第一街 123 号”。需要被识别为可能的匹配/重复。

但是,我还需要显示记录的 Telerik RadGrid 具有唯一的 DataKeyName 来识别双击 javascript 函数工作的行。

由于 JOIN,我的正常主键在“两个”表 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;

【问题讨论】:

    标签: sql tsql telerik radgrid


    【解决方案1】:

    如果您只想为每一行生成一个随机 ID,请将 NEWID() 函数添加到您的列列表中。

    例如: SELECT NEWID() AS PsuedoKey, * FROM ...

    【讨论】:

    • 这看起来不错@dgiard 我会尝试一下并告诉你。谢谢!
    • 你摇滚先生!谢谢@dgiard!
    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2011-11-03
    • 2012-09-17
    • 1970-01-01
    相关资源
    最近更新 更多