Sara Moradi
Sara Moradi

Reputation: 33

Insert default value in view without creating table in SQL

I have to create a view in SQL that has a set of default values without creating any tables, and I don't know if it is possible or not? Maybe something like this:

CREATE VIEW PersonalView 
AS 
    SELECT * 
    FROM (VALUES('Sara', 'Moradi', '22'))

Upvotes: 0

Views: 811

Answers (3)

Martin Smith
Martin Smith

Reputation: 453057

Another variant you can use is

CREATE VIEW PersonalView (first_name, last_name, age)
AS
  SELECT 'Sara', 'Moradi', 22

Upvotes: 0

Lukasz Szozda
Lukasz Szozda

Reputation: 175606

It is almost correct. You need to add aliases:

CREATE VIEW PersonalView 
AS 
SELECT * FROM ( VALUES ('Sara', 'Moradi', '22')) s(fist_name, last_name, age);

db<>fiddle demo

Upvotes: 2

Mureinik
Mureinik

Reputation: 311163

In SQL-Server you can write a query without a from clause, but note you'll need to give the literals column names. E.g.:

CREATE VIEW PersonalView AS SELECT 'Sara' AS name, 'Moradi' AS last_name, '22' AS age

Upvotes: 1

Related Questions