Mssql旧表以新表的不同数据类型和清空

时间:2014-08-19 10:01:42

标签: sql sql-server import

我为一个本地俱乐部的会员申请创建了一个新表,无论如何,现有的软件存储信息存储在DBF文件中我可以将其导入mssql作为一个表格没有问题我只是坚持合并旧数据进入新表。

这是旧的表结构(从DBF导入到mssql中)

[NUMBER] [int] NULL,
[LASTNAME] [char](30) NULL,
[FIRSTNAME] [char](20) NULL,
[TITLE] [char](5) NULL,
[SALUTATION] [char](25) NULL,
[ADD1] [char](30) NULL,
[ADD2] [char](30) NULL,
[ADD3] [char](30) NULL,
[TOWN] [char](30) NULL,
[COUNTY] [char](30) NULL,
[POSTCODE] [char](8) NULL,
[TELEPHONE] [char](30) NULL,
[TYPE] [char](1) NULL,
[PAID] [char](1) NULL,
[COMMITTEE] [char](30) NULL,
[WARNINGS] [int] NULL,
[MONTHDRAW] [int] NULL,
[WOMANS] [char](1) NULL,
[SENIORCIT] [char](1) NULL,
[DOB] [datetime] NULL,
[AMOUNTCLUB] [decimal](6, 2) NULL,
[DATEPAY] [datetime] NULL,
[AMOUNTBRAN] [decimal](6, 2) NULL,
[AMOUNTDUE] [decimal](6, 2) NULL

这是新表:

[MemberID] [nvarchar](10) NOT NULL,
[Title] [nvarchar](10) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[Surname] [nvarchar](50) NOT NULL,
[HouseNumber] [nvarchar](25) NULL,
[Street] [nvarchar](25) NULL,
[City] [nvarchar](25) NULL,
[County] [nvarchar](25) NULL,
[Postcode] [nvarchar](10) NULL,
[TelephoneNumber] [nvarchar](15) NULL,
[MobileNumber] [nvarchar](15) NULL,
[EmailAddress] [nvarchar](50) NULL,
[DOB] [date] NULL,
[Age] [nvarchar](10) NULL,
[SeniorCitizen] [nvarchar](5) NULL,
[Warnings] [nvarchar](50) NULL,
[MembershipCategory] [nvarchar](20) NULL,
[ServiceBranch] [nvarchar](20) NULL,
[Paid] [nvarchar](10) NULL,
[ToPay] [float] NULL,
[DatePaidIn] [date] NULL,
[Entry] [tinyint] NULL,
[FOB] [nvarchar](10) NULL,
[JoinDate] [date] NULL,
[SubscriptionYear] [date] NULL,
[TotalPaid] [float] NULL,

我正在编写插入内容;

INSERT INTO [dbo].[MemberDetails] 
([MemberID],[Title],[FirstName],[Surname],[HouseNumber],[Street],[City],[County],[Postcode],[TelephoneNumber],[MobileNumber],[EmailAddress],[DOB],[Age],[SeniorCitizen],[Warnings],[MembershipCategory],[ServiceBranch],[Paid],[ToPay],[DatePaidIn],[Entry],[FOB],[JoinDate],[SubscriptionYear],[TotalPaid])
SELECT [NUMBER], [TITLE], [FIRSTNAME], [LASTNAME], [ADD1], [ADD2], [TOWN], [COUNTY], [POSTCODE], [TELEPHONE]
FROM [dbo].[MEMBERS] 
GO

我到那个专栏然后意识到“我怎么做空值,因为旧表中没有人有移动记录?”我还需要为旧表值选择AS“新列名”吗?

1 个答案:

答案 0 :(得分:1)

假设您从OldTable转到NewTable,并在原始answer列中添加了question列:

insert  NewTable
        (question, answer)
select  question
,       '42' -- Specify a literal or default value
from    OldTable

您还可以省略insert列表中的新列:

insert  NewTable
        (question)
select  question
from    OldTable

这将复制question列,并将默认值null放在answer列中。

您不必在选择列表中使用别名。这是严格的订单。这样可以正常工作:

insert  NewTable
        (question)
select  question as CeciNEstPasUnePipe
from    OldTable