Click here to Skip to main content
15,886,830 members
Articles / Database Development / SQL Server

Use a SQL script to generate well formatted stored procedures in SQL Server

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Feb 2012CPOL1 min read 32.4K   5   5
Use a SQL script to generate well formatted stored procedures in SQL Server

Occasionally, you realize that you desire to create a lot of stored procedures, and that the information to build each stored procedure is contained within the database. But how can you best generate the code from the data? Well, assuming you like SQL, you can probably use SQL to generate your SQL.

Here is an example of doing just that, along with a few tips to make the code you generate human-readable.

  • Use char(13) + char(10) to wrap your output to the next line.
  • Use char(9) to indent by one tab.
  • Create one line of output for each line of your code-generating SQL script.
  • First write the query that obtains the values you need for code generation without generating the code to make sure you have the correct result set.
  • Generate your output to text, then copy the output into a new query window and it should look great!

This example creates a stored procedure for each table in your database. Each stored procedure will query the first integer column in the table and return the first row with an integer value greater than the value you passed in. This particular script will probably not be useful to you, and you may not like the way I formatted my output, but my only intent is to provide you a nice starting point for doing something like this to meet your own needs. This should work on most versions of SQL Server. Enjoy!

SQL
 select
‘CREATE PROCEDURE mysp_’ + i.TABLE_NAME + ‘FIRSTCOL ‘ + char(13) + char(10)
+ ‘(‘ + char(13) + char(10)
+ char(9) + ‘@’ + i.COLUMN_NAME + ‘ int ‘ + char(13) + char(10)
+ ‘)’ + char(13) + char(10)
+ ‘AS’ + char(13) + char(10)
+ ‘BEGIN’ + char(13) + char(10)
+ char(9) + ‘SET NOCOUNT ON’ + char(13) + char(10)
+ char(9) + ‘DECLARE @Err int’ + char(13) + char(10)
+ ‘/*Comment-Begin*/’ + char(13) + char(10)
+ ‘SELECT ‘ + i.COLUMN_NAME + char(13) + char(10)
+ char(9) + ‘FROM ‘ + char(13) + char(10)
+ char(9) + char(9) + i.TABLE_NAME + char(13) + char(10)
+ ‘ WHERE ‘ + char(13) + char(10)
+ char(9) + char(9) + i.COLUMN_NAME + ‘ > @’ + i.COLUMN_NAME + char(13) + char(10)
+ ‘/*Comment-End*/’ + char(13) + char(10)
+ char(9) + ’SET @Err = @@Error’ + char(13) + char(10)
+ char(9) + ‘RETURN @Err’ + char(13) + char(10)
+ ‘END’ + char(13) + char(10)
+ ‘GO’ + char(13) + char(10)
+ char(13) + char(10)
+ ‘GRANT EXEC ON ‘ + ‘mysp_’ + i.TABLE_NAME + ‘FIRSTCOL ‘ + ‘TO everyone’ 
+ char(13) + char(10)
+ ‘GO’ + char(13) + char(10)
+ char(13) + char(10)
+ char(13) + char(10)
from INFORMATION_SCHEMA.COLUMNS i WHERE i.ORDINAL_POSITION=1 and DATA_TYPE = ‘int’

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Kraft Software LLC
United States United States
Rob Kraft is an independent software developer for Kraft Software LLC. He has been a software developer since the mid 80s and has a Master's Degree in Project Management. Rob lives near Kansas City, Missouri.

Comments and Discussions

 
GeneralLike textformt.in tool Pin
Member 427128517-Oct-14 2:00
Member 427128517-Oct-14 2:00 
Questionwhat is the purpose of ur script Pin
Tridip Bhattacharjee15-Feb-12 19:51
professionalTridip Bhattacharjee15-Feb-12 19:51 
AnswerRe: what is the purpose of ur script Pin
Rob Kraft16-Feb-12 2:29
professionalRob Kraft16-Feb-12 2:29 
QuestionGetting error when execute ur script Pin
Tridip Bhattacharjee13-Feb-12 19:36
professionalTridip Bhattacharjee13-Feb-12 19:36 
AnswerRe: Getting error when execute ur script Pin
Rob Kraft14-Feb-12 2:34
professionalRob Kraft14-Feb-12 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.