How i can change and run sql file into mssql - sql

I have my database in sqlite .I exported sql file from sqlite manager. Now on executing that file into Microsoft SQL Server 2012 it is giving error on following lines.
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));
What is correct syntax for mssql?

You should write it as:
--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)
);
Note: In MS SQL default length of varchar datatype is 1. so you will have to specify length to each
varchar variable.

TSQL:
IF NOT OBJECT_ID('dbo.TableToDrop') IS NULL
BEGIN
DROP TABLE dbo.TableToDrop
END;
// change dbo. to your schema if necessary

Related

Create table with UNIQUEIDENTIFIER as PK

I have been trying (unsuccessfully) to create a table with a UNIQUEIDENTIFIER as the primary key. The script below will create the table just fine, however there is no PK specified. This is my first time using a UNIQUEIDENTIFIER, however. Any ideas?
T-SQL
CREATE TABLE [dbo].[AgentRelationshipCodes]
(
Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
RelationshipId char(3) NULL,
EffectiveDate datetime NULL,
LastChangeDate datetime NOT NULL,
LastChangeId char(6) NOT NULL,
AgtTableId int FOREIGN KEY REFERENCES AgentTransmission(ID)
);
Management Studio
create table [dbo].[AgentRelationshipCodes]
(
Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT NEWID(),
.
.
.
)
Repeat for the other tables.
Answered here by Christian Hayter

Why isn't my Auto Increment working in SQL Server 2008?

This is my table creation:
create table Movie
(
MovieID int NOT NULL IDENTITY (1,1) PRIMARY KEY,
MovieName varchar(40) NOT NULL UNIQUE,
CurrentStock int NOT NULL,
GenreID int NOT NULL,
RatingID int NOT NULL,
Max_Inventory int NOT NULL,
Platforms char(5) NOT NULL,
Discontinued bit,
DiscontinuedDate Date
);
create table Inventory
(
InventoryID int NOT NULL IDENTITY (1,1) PRIMARY KEY,
MovieID int Not NULL,
CurrentStock int NOT NULL,
Max_Inventory int NOT NULL
);
Stored procedures:
ALTER PROCEDURE [dbo].[InsInventory]
(#CurrentStock int,
#ChangeStock int)
as
begin
insert into Inventory(Max_Inventory, CurrentStock)
Values (#ChangeStock, #CurrentStock)
select SCOPE_IDENTITY();
END
ALTER PROCEDURE [dbo].[InsMovie]
(#GenreID int,
#RatingID int,
#Platform varchar(5),
#MovieName varchar(40)
)
AS
BEGIN
Insert into Movie (RatingID, MovieName, Platforms, GenreID)
Values (#RatingID, #MovieName, #Platform, #GenreID)
select SCOPE_IDENTITY();
SET NOCOUNT ON;
END
Foreign key:
ALTER Table Inventory
ADD CONSTRAINT fk_Inventory_Movie
FOREIGN KEY (MovieID) REFERENCES Movie(MovieID)
ALTER TABLE Movie
ADD CONSTRAINT fk_Movie_RatingLookUp
FOREIGN KEY (RatingID) REFERENCES RatingLookUp(RatingID)
ALTER TABLE Movie
ADD CONSTRAINT fk_Movie_GenreLookUp
FOREIGN KEY (GenreID) REFERENCES GenreLookUp (GenreID)
When I run my code I keep getting the error in Visual Studio
MovieID cannot be null
but it should be when I insert a row. I also made sure to manually check to see if SQL Server had the IsIdentity set, which it is. So please help a confused programmer out.
You shouldn't insert NULL as a primary key to make it generate a value, you should just don't insert that value at all by not listing it among the fields to insert. As a sample;
INSERT INTO Movie (moviename, currentstock, genreid, ratingid, max_inventory, platforms)
VALUES ('name1', 1, 1, 1, 1, '1');
A simple working sample with your exact table creation.
Below statement is your problem. your table def shows MovieID not null. but in below statement you didn't provide that.
insert into Inventory(Max_Inventory, CurrentStock)
Values (#ChangeStock, #CurrentStock)
here is what you need to do.. first you need to retrieve Movie id which will be return from executing InsMovie sproc. than you need to use that Movie Id as a prameter to InsInvetory sproc call.
according to your table def movie id is required. also for Movie table you missed two column values that i have added which are required as well.
Please see blow updated stored procedures.
ALTER PROCEDURE [dbo].[InsInventory]
(
#CurrentStock int,
#ChangeStock int,
#MovieID int
)
as
begin
insert into Inventory(MovieID, Max_Inventory,CurrentStock)
Values (#MovieID, #ChangeStock, #CurrentStock)
select SCOPE_IDENTITY();
END
ALTER PROCEDURE [dbo].[InsMovie]
(
#GenreID int,
#RatingID int,
#Platform varchar(5),
#MovieName varchar(40),
#CurrentStock int,
#max_inventory int
)
AS
BEGIN
Insert into Movie (CurrentStock, max_inventory, RatingID, MovieName, Platforms,GenreID)
Values(#CurrentStock, #max_inventory, #RatingID,#MovieName, #Platform, #GenreID)
select SCOPE_IDENTITY();
SET NOCOUNT ON;
END

How to change data type of columns without dropping it

CREATE TABLE DEPARTMENT(
DEPARTMENT_NAME VARCHAR(25) NOT NULL PRIMARY KEY ,
BUDGET_CODE INT NOT NULL,
OFFICE_NUM INT NOT NULL,
PHONE NUMERIC NOT NULL ,
);
I have this table that I am creating but i want to change budget code and office number in varchar instead of int. Is there a way to do this. this table is linked to other tables with foreign key. So i can't drop it and create a new one.
Try this
ALTER TABLE dbo.DEPARTMENT
ALTER COLUMN BUDGET_CODE VARCHAR(100)
ALTER COLUMN OFFICE_NUM VARCHAR(100)

SQL Server : create error how to write database name with the table name

CREATE DATABASE agom COLLATE Arabic_CI_AS
CREATE TABLE Branches
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Brands
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Work_Order
(
NUMBER INT NOT NULL,
BRANCHID INT NOT NULL,
BRANDID INT NOT NULL,
WDATE DATE NOT NULL,
REPAIRSTATUS VARCHAR(255),
REPAIRCOST VARCHAR(255),
REMARK VARCHAR(500),
PRIMARY KEY (NUMBER,BRANCHID,BRANDID)
)
CREATE TABLE agom.Profiles
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
USERNAME VARCHAR(25) NOT NULL,
PASS VARCHAR(25) NOT NULL
)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT branchfk
FOREIGN KEY (BRANCHID) REFERENCES Branches(ID)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT brandfk
FOREIGN KEY (BRANDID) REFERENCES Brands(ID)
I get an error that cannot create table
I try to write database name with the table name db.tablename but it's not working
I need to create the database then create the tables and its constraints but I don't know where is the error.I am a sql noob
It's never Database.Table.
It's either Table, or Schema.Table, or Database.Schema.Table, or Server.Database.Schema.Table.
You probably just want to insert USE agom right after create database, and then only refer to tables by name.
Alternatively, you can refer to your tables as agom.dbo.Work_Order, dbo being the default database schema.
See Using Identifiers As Object Names for general reference.

MS Access Create Table with Autoincrement and default date

I try to create MS Access Table with autoincrement ID and Default Date field, but next query always says "Syntax error in CREATE TABLE statement.":
CREATE TABLE Table1
(
[ID] AUTOINCREMENT,
[Email] TEXT(255),
[ProductID] NUMBER,
[DateCreate] DATETIME,
[DateSend] DATETIME
);
ALTER TABLE Table1
ALTER [DateSend] DATETIME DEFAULT NOW() NOT NULL;
Who can help me to fix that query. Thanks!
There are many NUMBER types in Ms-Access, so you have to be specific. I guess you want Integer.
CREATE TABLE Table1
(
[ID] AUTOINCREMENT,
[Email] TEXT(255),
[ProductID] INTEGER,
[DateCreate] DATETIME,
[DateSend] DATETIME
);
The ALTER TABLE syntax requires ALTER COLUMN :
ALTER TABLE Table1
ALTER COLUMN
[DateSend] DATETIME DEFAULT NOW() NOT NULL;
You could also have those two in one statement:
CREATE TABLE Table1
(
[ID] AUTOINCREMENT,
[Email] TEXT(255),
[ProductID] INTEGER,
[DateCreate] DATETIME,
[DateSend] DATETIME DEFAULT NOW() NOT NULL
);
It's best practise to have a PRIMARY KEY on every table, and you probably intended that for the ID:
[ID] AUTOINCREMENT PRIMARY KEY,
A page with a lot of useful information about how to handle Access with SQL:
Intermediate Microsoft Jet SQL for Access 2000
CREATE TABLE Tblcontact
(
contactid AUTOINCREMENT PRIMARY KEY ,
firstname CHAR (60),
lastname CHAR (60),
email VARCHAR (75)
);