Posts Tagged ‘ms-sql’
I received the following question a couple of days ago from a programmer using the “Ask a Question” form which you can access from the menu of this blog:
Given the following table:
How do I select names of persons who know both Hindi and English?
In this table, that query should return Nikhil and Kisu.
Last week I had an interesting assignment: Given a specific record, randomly retrieve three related records from the database.
Naturally, as a programmer, I started looking for the SQL random function. Which I found.
But even as I was searching for how to use that function, I was thinking to myself, “How am I going to structure things in such a way so as to randomly select the records once I have the function? It isn’t like I can retrieve the records into an array and then select them out. Am I going to have to retrieve them into a temp table?”
The IF statement we looked at on Tuesday was pretty tame compared to the WHILE construct.
Actually, the main thing you need to keep in mind is that WHILE is all you have. There is no such thing as a FOR loop or a DO WHILE loop. So, you have to force WHILE to do those for you.
The basic syntax of WHILE looks like this:
Before we get into the specifics of the commands that can be used within a stored procedure, I think it would be helpful to review some of the more basic commands that we can use that don’t really need a whole lot of discussion.
I know I said I wasn’t going to spend a lot of time on the basics, but my research indicates that a lot of people are searching for basic sql commands and very few sites are addressing that issue specifically.
So, if this is unhelpful to you, just consider this Dave’s SEO experiment.
Now that we’ve established a reason for using them, let’s continue with the basic syntax of the stored procedure. I think you’ll find that it is not that dissimilar to writing a procedure in any other language. Although, as we move on, I think you’ll find that the syntax we have to use to get anything done is quite cryptic.