【问题标题】:Avoid adding duplicate values避免添加重复值
【发布时间】:2020-09-23 04:35:28
【问题描述】:

我有这种方法可以在另一个数据库的表中添加来自另一个数据库的相同值:

public function insert()
{
    $indexes= DB::connection('sqlsrv')
            ->select
            (
            "
            IF  NOT EXISTS (SELECT * FROM sys.objects 
            WHERE object_id = OBJECT_ID(N'[test].[dbo].[indexes]') AND type in (N'U'))
            CREATE TABLE test.dbo.indexes 
                (
                    table_view nvarchar(500) not null,
                    [columns] nvarchar(500) not null,
                    [type] nvarchar(50) not null,
                    index_name nvarchar(500) not null,
                    index_id int not null
                )
            insert into test.dbo.indexes (table_view, [columns], [type], index_name, index_id)

            select 
            schema_name(t.schema_id) + '.' + t.[name] as table_view,
            substring(column_names, 1, len(column_names)-1) as [columns],
            case when i.is_primary_key = 1 then 'Primary_key'
            when i.is_unique = 1 then 'Unique'
            else 'Not_unique' end as [type],
            i.[name] as index_mane,
            i.index_id
            from sys.objects t
            inner join sys.indexes i
            on t.object_id = i.object_id
            cross apply (select col.[name] + ', '
                from sys.index_columns ic
                    inner join sys.columns col
                        on ic.object_id = col.object_id
                        and ic.column_id = col.column_id
                where ic.object_id = t.object_id
                    and ic.index_id = i.index_id
                        order by col.column_id
                        for xml path ('') ) D (column_names)
            where t.is_ms_shipped <> 1
            and index_id > 0
            order by schema_name(t.schema_id) + '.' + t.[name], i.index_id
            "
            );
}

问题是当我多次运行时,记录会被复制。我能做些什么来阻止这种情况?我需要它,当我第二次、第三次、X 次运行它时,只添加不同的部分。

【问题讨论】:

  • 我不是想将某个表中的某些内容复制到另一个表中,而是尝试将该选择的结果放在我在脚本开头创建的表中,所以这不起作用
  • 试过了,但没用,可能是我学习的时候不知道在哪里添加这一行:(

标签: php sql sql-server laravel


【解决方案1】:

使用not exists 条件来测试重复项,如下所示。

insert into test.dbo.indexes (table_view, [columns], [type], index_name, index_id)
select table_view, [columns], [type], index_name, index_id
from (
    select 
        schema_name(t.[schema_id]) + '.' + t.[name] as table_view
        , substring(column_names, 1, len(column_names)-1) as [columns]
        , case when i.is_primary_key = 1 then 'Primary_key'
            when i.is_unique = 1 then 'Unique'
            else 'Not_unique' end as [type]
        , i.[name] as index_name
        , i.index_id
    from sys.objects t
    inner join sys.indexes i
    on t.[object_id] = i.[object_id]
    cross apply (
        select col.[name] + ', '
        from sys.index_columns ic
        inner join sys.columns col on ic.[object_id] = col.[object_id] and ic.column_id = col.column_id
        where ic.[object_id] = t.[object_id]
        and ic.index_id = i.index_id
        order by col.column_id
        for xml path ('')
    ) D (column_names)
    where t.is_ms_shipped <> 1
    and index_id > 0
) X
-- The following where clause prevents the insertion of duplicates
where not exists (
    select 1
    from test.dbo.indexes I
    where I.table_view = X.table_view and I.[columns] = X.[columns] and I.[type] = X.[type] and I.index_name = X.index_name and I.index_id = X.index_id
);

注意:在insert 语句中添加order by 没有任何好处,表格本质上是无序的,如果顺序很重要,您必须在选择时使用order by

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2011-11-11
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2019-03-30
    相关资源
    最近更新 更多