0

I have some bulk views to create for an entire database.

To create a view the general syntax is as follows:

CREATE VIEW [TABLE_NAME] 
AS 
    SELECT [COLUMN1], [COLUMN2], [COLUMN3], [COLUMN4]
    FROM [TABLE_NAME]
    WITH CHECK OPTION;

I would like to set the column names in the script above by querying the column names ([COLULMN1], [COLUMN2], etc) from INFORMATION_SCHEMA.COLUMNS.

Is there a way to achieve this by table name?

2

2 Answers 2

3

COALESCE is your friend good programmer. What you want to do is get a csv list of COLUMNS. Then using dynamic sql you can auto generate the rest of the code.

 declare @columns AS VARCHAR(MAX)

SELECT @COLUMNS = NULL


select  @COLUMNS = coalesce(@columns+',','')+c.name from syscolumns as c
inner join sysobjects as o on c.id = o.id
WHERE O.NAME = 'change me to your table name'

SELECT @COLUMNS
SELECT ' CREATE VIEW ' + 'COOL VIEW NAME' + ' AS ' +
' SELECT ' + @COLUMNS +
 ' FROM '+ ' change me to your table name '+
 ' WITH CHECK OPTION;' 

EDIT

I purposely didn't declare the view anywhere. If you want to declare the view just execute the scripts like so. BUT YOU SHOULD NEVER just execute code on your servers without reading it all I purposely excluded the execution part as I think it is bad judgement just to cut and paste code and execute it without understanding/testing.

DECLARE @sql varchar(max)


    SELECT @sql = ' CREATE VIEW ' + 'COOL VIEW NAME' + ' AS ' +
    ' SELECT ' + @COLUMNS +
     ' FROM '+ ' change me to your table name '+
     ' WITH CHECK OPTION;' 

EXEC(@sql);
Sign up to request clarification or add additional context in comments.

4 Comments

Wonderful and SMART IDEA! Thank you very much for your suggestion. I'll give it a try and let you know how it works!
Tried this and it didn't seem to work. The following was executed successfully but I don't see the view being created anywhere... ??
DECLARE @fieldnames VARCHAR(MAX) select @fieldnames = coalesce (@fieldnames+', ', '') + V_FIELDNAME from tmp_fieldNames --Select @fieldnames Select 'CREATE VIEW ' + 'DS_STAGE_QAS.dbo.DYN_MARC_P2004' + ' AS ' + 'SELECT ' + @fieldnames + 'FROM ' + 'STAGE_MARC' + 'WHERE STAGE_MARC.PLANT = 2040' + 'with check option;'
Disregard - it will work.. I just have some clean up to do. Thank you very much for your assistance.
0

Here's one option... replace "MyTableName" with the table name you want, or wrap it in a cursor that reads TABLE_NAME from INFORMATION_SCHEMA.VIEWS into @tableName:

DECLARE @tableName sysname;
DECLARE @sql nvarchar(max);
DECLARE @columnList nvarchar(max);
SELECT @tableName = 'MyTableName';
SELECT @columnList = '';
SELECT @columnList += CASE WHEN LEN(@columnList) = 0 THEN '' ELSE ', ' END + COLUMN_NAME 
  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName ORDER BY ORDINAL_POSITION;
SELECT @sql = 'CREATE VIEW [TABLE_NAME] AS 
 SELECT ' + @columnList + '
FROM [' + @tableName + ']
 WITH CHECK OPTION;'

PRINT @sql
EXEC(@sql);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.