Solved

SSMS Auto Generate scripts

Posted on 2014-12-28
7
95 Views
Last Modified: 2015-01-06
When you go into SQL 2012/2014 Management Studio and are looking at the list of the tables in a database and then on a table you right click and select Script Table as Insert as an example, if you have a table with hundreds of fields you will get a lengthy list of fields each one on its own row. Now since I am looking to take this script and paste it into a program, I would prefer to see fields group on a line with 7-8 fields per line. Is there a option setting that will allow that or some kind of conversion tool to format your SQL statement?
0
Comment
Question by:rwheeler23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 27

Expert Comment

by:Zberteoc
ID: 40521972
No. You will have to do it manually or to build a script yourself in order to get that. However, I don't think it worth the effort. The formatting is just a matter of make it easier to read and it doesn't really matter how many lines it will take.  Especially if you paste it in an application code in the end it will help to have that formatting.
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 40521998
Here is an example of code that will do that for you but only regarding the columns list:
declare
	@cols varchar(max)=''
select 
	@cols=@cols+
	COLUMN_NAME+' '+
	DATA_TYPE+' '+
	case when DATA_TYPE like '%CHAR' then '('+cast(CHARACTER_MAXIMUM_LENGTH as varchar)+')' else '' end +' '+
	case when DATA_TYPE in ('numeric', 'decimal') then '('+cast(NUMERIC_PRECISION as varchar)+','+cast(NUMERIC_SCALE as varchar)+')' else '' end +' '+
	case when COLUMN_DEFAULT is not null then COLUMN_DEFAULT
	else ''
	end+' '+
	case when IS_NULLABLE ='NO' then 'NOT NULL' else 'NULL' end + ' '+
	', '+case when ORDINAL_POSITION%7=0 then CHAR(13)+char(10) else '' end 

from 
	INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='YorTableName'
print @cols

Open in new window

0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 40522005
Small correction:
declare
	@cols varchar(max)=''
select 
	@cols=@cols+
	COLUMN_NAME+' '+
	DATA_TYPE+' '+
	case when DATA_TYPE like '%CHAR' then '('+cast(CHARACTER_MAXIMUM_LENGTH as varchar)+')' else '' end +' '+
	case when DATA_TYPE in ('numeric', 'decimal') then '('+cast(NUMERIC_PRECISION as varchar)+','+cast(NUMERIC_SCALE as varchar)+')' else '' end +' '+
	case when COLUMN_DEFAULT is not null then 'DEFAULT '+cast(COLUMN_DEFAULT as varchar)
	else ''
	end+' '+
	case when IS_NULLABLE ='NO' then 'NOT NULL' else 'NULL' end + ' '+
	', '+case when ORDINAL_POSITION%7=0 then CHAR(13)+char(10) else '' end 

from 
	INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='YorTableName'
print @cols

Open in new window

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:rwheeler23
ID: 40522014
Nice scripts. Thanks.
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 40522020
Be careful, though, the script doesn't deal with the identity property.
0
 
LVL 27

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 40522027
Here is with IDENTITY as well:
declare
	@cols varchar(max)=''
select 
	@cols=@cols+
	COLUMN_NAME+' '+
	DATA_TYPE+' '+
	case when COLUMNPROPERTY( OBJECT_ID(TABLE_NAME), COLUMN_NAME, 'IsIdentity')=1 then 'IDENTITY' else '' end +' '+
	case when DATA_TYPE like '%CHAR' then '('+cast(CHARACTER_MAXIMUM_LENGTH as varchar)+')' else '' end +' '+
	case when DATA_TYPE in ('numeric', 'decimal') then '('+cast(NUMERIC_PRECISION as varchar)+','+cast(NUMERIC_SCALE as varchar)+')' else '' end +' '+
	case when COLUMN_DEFAULT is not null then 'DEFAULT '+cast(COLUMN_DEFAULT as varchar)
	else ''
	end+' '+
	case when IS_NULLABLE ='NO' then 'NOT NULL' else 'NULL' end + ' '+
	', '+case when ORDINAL_POSITION%7=0 then CHAR(13)+char(10) else CHAR(13)+char(10) end 

from 
	INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='YorTableName'
print @cols

Open in new window

0
 

Author Closing Comment

by:rwheeler23
ID: 40533248
tHANKS
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CAST issue with SQL 2000 15 44
mssql 7 32
SQL - Load records to temp table through CTE 6 37
SQL Server 2012 and core licensing 5 30
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question