How would I create an index on this temp table? - sql

I am trying to speed up a query and I think I am confused about indexes. How, and what index would I add to this table. The ID is Unique, would this be a primary index?
CREATE TABLE #OSP
(
[Id] UniqueIdentifier,
[YearMonth] int,
[Expenditure] decimal (7,2),
[Permit] decimal (7,2)
);

You can specify the primary key in your create table statement.
CREATE TABLE #OSP
(
[Id] UniqueIdentifier primary key,
[YearMonth] int,
[Expenditure] decimal (7,2),
[Permit] decimal (7,2)
);

If you're joining on id then creating an index on that would help.
I think this would work:
CREATE TABLE #OSP
(
[Id] UniqueIdentifier,
[YearMonth] int,
[Expenditure] decimal (7,2),
[Permit] decimal (7,2)
);
CREATE UNIQUE CLUSTERED INDEX [idx_id] ON #Osp ([Id] ASC)

Related

Create an auto incrementing alpha numeric primary key in SQL Server Management Studio

I have a Student table in SQL Server database which is as follows:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I want the Id property to be alpha-numeric and auto-increment itself for a new entry. I want Id to be S<number> and then S<number+1> and so on.
I tried to solve this problem as a two-step process:
(i) I first tried to make the Id an auto-incrementing property by doing this:
Then I pressed "Update":
And then I updated again and it led me to this table:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I do not think Id is an auto-incrementing value yet. How can I make it both auto-incrementing and alpha-numeric from the following interface:
It seems that you don't really want a fully auto-incrementing alphanumeric column A001,A002...B001, you just want a regular integer column with a prefix of S. For this you can use a simple computed column
ALTER TABLE Student
ADD MyId AS CONCAT('S', Id);

How can I convert data contained if yes do somethings or no don't do anything to table in SQL Server

I have data I want to looking to design table as in the picture
Create a table with the following design
CREATE TABLE dbo.MyData
(
SeqNo INT IDENTITY(1,1) CONSTRAINT Pk_MyDate_SeqNo PRIMARY KEY,
Diseases VARCHAR(255),
YesNo BIT CONSTRAINT Df_MyData_YesNo DEFAULT(0),
Duration INT,
Treatement VARCHAR(255)
)
Insert the value 1 To the Column YesNo for value Yes for No insert Value 0

copy a column with identical function to another table

I want to copy a column generated by identity function, identity(1,1), to another table. However, after the process, the whole new column only shows 1.
Is there a way to fix that? thank you
CREATE TABLE Dim_Route(RtID INT Identity(1,1) Primary Key, Itinerary varchar(50) NOT NULL, )
CREATE TABLE Dim_FlightSchedule(FSID INT Identity(1,1) Primary Key, RTID INT, Constraint fk_Route Foreign KEY (RTID) REFERENCES Dim_Route(RtID), )
insert Dim_Route (Itinerary)
values
('B'),
('C'),
('D'),
('E'),
('F')
insert Dim_FlightSchedule (RTID)
select RTID from Dim_Route
Here is my code:
The original table:CREATE TABLE Dim_Route(RtID INT Identity(1,1) Primary Key,
Itinerary varchar(50) NOT NULL,
)
The destination table:CREATE TABLE Dim_FlightSchedule(FSID INT Identity(1,1) Primary Key,
RTID INT,
Constraint fk_Route Foreign KEY (RTID) REFERENCES
Dim_Route(RtID),
)
UPDATE Dim_FlightSchedule
SET Dim_FlightSchedule.RTID = Dim_Route.RTID
FROM Dim_Route
The RTID in destination table are all 1.
I don't know where I got wrong. Thank you

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

How i can change and run sql file into mssql

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