Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 创建插入查询(外键约束错误)_Sql_Sql Server_Sql Server 2005_Insert_Foreign Keys - Fatal编程技术网

Sql 创建插入查询(外键约束错误)

Sql 创建插入查询(外键约束错误),sql,sql-server,sql-server-2005,insert,foreign-keys,Sql,Sql Server,Sql Server 2005,Insert,Foreign Keys,我想将数据从一个数据库的表移动到另一个数据库的表。我收到一个外键错误。如何插入除没有外键的行之外的所有有效数据 我的问题是: SET IDENTITY_INSERT City ON INSERT INTO City ([cityid],[city],[country],[state],[cityinfo] ,[enabled],[countryid],[citycode],[stateid],[latitude],[longitude]) SELECT [cityid],[ci

我想将数据从一个数据库的表移动到另一个数据库的表。我收到一个外键错误。如何插入除没有外键的行之外的所有有效数据

我的问题是:

  SET IDENTITY_INSERT City ON

  INSERT INTO City ([cityid],[city],[country],[state],[cityinfo]
  ,[enabled],[countryid],[citycode],[stateid],[latitude],[longitude])
  SELECT [cityid],[city],[country],[state],[cityinfo]
  ,[enabled],[countryid],[citycode],[stateid],[latitude],[longitude]
  FROM TD.DBo.City
获取此错误:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__city__countryid__3E52440B". The conflict occurred in database "schoolHigher", table "dbo.country", column 'countryId'.

内部联接
另一个数据库的表与
国家/地区
表。只有具有现有国家/地区的记录才会被选中

注意:您应该检查两个数据库中相应的countryid是否匹配

SET IDENTITY_INSERT City ON

INSERT INTO City (
    [cityid]
    ,[city]
    ,[country]
    ,[state]
    ,[cityinfo]
    ,[enabled]
    ,[countryid]
    ,[citycode]
    ,[stateid]
    ,[latitude]
    ,[longitude])
SELECT  ct.[cityid]
        ,ct.[city]
        ,ct.[country]
        ,ct.[state]
        ,ct.[cityinfo]
        ,ct.[enabled]
        ,ct.[countryid]
        ,ct.[citycode]
        ,ct.[stateid]
        ,ct.[latitude]
        ,ct.[longitude]
FROM  TD.DBo.City ct
      INNER JOIN dbo.Country cnt ON cnt.CountryID = ct.CountryID

别客气,也别忘了检查两个国家/地区表。