创建脚本以将表中的数据插入另一个表

时间:2014-07-09 12:53:53

标签: sql sql-server-2008 tsql

我需要将表中的数据插入到不同数据库中的另一个表中。

我有这个脚本来做到这一点:

SELECT 'if not exists (select [PortSymbol] from dbo.Ports where [PortSymbol] =N''' + tbl.Change+ ''' )
    INSERT INTO dbo.Ports([PortSymbol],[HasMovementDiscount], [ModifyDateTime], [PortEnglishName], [PortName] ) 
        values(N'''+tbl.Change+''' , N''False'' , ''GetDate()'',N'''+tbl.EnglishName+''',N'''+tbl.Name+''' )  '
from
(
  SELECT top 100 [Change]
      ,[Name]
      ,[EnglishName] 
  FROM [dbo].[Ports$] where [Name] is not null 

 ) tbl

但这不能正常工作!

这是我的输出:

if not exists (select [PortSymbol] from dbo.Ports where [PortSymbol] =N'AD ' )
        INSERT INTO dbo.Ports([PortSymbol],[HasMovementDiscount], [ModifyDateTime], [PortEnglishName], [PortName] ) 
        values(N'AD ' , N'False' , 'GetDate()',N'',N'.ANDORRA' )
if not exists (select [PortSymbol] from dbo.Ports where [PortSymbol] =N'AD ALV' )
        INSERT INTO dbo.Ports([PortSymbol],[HasMovementDiscount], [ModifyDateTime], [PortEnglishName], [PortName] ) 
        values(N'AD ALV' , N'False' , 'GetDate()',N'Andorra l
if not exists (select [PortSymbol] from dbo.Ports where [PortSymbol] =N'AD CAN' )
        INSERT INTO dbo.Ports([PortSymbol],[HasMovementDiscount], [ModifyDateTime], [PortEnglishName], [PortName] ) 
        values(N'AD CAN' , N'False' , 'GetDate()',N'Canillo',
if not exists (select [PortSymbol] from dbo.Ports where [PortSymbol] =N'AD ENC' )
        INSERT INTO dbo.Ports([PortSymbol],[HasMovementDiscount], [ModifyDateTime], [PortEnglishName], [PortName] ) 
        values(N'AD ENC' , N'False' , 'GetDate()',N'Encamp',N

和源表的架构:

CREATE TABLE [dbo].[Ports$](
[Change] [nvarchar](255) NULL,
[Country] [nvarchar](255) NULL,
[Location] [nvarchar](255) NULL,
[Name] [nvarchar](255) NULL,
[EnglishName] [nvarchar](255) NULL,
[Subdivision] [nvarchar](255) NULL,
[Status] [nvarchar](255) NULL,
[Function] [nvarchar](255) NULL,
[Date] [nvarchar](255) NULL,
[IATA] [nvarchar](255) NULL,
[Coordinates] [nvarchar](255) NULL,
[Remarks] [nvarchar](255) NULL
) ON [PRIMARY]

这是目的地表:

CREATE TABLE [dbo].[Ports](
[PortID] [bigint] IDENTITY(1,1) NOT NULL,
[CityID] [bigint] NULL,
[CountryID] [bigint] NULL,
[PortName] [nvarchar](50) NOT NULL,
[PortEnglishName] [nvarchar](50) NOT NULL,
[PortSymbol] [nvarchar](6) NOT NULL,
[HasMovementDiscount] [bit] NOT NULL,
[ModifyDateTime] [datetime] NOT NULL,
 CONSTRAINT [PK_PORTS] PRIMARY KEY NONCLUSTERED 
(
    [PortID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

1 个答案:

答案 0 :(得分:2)

我认为查询的问题是,如果name或englishname列包含'那么您的输出查询将变为无效。同样对于getdate,你不应该使用''。您可以尝试以下查询吗?

SELECT 'if not exists (select [PortSymbol] from dbo.Ports where [PortSymbol] =N''' + tbl.Change+ ''' )
    INSERT INTO dbo.Ports([PortSymbol],[HasMovementDiscount], [ModifyDateTime], [PortEnglishName], [PortName] ) 
        values(N'''+tbl.Change+''' ,'False', GetDate(),N'''+ REPLACE(tbl.EnglishName,'''','''''') +''',N'''+REPLACE(tbl.Name,'''','''''')+''' )  '
from
(
  SELECT top 100 [Change]
      ,[Name]
      ,[EnglishName] 
  FROM [dbo].[Ports$] where [Name] is not null 

 ) tbl

您必须为所有列执行此操作。如果它们不是基于varchar的字符串,则需要检查数据类型并删除引用