1

私が行った複雑な更新のこの簡単な例を作成しましたが、更新値を取得する選択に参加していないため、これが実際に機能することに驚いています。場所が存在する部分でのみです。

だから私の質問は、これがどのように機能するのですか? 結合されていない場合、正しいレコードを実際に更新するにはどうすればよいですか?

UPDATE dbo.gg_ddl_changes_mssql
SET gg_ddl_changes_mssql.column_id       = a.ordinal_position, 
    gg_ddl_changes_mssql.default_value   = a.default_value, 
    gg_ddl_changes_mssql.nullable        = a.is_nullable, 
    gg_ddl_changes_mssql.data_type       = a.data_type, 
    gg_ddl_changes_mssql.data_length     = a.character_maximum_length, 
    gg_ddl_changes_mssql.data_precision  = a.numeric_precision,
    gg_ddl_changes_mssql.data_scale      = a.numeric_scale, 
    gg_ddl_changes_mssql.date_precision  = a.datetime_precision, 
    gg_ddl_changes_mssql.pk_id           = a.pk_id, 
    gg_ddl_changes_mssql.pk_name         = a.CONSTRAINT_NAME, 
    gg_ddl_changes_mssql.last_updated    = GETDATE()
FROM (SELECT c.ordinal_position, 
             LEFT(c.column_default, 2000) as "default_value", 
             c.is_nullable, c.data_type, 
             c.character_maximum_length, 
             c.numeric_precision, 
             c.numeric_scale, 
             c.datetime_precision, 
             pc.ordinal_position as "pk_id", 
             pc.constraint_name
   from INFORMATION_SCHEMA.tables  AS t, 
        INFORMATION_SCHEMA.COLUMNS AS c
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS p
  ON c.table_name = p.table_name
LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE pc
  ON (p.constraint_name = pc.constraint_name
  AND c.column_name     = pc.column_name)
 WHERE t.table_type       = 'BASE TABLE' 
   AND t.table_name       = c.table_name 
   AND t.table_schema     = c.table_schema 
   AND p.constraint_type  = 'PRIMARY KEY'
   ) as a
WHERE EXISTS (SELECT *
                FROM INFORMATION_SCHEMA.tables  AS t1, 
                     INFORMATION_SCHEMA.COLUMNS AS c1
              LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS p1
                ON c1.table_name = p1.table_name
              LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE pc1
                ON (p1.constraint_name = pc1.constraint_name
                AND c1.column_name     = pc1.column_name)
               WHERE t1.table_type       = 'BASE TABLE' 
                 AND c1.table_schema     = gg_ddl_changes_mssql.table_schema
                 AND c1.table_name       = gg_ddl_changes_mssql.table_name
                 AND c1.column_name      = gg_ddl_changes_mssql.column_name
                 AND t1.table_name       = c1.table_name 
                 AND t1.table_schema     = c1.table_schema 
                 AND p1.constraint_type  = 'PRIMARY KEY'
                 AND ( c1.ordinal_position             != gg_ddl_changes_mssql.column_id
                   OR  LEFT(c1.column_default, 2000)   != gg_ddl_changes_mssql.default_value
                   OR  c1.is_nullable                  != gg_ddl_changes_mssql.nullable
                   OR  c1.data_type                    != gg_ddl_changes_mssql.data_type
                   OR  c1.character_maximum_length     != gg_ddl_changes_mssql.data_length
                   OR  c1.numeric_precision            != gg_ddl_changes_mssql.data_precision 
                   OR  c1.numeric_scale                != gg_ddl_changes_mssql.data_scale
                   OR  c1.datetime_precision           != gg_ddl_changes_mssql.date_precision
                   or  pc1.ordinal_position            != gg_ddl_changes_mssql.pk_id
                   or  pc1.constraint_name             != gg_ddl_changes_mssql.pk_name))

Oracle では、tableY に複数のレコードがある場合、select 値が多くの値を返すため、値を取得します。

4

2 に答える 2