Can someone please say how to create an alias name for a table in a snowflake?
sample table:
CREATE OR REPLACE TABLE USER.EMPLOYEE(
EMPNO CHAR(6 ) COLLATE 'en_US-upper-rtrim' NOT NULL,
FIRSTNME VARCHAR(12 ) COLLATE 'en_US-upper-rtrim' NOT NULL,
MIDINIT CHAR(1 ) COLLATE 'en_US-upper-rtrim' NULL,
LASTNAME VARCHAR(15 ) COLLATE 'en_US-upper-rtrim' NOT NULL,
WORKDEPT CHAR(3 ) COLLATE 'en_US-upper-rtrim' NULL,
PHONENO CHAR(4 ) COLLATE 'en_US-upper-rtrim' NULL,
HIREDATE DATE NULL,
JOB CHAR(8 ) COLLATE 'en_US-upper-rtrim' NULL,
EDLEVEL SMALLINT NOT NULL,
SEX CHAR(1 ) COLLATE 'en_US-upper-rtrim' NULL,
BIRTHDATE DATE NULL,
SALARY NUMBER(9,2) NULL,
BONUS NUMBER(9,2) NULL,
COMM NUMBER(9,2) NULL
);
Alias:
CREATE ALIAS "USER"."EMP" FOR TABLE "USER"." EMPLOYEE"
If you are searching for SYNONYM/ALIAS, they are not supported by Snowflake.
CREATE SYNONYM EMP FOR USER.EMPLOYEE;
Reference: Migrating Oracle Database to Snowflake: Reference Manual - APPENDIX D
Depending on requirements VIEW could be used instead:
CREATE VIEW USER.EMP
AS
SELECT *
FROM USER.EMPLOYEE;
Related
I want to convert the following SQL server query into a Teradata BTEQ script. Could anyone help in this process..
The table creation logic is as follows and this needs to be converted to Teradata
CREATE TABLE [Eqp].[t_WSTCPEStairstep]
(
[SysID] [smallint] NULL,
[PrinID] [smallint] NULL,
[Account] [bigint] NOT NULL,
[Order_No] [bigint] NOT NULL,
[Order_Typ] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Eqp_Serial] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Eqp_Typ] [varchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Eqp_Model] [varchar] (9) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Disco_Dte] [date] NULL,
[Return_Dte] [date] NULL,
[Restart_Dte] [date] NULL,
[Lost_Dte] [date] NULL,
[TestFlag] [smallint] NULL
) ON [PRIMARY]
WITH
(
DATA_COMPRESSION = PAGE
)
GO
CREATE NONCLUSTERED INDEX [ix_WSTCPEStairstepDiscoDteIndex] ON [Eqp].[t_WSTCPEStairstep] ([Disco_Dte]) ON [PRIMARY]
GO
CREATE CLUSTERED INDEX [ix_WSTCPEStairstepSPAIndex] ON [Eqp].[t_WSTCPEStairstep] ([SysID], [Account]) WITH (DATA_COMPRESSION = PAGE) ON [PRIMARY]
GO
I'm not sure about some of the SQL Server specific bits (compression, clustered, primary), but this will give you a start:
CREATE TABLE Eqp.t_WSTCPEStairstep (
SysID SMALLINT,
PrinID SMALLINT,
Account BIGINT NOT NULL,
Order_No BIGINT NOT NULL,
Order_Typ VARCHAR(1),
Eqp_Serial VARCHAR(25),
Eqp_Typ VARCHAR(2),
Eqp_Model VARCHAR(9) ,
Disco_Dte DATE,
Return_Dte DATE,
Restart_Dte DATE,
Lost_Dte DATE,
TestFlag SNALLINT
)
PRIMARY INDEX(col1, col2, ...);
-- Indexes
CREATE INDEX ix_WSTCPEStairstepDiscoDteIndex (Disco_Dte) ON Eqp.t_WSTCPEStairstep;
CREATE INDEX ix_WSTCPEStairstepSPAIndex (SysID, Account) ON Eqp.t_WSTCPEStairstep;
Which column(s) do you use to access data in this table? If they provide even distribution (i.e. mostly distinct values), then specify these as your PRIMARY INDEX fields. And if these fields are unique, better yet - UNIQUE PRIMARY INDEX. Maybe it's one of the indexes you specified -- disco_dte or (SysID, Account).
Some more notes:
columns should be NULLABLE by default
if TestFlag is just 1/0, you can use the BYTEINT data type
you may want to convert the VARCHAR(1) and VARCHAR(2) to CHAR
for compression, you can add that at multiple levels, but most common I think is at the column level
I'm reverse engineering a script that creates a payroll database from my textbook. I'm rebuilding it into a library management system instead. To start I've drawn out a diagram of the tables/columns and their relationships here. Now following the script from the textbook I use the authors technique to create all the tables except for the BooksIssued table and InvoiceItems table. This is my code
USE master
GO
/******Check to see if database exists******/
IF DB_ID('SET2133810') IS NOT NULL
DROP DATABASE SET2133810
GO
/******Object: Database SET2133810******/
CREATE DATABASE SET2133810
GO
USE SET2133810
GO
/******Object: table*****/
CREATE TABLE VendorsList(
VendorID int IDENTITY(1,1) NOT NULL,
VendorName varchar(50) NOT NULL,
VendorPhone varchar(50) NULL,
VendorContactLName varchar(50) NULL,
VendorContactFName varchar(50) NULL,
VendorAddress varchar(50) NULL,
VendorCity varchar(50) NOT NULL,
VendorState char(2) NOT NULL,
VendorZipCode varchar(20) NOT NULL,
CONSTRAINT PK_VendorsList PRIMARY KEY CLUSTERED (
VendorID ASC
)
)
GO
/******Object: Table*****/
CREATE TABLE InvoicesList(
InvoiceID int IDENTITY(1,1) NOT NULL,
VendorID int NOT NULL,
InvoiceNumber varchar(50) NOT NULL,
InvoiceDate smalldatetime NOT NULL,
InvoiceTotal money NOT NULL,
PaymentTotal money NOT NULL,
PaymentDate smalldatetime NULL,
CONSTRAINT PK_InvoicesList PRIMARY KEY CLUSTERED(
InvoiceID ASC
)
)
GO
/******Object: Table*****/
CREATE TABLE BookList(
BookID int IDENTITY(1,1) NOT NULL,
BookISBN varchar(50) NOT NULL,
BookTitle varchar(50) NOT NULL,
BookAuthor varchar(50) NOT NULL,
BookPublisher varchar(50) NOT NULL,
BookGenre varchar(50) NULL
CONSTRAINT PK_BookList PRIMARY KEY CLUSTERED(
BookID ASC
)
)
GO
/******Object: Table*****/
CREATE TABLE MembersList(
MemberID int IDENTITY(1,1) NOT NULL,
MemberLName varchar(50) NOT NULL,
MemberFName varchar(50) NOT NULL,
MemberAddress varchar(50) NULL,
MemberCity varchar(50) NOT NULL,
MemberState char(2) NOT NULL,
MemberZipCode varchar(20) NOT NULL,
MemberPhone varchar(50) NOT NULL,
MemberEmail varchar(50) NOT NULL,
CONSTRAINT PK_MembersList PRIMARY KEY CLUSTERED(
MemberID ASC
)
)
GO
I'm confused on how I would create these tables in the same way? Since they both depend on the other tables and the information thats already in them. I've looked around and found Join statements, but none of them were used in a way that would help me with this problem.
Following should be the order to create tables.
BookList
MemeberList
Book Issued
VendorList
InvoiceList
InvoiceItems
Invoice list shoul be created in last as it have references to vendor and invoice items and BookIssues should be created after MemberList and Book List
This is my task list:
Design a star or snowflake schema for a data warehouse that will help answer various business questions regarding sales.
Create a database in SQL Server (this is the data warehouse server) based on the star/snowflake schema you designed.
Load data from the source system i.e. the OLTP database shown earlier to the star/snowflake schema database.
The data from most of the tables in the OLTP database have been prepared in a form of SQL statements. Unfortunately, due to company's policy, you are not allowed to load data from the Products and Customer table 'directly'.
The Customer data has been prepared in CSV format
The Product data can only be retrieved from cloud database in JSON format http://bi.edisonsiow.com/ay1516s2/ca1/getProducts.php
You are required to find the most efficient way, which might include coding, to load data from the sources to your SQL Server.
So far i have done this :
CREATE TABLE Customers (
customerNumber Int NOT NULL,
customerName Varchar(50) NOT NULL,
contactLastName Varchar(50) NOT NULL,
contactFirstName Varchar(50) NOT NULL,
phone Varchar (50) NOT NULL,
addressLine1 Varchar (50) NOT NULL,
addressLine2 Varchar (50) NOT NULL,
city Varchar (50) NOT NULL,
state Varchar (50) NULL,
postalCode Varchar (15) NOT NULL,
country Varchar(50) NOT NULL,
salesRepEmployeeNumber Int NOT NULL,
creditLimit Double NOT NULL,
PRIMARY KEY (customerNumber));
CREATE TABLE Offices (
officeCode Varchar(10) NOT NULL,
city Varchar(50) NOT NULL,
phone Varchar(50) NOT NULL,
addressLine1 Varchar(50) NOT NULL,
addressLine2 Varchar(50) NOT NULL,
state Varchar(50) NULL,
country Varchar(50) NOT NULL,
postalCode Varchar(15) NOT NULL,
territory Varchar(10) NOT NULL,
PRIMARY KEY (officeCode));
CREATE TABLE payments (
customerNumber Int NOT NULL,
checkNumber Varchar(50) NOT NULL,
paymentDate Datetime NOT NULL,
amount DOUBLE NOT NULL,
PRIMARY KEY (customerNumber, checkNumber));
CREATE TABLE OrderDetails (
orderNumber Int NOT NULL,
productCode Varchar(15) NOT NULL,
quantityOrdered Int NOT NULL,
priceEach DOUBLE NOT NULL,
orderLineNumber SMALLINT NOT NULL,
PRIMARY KEY (orderNumber, productCode));
CREATE TABLE ProductLines (
productLine Varchar (50) NOT NULL,
textDescription TEXT NOT NULL,
htmlDescription TEXT NOT NULL,
image BLOB NOT NULL,
PRIMARY KEY (productLine));
CREATE TABLE Orders (
orderNumber Int NOT NULL,
orderDate DateTime NOT NULL,
requiredDate DateTime NOT NULL,
shippedDate DateTime NOT NULL,
status Varchar(15) Not null,
comments TEXT NOT NULL,
customerNumber INT NOT NULL,
Primary key(orderNumber));
ALTER TABLE Orders ADD FOREIGN KEY(customerNumber) REFERENCES Customer(customerNumber);
CREATE TABLE Employees (
employeeNumber Int Not null,
lastName Varchar(50) Not null,
firstName Varchar(50) Not null,
extension Varchar(10) NOt null,
email Varchar(100) Not null,
officeCode Varchar(10) Not Null,
reportsTo Int Not null,
jobTitle Varchar(50) Not null,
Primary key(employeeNumber));
CREATE TABLE Products (
productCode Varchar(15) Not Null,
productName Varchar(70) Not Null,
productLine Varchar(50) Not null,
productScale Varchar(10) Not null,
productVendor Varchar(50) Not null,
productDescription TEXT Not null,
quantityinStock Int Not null,
Buy Price Double Not null,
MSRP Double Not null,
Primary key (productCode))
9:28PM
CREATE TABLE payments (
customerNumber Int NOT NULL,
checkNumber Varchar(50) NOT NULL,
paymentDate Datetime NOT NULL,
amount Float NOT NULL,
PRIMARY KEY (customerNumber, checkNumber));
CREATE TABLE Customers (
customerNumber Int NOT NULL,
customerName Varchar(50) NOT NULL,
contactLastName Varchar(50) NOT NULL,
contactFirstName Varchar(50) NOT NULL,
phone Varchar (50) NOT NULL,
addressLine1 Varchar (50) NOT NULL,
addressLine2 Varchar (50) NOT NULL,
city Varchar (50) NOT NULL,
state Varchar (50) NULL,
postalCode Varchar (15) NOT NULL,
country Varchar(50) NOT NULL,
salesRepEmployeeNumber Int NOT NULL,
creditLimit Float NOT NULL,
PRIMARY KEY (customerNumber));
ALTER TABLE Payments ADD FOREIGN KEY(customerNumber) REFERENCES Customers(customerNumber); -- here
CREATE TABLE Offices (
officeCode Varchar(10) NOT NULL,
city Varchar(50) NOT NULL,
phone Varchar(50) NOT NULL,
addressLine1 Varchar(50) NOT NULL,
addressLine2 Varchar(50) NOT NULL,
state Varchar(50) NULL,
country Varchar(50) NOT NULL,
postalCode Varchar(15) NOT NULL,
territory Varchar(10) NOT NULL,
PRIMARY KEY (officeCode));
CREATE TABLE OrderDetails (
orderNumber Int NOT NULL,
productCode Varchar(15) NOT NULL,
quantityOrdered Int NOT NULL,
priceEach Float NOT NULL,
orderLineNumber SMALLINT NOT NULL,
PRIMARY KEY (orderNumber, productCode));
CREATE TABLE ProductLines (
productLine Varchar (50) NOT NULL,
textDescription TEXT NOT NULL,
htmlDescription TEXT NOT NULL,
image Float NOT NULL,
PRIMARY KEY (productLine));
CREATE TABLE Orders (
orderNumber Int NOT NULL,
orderDate DateTime NOT NULL,
requiredDate DateTime NOT NULL,
shippedDate DateTime NOT NULL,
status Varchar(15) Not null,
comments TEXT NOT NULL,
customerNumber INT NOT NULL,
Primary key(orderNumber));
ALTER TABLE OrderDetails ADD FOREIGN KEY(orderNumber) REFERENCES Orders(orderNumber); -- here
CREATE TABLE Employees (
employeeNumber Int Not null,
lastName Varchar(50) Not null,
firstName Varchar(50) Not null,
extension Varchar(10) NOt null,
email Varchar(100) Not null,
officeCode Varchar(10) Not Null,
reportsTo Int Not null,
jobTitle Varchar(50) Not null,
Primary key(employeeNumber));
CREATE TABLE Products (
productCode Varchar(15) Not Null,
productName Varchar(70) Not Null,
productLine Varchar(50) Not null,
productScale Varchar(10) Not null,
productVendor Varchar(50) Not null,
productDescription TEXT Not null,
quantityinStock Int Not null,
BuyPrice Float Not null,
MSRP Float Not null,
Primary key (productCode))
ALTER TABLE OrderDetails ADD FOREIGN KEY(productCode) REFERENCES Products(productCode);
I dont know how to load from source system?
In SQL Server Management Studio, exp[and the server-name, and under databases, find your database.
Right-click it, select Tasks, and then Import Data.
Use the import/export wizard to assist you in importing data from your source data.
Need help here. I have 2 questions based on this query.
SELECT cdd.FieldID,cdd.PlanTypeID,pt.IsFinancial
FROM ClientDataDictionary cdd
INNER JOIN #tblPlanTypes pt ON cdd.PlanTypeID = pt.PlanTypeID
INNER JOIN ClientDataDictionaryFieldType cddftype
ON cdd.FieldId = cddftype.FieldID AND cdd.PlanTypeId = cddftype.PlanTypeID
WHERE cdd.ClientId = #ClientID AND cdd.IsHidden = 0
AND cddftype.FieldTypeID = 4
AND cddftype.FieldID = cddftype.ParentFieldIDSectionTitle
AND (SELECT COUNT(cddftype2.FieldID)
FROM ClientDataDictionaryFieldType cddftype2
Where cddftype2.ParentFieldIDSectionTitle = cdd.FieldID
AND cddftype2.PlanTypeID = cdd.PlanTypeID
AND EXISTS(Select tbl.FieldID From #tblSelectedFields tbl
Where tbl.FieldID = cddftype2.FieldID AND tbl.PlantypeID =cdd.PlanTypeID COLLATE SQL_Latin1_General_CP1_CI_AS)
) != 0 COLLATE SQL_Latin1_General_CP1_CI_AS
Where do I place my COLLATE keyword in this query? I know that temp tables have different collate "type".
I tried using COLLATE DATABASE_DEFAULT however, the error still occurs. Will this resolve the issue?
Please help me here. Thanks!
[UPDATE 1]
ClientID is set to UNIQUEIDENTIFIER data type.
[UPDATE 2]
here are the table definitions
#tblPlanTypes
PlanTypeId nvarchar(50),
PlanType nvarchar(max),
Prefix NVARCHAR(10),
IsFinancial BIT,
TotalCount BIGINT,
PlanCount BIGINT
ClientDataDictonary
[ClientDataDictionaryId] [uniqueidentifier] NOT NULL ROWGUIDCOL CONSTRAINT [DF_ClientDataDictionary_ClientDataDictionaryId] DEFAULT (newid()),
[ClientId] [uniqueidentifier] NOT NULL,
[PlanTypeId] [uniqueidentifier] NULL,
[FieldId] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[FieldText] [nvarchar] (max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[FieldType] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DefaultValue] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[HelpText] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[IsHidden] [bit] NULL CONSTRAINT [DF_ClientDataDictionary_IsHidden] DEFAULT ((0)),
[IsSummable] [bit] NULL,
[IsRequired] [bit] NULL,
[IsContractRenewal] [bit] NULL,
[HasAlert] [bit] NULL CONSTRAINT [DF_ClientDataDictionary_HasAlert] DEFAULT ((0)),
[AlertMessage] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CentralLanguageId] [uniqueidentifier] NOT NULL,
[LookupTypeName] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[RowVersion] [timestamp] NULL,
[LastUpdated] [datetime] NULL CONSTRAINT [DF_ClientDataDictionary_LastUpdated] DEFAULT (getutcdate()),
[Sequence] [int] NULL
ClientDataDictionaryFieldType
[ClientDataDictionaryFieldTypeID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_ClientDataDictionaryFieldType_ClientDataDictionaryFieldType] DEFAULT (newid()),
[PlanTypeID] [uniqueidentifier] NULL,
[FieldID] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[FieldTypeID] [int] NULL,
[ParentFieldIDSectionTitle] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
#tblSelectedFields
FieldID nvarchar(255),
PlanTypeID UniqueIdentifier,
FieldText nvarchar(MAX),
sequence int,
FieldType nvarchar(100),
IsFinancial bit
[UPDATE 3]
Tried the suggestion by #Mack, but a new error emerged. Expression type uniqueidentifier is invalid for COLLATE clause. This is caused by the #ClientID having a data type of uniqueidentifier. Any suggestions?
First of all - if you include the descriptions of the tables in your example people here will be able to help you a lot quicker.
Second to debug this query yourself break it into logical parts(separate any subqueries, remove tables one at a time), then remove the where clauses one by from each logical part until your query works, this will help you identify the cause of the problem, remember that the join clauses could also be the problem.
Finally, the code below will allow you to determine the collation type for each column you are querying, it's from this article by Pinal Dave
USE yourdb
GO
SELECT name, collation_name
FROM sys.columns
WHERE OBJECT_ID IN ( SELECT OBJECT_ID
FROM sys.objects
WHERE type = 'U'
AND name = 'yourtable')
AND name IN('yourcolumn1','yourcolumn2',.....,'yourcolumnX')
Thanks for the table definitions...
At first glance, your collate issue lies with this field
PlanTypeId nvarchar(50)
used in this join: INNER JOIN #tblPlanTypes pt ON cdd.PlanTypeID = pt.PlanTypeID
if you apply the COLLATE to the join you should resolve your problem.
You need to place COLLATE keywords after textual (char, nchar, varchar, nvarchar) field names in comparison/join operators - 'alias1.txtfield1 COLLATE something = alias2.txtfield2 COLLATE something'. There's no point in using COLLATE with integer fields (I hope your IDs are of integer variety?).
i have two tables:
1.
CREATE TABLE [dbo].[HotelSourceMap](
[hotelsourcemapid] [bigint] IDENTITY(1,1) NOT NULL,
[dspartnerid] [bigint] NOT NULL,
[dshotelid] [bigint] NOT NULL,
[countrycode] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[countryid] [bigint] NULL)
2.
CREATE TABLE [dbo].[country](
[countryId] [smallint] IDENTITY(1,1) NOT NULL,
[countryName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[countryCode] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL)
situation is like this:
i want to update countryid in HotelSourceMap table from country.countryid .
where hotelsourcemap.countrycode = country.countrycode
some thing like this:
UPDATE HotelSourceMap
SET
HotelSourceMap.countryid =country.countryId
WHERE
HotelSourceMap.countrycode = country.countryCode
There's probably a set-based solution for this, in which case that would be preferable, and I'm sure someone will post it, but until then, at least this will do the job:
UPDATE
HotelSourceMap
SET
countryid = (SELECT countryId FROM country WHERE country.countryCode = HotelSourceMap.countrycode)
This is really questionable table design, but here is the SQL:
UPDATE HotelSourceMap
SET countryid = co.countryId
FROM HotelSourceMap AS hsm
JOIN country AS co
ON (hsm.countryCode = co.countryCode)