Quick Search for:  in language:    
SQL,SPUDF,INI,base,idea,article,extend,bounda
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 44,501. lines
 Jobs: 125. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for SQL
Beginners pack
By hardik shah on 1/11


Inline Views
By Thivya Prabakaran on 1/6


Create thousand Data Base users quickly
By N.D.I.Samantha on 1/1


Numbers from String
By Jeff Michelson on 12/30


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Code stored in Binary/Text files instead in Stored Procedures-SQL SERVER

Print
Email
 

Submitted on: 9/26/2003 3:48:29 AM
By: Raghavendra Narayana 
Level: Advanced
User Rating: By 6 Users
Compatibility:SQL Server 2000

Users have accessed this article 3208 times.
 

(About the author)
 
     The base idea of this article is to extend the boundary of SQL Server by introducing Code/part of code stored in ASCII/Binary/other file format in same/different server. The code of the Stored Procedures or User Defined Functions need not to reside only in SP/UDF, you can have the code/part of code in ASCII/Binary/Other file format and stored under specific server/folder. This concept is useful while introducing .INI file concept in SQL Server. Plus you can avoid re-compilation of Stored Procedures on every small change. To some extent this concept also introduces code security, non-sharing of business logics.

This article has accompanying files

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Code stored in Binary/Text files instead in Stored Procedures-SQL SERVER

 

The base idea of this article is to extend the boundary of SQL Server by introducing Code/part of code stored in ASCII/Binary/other file format in same/different server.  The code of the Stored Procedures or User Defined Functions need not to reside only in SP/UDF, you can have the code/part of code in ASCII/Binary/Other file format and stored under specific server/folder.  This concept is useful while introducing .INI file concept in SQL Server.  Plus you can avoid re-compilation of Stored Procedures on every small change.  To some extent this concept also introduces code security, non-sharing of business logics.

 

Advantages

Ø       For security purpose.

o        Store the “Query/part of query/Stored Procedure” in a Text/Binary/other file format. Give a restricted access to the server/folder. Read the file through “Bulk Insert” command or through any other feature of SQL SERVER for execution.

o        Include an encryption/decryption algorithm to store/retrieve the code for execution from a Text/other file format.

Ø       Reduce the creation/usage of GUIs if the input data is very less. You can even come out with some innovative approaches by making use of “Replaceable Parameter in Batch files” DOS concept.

Ø       Access/Leverage can be given to Users who have SQL Query knowledge. 

Ø       Reduce the Developer/DBA work/involvement in some specific tasks where business ideas cannot be shared with Development team.

 

Let us explore the cases where we can make use of this concept.  Case 1 and 2 explained here are expectable project requirements.  But the Case 3 can be given a thought on Security terms.

 

Case 1: .INI File

A SQL Server job is scheduled to run on daily basis at 11:30 PM.  The Job will execute one Stored Procedure to produce an Excel file by querying table(s).  The report is an analysis report based on Department(s).  User wants data of different department(s) during different weeks/months/quarters in the year.  The “Department” is the only input required for the Report, there is no GUI involved in it.  User has access to one .INI file where he could add/change the department names.  The SP reads the .INI file stored in a shared folder, and generates the report based on the departments stored in .INI file. 

 

Dept.ini contains two departments viz., BPO, DEVELOPMENT.  So the report will be on these departments.

 

CREATE PROCEDURE DBO.CASE1

AS

BEGIN

      --Procedure to generate Profit & Loss report

      --DECLARATION SECTION

 

      --OTHER SQL STATEMENTS

     

      --Temporary table for getting Department Name

      CREATE TABLE #tmpDept (Dept_Name VARCHAR(20))  

 

      --Gets the data from .ini file to Temp table

      BULK INSERT #tmpDept

         FROM '\\computer1\Thomson_Financial\dept.ini'

         WITH (ROWTERMINATOR = '\n')

 

      --Get some summary data from a view

      SELECT dept_ID, dept_TurnOver, dept_Profit, dept_Loss

FROM vw_DeptSummary

      WHERE dept_ID IN (SELECT DEPT_ID FROM DBO.DEPARTMENT

                        WHERE DEPT_NAME IN (SELECT C1 FROM #TMP))

 

      --OTHER SQL STATEMENTS

END

 

 

 

 

Case 2: SQL Query

There is a SQL Query which changes constantly due to the requirement changes.  And every time there is a change you need to edit the SP, recompile it. The setup exists in many other locations, so you need to contact respective DB authority for changes every time.  In this case, you can store that query in a Text file.  Let the SP read the Text file, get the query, and execute it.

 

Query.sql file contains the constantly changing query.

 

 

CREATE PROCEDURE dbo.CASE2

AS

BEGIN

      --DECLARATION SECTION

      DECLARE @SQLQuery as NVARCHAR(4000)

 

      --OTHER SQL STATEMENTS

     

      --Temporary table for getting the query

      CREATE TABLE #tmpQuery (Query NVARCHAR(4000))

 

      --Gets the query from .sql to Temp table

      BULK INSERT #tmpQuery

         FROM '\\computer1\Thomson_Financial\QUERY.sql'

 

      --Get Query to a Variable for execution

      SELECT @SQLQuery = Query FROM #TMPQUERY

 

      --Execute the query

      EXECUTE sp_executesql @SQLQUERY

 

      --OTHER SQL STATEMENTS

END

 

Case 3: Complete Stored Procedure in a Binary File

 

There are some business related functions need to be created/maintained.  It is related to core business stuff and Management does not want to explain it to the Development team.  One of the Management guys has SQL Server skill and he will take care of creating/editing the content of Stored Procedure.  It is converted to Binary file and stored in server with some specific rights.  So the task of Developer is to just read the complete Binary file and execute it.

 

You can even introduce encrypt/decrypt feature for security purpose.  Just add converting/decrypting back to original code in the calling SP.

 

The SP is in a Binary file.

 

CREATE PROCEDURE dbo.CASE3

AS

BEGIN

      DECLARE @SQLsp as NVARCHAR(4000)

 

      --Temporary table for getting the SP

      CREATE TABLE #tmpSP (spText NVARCHAR(4000))

 

      --Gets the SP from .sql to Temp table

      BULK INSERT #tmpSP

         FROM '\\computer1\Thomson_Financial\SP.sql'

 

      --Convert/Decrypt Code on the content of SP.sql

 

      --Get the complete SP to a Variable to execute it

      SELECT @SQLsp = spText FROM #tmpSP

 

      --Execute the SP

      EXECUTE sp_executesql @SQLsp

END

 

Conclusion

Some of the readers may feel that this kind of requirements would be rare ones, but if you look at the MSDN News Group queries, people are in search of many innovative approaches.  Now they are not just sticking to the Basics of DB objects.  This effort of Binary/Text File incorporation in Stored Procedures is also a small contribution towards extending the boundaries of MS SQL Server 2000.

 

Raghavendra Narayana

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.

Virus note:All files are scanned once-a-day by Planet Source Code for viruses,but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:
1)Re-scan downloaded files using your personal virus checker before using it.
2)NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 9 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
10/3/2003 1:14:56 PM:foxsermon
Personally, I think it's a good idea, 5 gloves for U.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/6/2003 4:54:04 AM:Zorrer
Very nice **** from me!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.