SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Generate Create View statements for database tables.


Generate Create View statements for database tables.

Author
Message
whenriksen
whenriksen
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3131 Visits: 696
Comments posted to this topic are about the item Generate Create View statements for database tables.

Wes
(A solid design is always preferable to a creative workaround)
Hilda Garcia
Hilda Garcia
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 33
Can you show us what the output will look like?
whenriksen
whenriksen
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3131 Visits: 696
Here is the definition of the Employee table from the AdventureWorks db. The output is formatted as displayed.

Create View [dbo].[Employee] 
AS
( SELECT [EmpID]
, [NAME]
, [Salary]
FROM [AdventureWorks2008R2].[dbo].[Employee]
)
GO



As written, it uses the 3 part naming from the source table and uses the table name as is for the view name, which will not be valid while the table has the same name. Change the view name portion to fit your naming convention. For example to prefix the table name with a "v", add the v to the string containing the dot between the schema and table name, as below.

QUOTENAME(c1.TABLE_SCHEMA) + '.v' + QUOTENAME(c1.TABLE_NAME)



Wes
(A solid design is always preferable to a creative workaround)
Iwas Bornready
Iwas Bornready
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45628 Visits: 886
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search