Create Table View using alises in column names - database

I am trying to create a view in DB2 with specific column names
so that I can compare them with another table with same column names.
My question is why this doesn't work, how to make View with column names as alises?
So, basically my idea is to have a table with different column names.
CREATE VIEW testView (
ADD_ID AS address_id,
ADD_CODE AS address_code)
AS SELECT * FROM SOP.CUST_ADDRESS;

You would code your view either like this
CREATE VIEW testView
AS SELECT
ADD_ID AS address_id
, ADD_CODE AS address_code
FROM SOP.CUST_ADDRESS;
or like this
CREATE VIEW testView (address_id, address_code )
AS SELECT
ADD_ID
, ADD_CODE
FROM SOP.CUST_ADDRESS;

Related

column_list in views are displayed as manadory

In recent day, <column_list> is added automatically to views, eventhough the parameter is documented as optional: https://docs.snowflake.com/en/sql-reference/sql/create-view.html .
This causes views that are defined as
create view my_view
as
select *
from tbl
to throw an error, if a new field is added to a table, unless the view is refreshed.
Is there a way to define <column_list> as optional?
This behavior is by designand it is decribed at CREATE VIEW - Usage Notes:
View definitions are not dynamic. A view is not automatically updated if the underlying sources are modified such that they no longer match the view definition, particularly when columns are dropped
To reporduce the case:
CREATE OR REPLACE TABLE t AS SELECT 1 AS c;
CREATE VIEW v_t AS SELECT * FROM t;
SELECT * FROM v_t;
ALTER TABLE t ADD COLUMN d INT;
SELECT * FROM v_t;
-- SQL compilation error:
-- View definition for 'PUBLIC.V_T' declared 1 column(s),
-- but view query produces 2 column(s).

Add a new column to a view in SQL

This is my view
CREATE VIEW seat_availability AS
SELECT flightid,flightdate, maxcapacity,
FROM flight
And I want to add 2 new columns named 'bookedseats' and 'availableseats' which don't exist in any tables but are columns I need to add.
I've done my research online and some say you can alter views by using:
ALTER VIEW
And some have said that you can't do that and have to edit from the view you've just created.
I've tried this:
CREATE VIEW seat_availability AS
SELECT flightid,flightdate, maxcapacity, bookedseats varchar(10), availableseats varchar(10)
FROM flight
which gave this error:
ERROR: syntax error at or near "varchar" LINE 2: ...ECT
flightid,flightdate, maxcapacity, bookedseats varchar(10...
I've also tried ALTER VIEW:
ALTER VIEW seat_availability AS
SELECT flightid,flightdate, maxcapacity, bookedseats varchar(10), availableseats varchar(10)
FROM flight
And I got this error:
ERROR: syntax error at or near "AS" LINE 1: ALTER VIEW
seat_availability AS
It would be easy to add columns if they existed in other tables but because I need to add 2 columns that don't exist in any table, it's proving difficult to do. If someone could help it would be very appreciated. Thank you.
Perhaps I may need to drop the view? and start again with two new columns added but how do I add them since they don't exist in any table in my database??
You don't define the datatype of a column like that. You let the view use the underlying datatype like this.
ALTER VIEW seat_availability AS
SELECT flightid
, flightdate
, maxcapacity
, bookedseats
, availableseats
FROM flight
Or if you need to explicitly change the datatype you need to use CONVERT like this.
ALTER VIEW seat_availability AS
SELECT flightid
, flightdate
, maxcapacity
, bookedseats = convert(varchar(10), bookedseats)
, availableseats = convert(varchar(10), availableseats)
FROM flight
Try this to add fake columns to the view:
ALTER VIEW [dbo].[view_seat_availability]
AS
SELECT flightid
,flightdate
,maxcapacity
,CAST(NULL AS VARCHAR(10)) AS 'bookedseats'
,CAST(NULL AS VARCHAR(10)) AS 'availableseats'
FROM flight

SQL Server: view or function contains a self-reference. Views or functions cannot reference themselves directly or indirectly

I'm trying to change the date format of a column stored in a view by doing an ALTER VIEW statement, but it's not working and I'm not sure why.
My query:
ALTER VIEW v_cust_invoices
AS
SELECT
FORMAT(INV_DATE,'MM-dd-yy') as INV_DATE
FROM
v_cust_invoices
I always get the error
View or function 'v_cust_invoices' contains a self-reference. Views or functions cannot reference themselves directly or indirectly
I'm trying to change the date format of INV_DATE to mm-dd-yy (it's currently yy-mm-dd). Can someone help me? Not sure what I'm doing wrong.
Edit: If I do
ALTER VIEW v_cust_invoices
AS
SELECT FORMAT(INV_DATE,'MM-dd-yy') as INV_DATE
FROM INVOICE
instead, it deletes all of the columns except for INV_DATE.
View definition:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_cust_invoices] AS
SELECT
CUSTOMER.CUST_NUM, CUST_LNAME,
CUST_BALANCE, INV_NUM, INV_DATE, INV_AMOUNT
FROM
CUSTOMER, INVOICE
GO
You must change the query so that it does not reference itself.
Look at the current definition of the v_cust_invoices view, at its FROM clause. You will see a reference to a table or to some other view. replace the last line in your query with the last line below, substituting the name of the table (or other view) this view gets it's data from.
ALTER VIEW v_cust_invoices
AS
SELECT FORMAT(INV_DATE,'MM-dd-yy') as INV_DATE,
[Plus all the rest of the output columns
from CURRENT definition of v_cust_invoices]
FROM [here Put the table or other view that is in current view definition]
or, now that op has posted complete view definition:
ALTER VIEW [dbo].[v_cust_invoices] AS
SELECT FORMAT(INV_DATE,'MM-dd-yy') INV_DATE,
CUSTOMER.CUST_NUM, CUST_LNAME,
CUST_BALANCE, INV_NUM, INV_DTE, INV_AMOUNT
FROM CUSTOMER, INVOICE
GO

SQL server : Inserting/updating missing data from one table to another

I have two table "Container" and "Control". These are existing tables and there is no foreign key relationship between the two. These are also very old tables so are not normalized. And I cannot change the structure now.
Below is the structure of the two tables.
Container table :
Control Table :
The Name field in Control table contains CTableName+CPName from Container table.
I want to update the columnName field of Control table with the value of CID column of Container table. and also want to insert one more record (for ctable2 i.e the fourth row in final Control table below) in Control table.
The tablename and columnname columns have will always be have default values.
The final Control table should look like this:
How do I do this?
I hope you want to apply this fix because you want normalize your table structure.
Try this:
First step:
In this way you'll UPDATE all Control rows with the value of Container table where the couple fields CTableName and CPName are the same of Name (excluding the rows of Container with the same couple fields)
UPDATE Control
SET ColumnValue = (
SELECT c.CID
FROM Container c
WHERE c.CTableName + '+' + c.CPName = Control.Name
AND NOT EXISTS(
SELECT 'PREVIOUS'
FROM Container c2
WHERE c.CTableName = c2.CTableName
AND c.CPName = c2.CPName
AND c.CID < c2.CID
)
),
TableName = 'default', ColumnName = 'default'
WHERE ColumnValue IS NULL
Second step:
Adding elements don't present in Control table
INSERT INTO Control (field list)
SELECT field list
FROM Container co
WHERE NOT EXISTS(
SELECT 'in_control'
FROM Control ct
WHERE co.CID = ct.ColumnValue
)
After these two steps you can drop column Name in Control table
I am an Oracle plsql programmer and worked with Sql-server as well.
First you should describe the relationship between the 2 tables, in the end i could figger it out but it's better you explain it yourself.
To update a table with information from another table you should ask yourself:
- when should the update take place?
- what are the conditions to start the update?
- how should the update be done?
In Oracle there is a database object called a trigger. It's quite a handy object and probably just what you need. I believe that sql-server has it too.
Pls fee free to ask any questions but do read the sql-server appropriate manual as well.
Good luck, Edward.

T-SQL: Alter View

I have a view with a field for date as a varchar. I need to change it to date time using the following code
CONVERT(DATETIME, MyDates)
This works fine when executing the view but I want to make the change permanent. I need some help with the syntax. So far I have
ALTER VIEW tableName
CONVERT(DATETIME, MyDates)
but it's obviously not working
Since a view (unless it's a materialized/indexed view which has some extra peculiarities) is more or less just a stored select query, what you do is to just change the select query and alter the view using that.
For example, if you have the view;
CREATE VIEW testview AS
SELECT id, value FROM test;
...where value is a varchar and you want it to be reflected in the view as a datetime, you can just issue;
ALTER VIEW testview AS
SELECT id, CAST(value AS DATETIME) value FROM test;
...to make it appear as a datetime in the view.
An SQLfiddle with a simple demo.
A view only fetches the data from the table as per the query.So you cannot change the datatype of the view. you have to change it in table.

Resources