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
Solved

SSMS Auto Generate scripts

Posted on 2014-12-28
7
91 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
  • 5
  • 2
7 Comments
 
LVL 26

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 26

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 26

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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

Expert Comment

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

809 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