473,750 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing SQL strings - style and clarity

Hello,

Just wondering how you all deal with writing large SQL strings when coding
database functionailty. Some of the SQL strings can get quite long and it
looks a bit messy and hard to read in the code. Is there a good way to write
the SQL so it remains neat, clear, readable and manageable?

Also, being new to VS 2005, is there a way in which SQL commands can be
tested in the Query browser and then 'parcelled up' automatiallt as
individual functions available for use throughout the app?

Thanks for any views,
Best regards,
David Ross
Jul 24 '06 #1
5 1218
Hello,
>
Just wondering how you all deal with writing large SQL strings when coding
database functionailty. Some of the SQL strings can get quite long and it
looks a bit messy and hard to read in the code. Is there a good way to write
the SQL so it remains neat, clear, readable and manageable?

Also, being new to VS 2005, is there a way in which SQL commands can be
tested in the Query browser and then 'parcelled up' automatiallt as
individual functions available for use throughout the app?

Thanks for any views,
Best regards,
David Ross
A better coding practice would be to put all those "sql strings" into
stored procedures, and call them (using parameters of course).
This way SqlServer can even precompile the sql-code, for better
performance.

Hans Kesting
Jul 24 '06 #2
"Hans Kesting" wrote:
Hello,

Just wondering how you all deal with writing large SQL strings when coding
database functionailty. Some of the SQL strings can get quite long and it
looks a bit messy and hard to read in the code. Is there a good way to write
the SQL so it remains neat, clear, readable and manageable?

Also, being new to VS 2005, is there a way in which SQL commands can be
tested in the Query browser and then 'parcelled up' automatiallt as
individual functions available for use throughout the app?

Thanks for any views,
Best regards,
David Ross

A better coding practice would be to put all those "sql strings" into
stored procedures, and call them (using parameters of course).
This way SqlServer can even precompile the sql-code, for better
performance.

Hans Kesting
Thanks Hans!

That sounds like a good approach. I'll give it a shot!

Cheers,
David
Jul 24 '06 #3
David++,

Hans' idea of using stored procs is a good one.
However, if you still must use long SQL from your app, one idea is to add
the SQL into a text file (eg. YourSQL.sql), then add the text file as an
embedded resource to your project. Then you can read the resource from your
code.

HTH,
Stephen
"David++" <Da***@discussi ons.microsoft.c omwrote in message
news:0A******** *************** ***********@mic rosoft.com...
Hello,

Just wondering how you all deal with writing large SQL strings when coding
database functionailty. Some of the SQL strings can get quite long and it
looks a bit messy and hard to read in the code. Is there a good way to
write
the SQL so it remains neat, clear, readable and manageable?

Also, being new to VS 2005, is there a way in which SQL commands can be
tested in the Query browser and then 'parcelled up' automatiallt as
individual functions available for use throughout the app?

Thanks for any views,
Best regards,
David Ross

Jul 24 '06 #4
"Stephen Ahn" wrote:
David++,

Hans' idea of using stored procs is a good one.
However, if you still must use long SQL from your app, one idea is to add
the SQL into a text file (eg. YourSQL.sql), then add the text file as an
embedded resource to your project. Then you can read the resource from your
code.

HTH,
Stephen
"David++" <Da***@discussi ons.microsoft.c omwrote in message
news:0A******** *************** ***********@mic rosoft.com...
Hello,

Just wondering how you all deal with writing large SQL strings when coding
database functionailty. Some of the SQL strings can get quite long and it
looks a bit messy and hard to read in the code. Is there a good way to
write
the SQL so it remains neat, clear, readable and manageable?

Also, being new to VS 2005, is there a way in which SQL commands can be
tested in the Query browser and then 'parcelled up' automatiallt as
individual functions available for use throughout the app?

Thanks for any views,
Best regards,
David Ross


Thanks Stephen,

I'll look into that option as well.

Cheers,
David
Jul 24 '06 #5
Hans Kesting wrote:
A better coding practice would be to put all those "sql strings" into
stored procedures, and call them (using parameters of course).
This way SqlServer can even precompile the sql-code, for better
performance.
You might wish to hold off on automatically putting all SQL queries in
stored procedures. There is some debate about this practice. I'm
still kind of middle of the road on it, but look at this article:

http://weblogs.asp.net/fbouma/archiv.../18/38178.aspx

As an alternative, what we have done is create a shared class called
SqlQuery with shared string constants that contain parameterized SQL
queries and then use that in our code (watch for typos):

Shared Class SqlQuery
Shared Const Query1 As String = "SELECT * FROM Table WHERE Column =
'ABC'"
End Class

Jul 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
7398
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
102
7116
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality of its documentation, it is a impeccability. it has or possesses properties of:
14
6110
by: dmh2000 | last post by:
I recently complained elsewhere that Python doesn't have multiline comments. i was told to use triple quoted strings to make multiline comments. My question is that since a triple quoted string is actually a language construct, does it use cause a runtime construction of a string which is then discarded, or is the runtime smart enough to see that it isn't used and so it doesn't construct it? example def fun(self):
4
2011
by: Kza | last post by:
Hi, just in the process of maintaining some software that used some funy old string library and char*s , and we are updating everything to use std::strings. (or should I say std::basic_string<>s) I find it wierd that that all the new c++ ansi style librarys like the streams and file handling classes still expect us to use old style char* type strings. For example, ofstreams open function expects the filename as a char* parameter rather...
89
5141
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append the contents of pString2 to pString? (giving "Blah Blah Blah")
23
2965
by: arnuld | last post by:
i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors, with "run-time shown", claim that C++ Library strings are faster than C-style character strings. i wrote the same programme in C & hence found that claim of the authors is *partial*. If we use C-style strings in C++ instead of Library String class, then they are slow but if write the same programme in C then C strings are "faster" than both C++ Library strings & C-style...
19
4778
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
4
1325
by: Andreas Beyer | last post by:
Hi, I found the following quite cryptic code, which basically reads the first column of some_file into a set. In Python I am used to seeing much more verbose/explicit code. However, the example below _may_ actually be faster than the usual "for line in ..." Do you consider this code good Python style? Or would you recommend to refrain from such complex single-line code?? Thanks!
4
1785
by: John Brock | last post by:
I have a .NET application that, among other things, creates Excel workbooks, and I have run into a very strange problem involving formulas on one worksheet that reference values on another worksheet. The text I write into, let's say, cell A25 on Sheet1 (using .NET) looks something like this: =VLOOKUP(RC,'Sheet2'!A:X,6,FALSE) On the completed workbook this turns into:
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.