Collections:
Assigning New Column Names in a View in SQL Server
How To Assign New Column Names in a View in SQL Server?
✍: FYIcenter.com
By default, column names in a view are provided by the underlying SELECT statement.
But sometimes, the underlying SELECT statement can not provide names for output columns that specified as expressions with functions and operations. In this case, you need to assign new names for the view's columns. The tutorial exercise below creates a view to merge several table columns into a single view column with a format called CSV (Comma Separated Values):
CREATE VIEW fyi_links_dump AS
SELECT CONVERT(VARCHAR(20),id)
+ ', ' + CONVERT(VARCHAR(20),counts)
+ ', ''' + url + ''''
FROM fyi_links WHERE counts > 1000
GO
Msg 4511, Level 16, State 1, Procedure fyi_links_dump,
Line 2
Create View or Function failed because no column name
was specified for column 1.
CREATE VIEW fyi_links_dump (Line) AS
SELECT CONVERT(VARCHAR(20),id)
+ ', ' + CONVERT(VARCHAR(20),counts)
+ ', ''' + url + ''''
FROM fyi_links WHERE counts > 1000
GO
SELECT TOP 3 * FROM fyi_links_dump
GO
Line
------------------------------------------------------------
7600, 237946, ' eyfndw jdt lee ztejeyx l q jdh k '
19437, 222337, ' eypx u x'
55924, 1877, ' eyq ntohxe i rtnlu riwaskzp cucoa dva c rc'
The first CREATE VIEW gives you an error, because the SELECT statement returns no column for the concatenated value, and no view column name is specified explicitly.
⇒ Determining Data Types of View Columns in SQL Server
⇐ Deleting Data from a View in SQL Server
2016-11-03, 2289🔥, 0💬
Popular Posts:
What Is Oracle in Oracle? Oracle is a company. Oracle is also a database server, which manages data ...
Where to find answers to frequently asked questions on INSERT, UPDATE and DELETE Statements in MySQL...
How To End a Stored Procedure Properly in SQL Server Transact-SQL? Where the end of the "CREATE PROC...
How To Use SQL*Plus Built-in Timers in Oracle? If you don't have a stopwatch/timer and want to measu...
How To Get a List of All Tables with "sys.tables" View in SQL Server? If you want to see the table y...