我怎样才能将sql文件更改并运行到mssql中

时间:2013-12-12 10:39:14

标签: sql sql-server

我的数据库是在sqlite。我从sqlite manager导出了sql文件。现在,在将该文件执行到Microsoft SQL Server 2012中时,它会在以下行中给出错误。

DROP TABLE

IF EXISTS "Admin";
    CREATE TABLE "AdvancePayments" (
        "Id" INT PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
        "date" VARCHAR,
        "Name" VARCHAR,
        "Amount" INT,
        "Remarks" VARCHAR);

INSERT INTO "AdvancePayments"
VALUES (
    1,
    '11/22/2013',
    'wqqw',
    21,
    'fgdsr');

CREATE TABLE "DVouchers" (
    "DVid" INT PRIMARY KEY NOT NULL,
    "vdate" VARCHAR DEFAULT(NULL),
    "Vehiclenum" VARCHAR,
    "Coupen" BOOL,
    "Saleperson" VARCHAR,
    "DVnumber" INT,
    "Ares" VARCHAR,
    "Ctitle" VARCHAR,
    "remarks" VARCHAR,
    "mEntry" BOOL,
    "Cust_Id" INT,
    FOREIGN KEY (Cust_Id) REFERENCES Customer_New(Cust_Id));

mssql的正确语法是什么?

2 个答案:

答案 0 :(得分:0)

TSQL:

IF NOT OBJECT_ID('dbo.TableToDrop') IS NULL
BEGIN
DROP TABLE dbo.TableToDrop
END;

//更改dbo。必要时到您的架构

答案 1 :(得分:0)

你应该把它写成:

--DROP TABLE IF EXISTS Admin;
--query to drop a table
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))
DROP TABLE [dbo].[TableName]
GO

CREATE TABLE [AdvancePayments] 
(
    [Id] INT IDENTITY(1,1) PRIMARY KEY NOT NULL ,--The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature
    [date] VARCHAR(10),-- best practice is to use datetime datatype to store date feilds.
    [Name] VARCHAR(100),
    [Amount] INT,
    [Remarks] VARCHAR(1000)
);

INSERT INTO AdvancePayments
VALUES (
    --1,-- you cannot give an explicit value to identity column untill IDENTITY_INSERT is ON. So comment this.
    '11/22/2013',
    'wqqw',
    21,
    'fgdsr');

CREATE TABLE [DVouchers] (
    [DVid] INT PRIMARY KEY NOT NULL,
    [vdate] VARCHAR(20) DEFAULT(NULL),
    [Vehiclenum] VARCHAR(100),
    [Coupen] BIT,-- Bit is the datatype used for Boolean values.
    [Saleperson] VARCHAR(100),
    [DVnumber] INT,
    [Ares] VARCHAR(100),
    [Ctitle] VARCHAR(100),
    [remarks] VARCHAR(100),
    [mEntry] BIT,
    [Cust_Id] INT,
    FOREIGN KEY (Cust_Id) REFERENCES Customer_New(Cust_Id)
    );

注意:在MS SQL中,varchar数据类型的默认长度为1.因此您必须为每个指定长度 varchar变量。