Results 1 to 11 of 11

Thread: How do YOU write sql statements?

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    How do YOU write sql statements?

    This is not about the syntax in writing sql, rather the coding techniques.

    In a typical sql statement column names and table names are required. Two possible way to write the statement would be by hardcoding the names in the statement text or by using constanst....

    Code:
    SELECT thisColumn, thatColumn FROM myTable WHERE anotherColumn = something
    OR

    Code:
    Const THIS_COLUMN = "thisColumn"
    Const THAT_COLUMN = "thatColumn"
    Const MY_TABLE = "myTable"
    Const ANOTHER_COLUMN = "anotherColumn"
    SelectStatement = string.format("SELECT {0}, {1} FROM {2} WHERE {3} = something",THIS_COLUMN, THAT_COLUMN, MY_TABLE, ANOTHER_COLUMN)
    I'm fairly very young when it comes to database programming, and am curious about how YOU handle specifying the names in the sql statements. I don't see that either method is not correct, but one way (or another) may have its benefits. What say you?

    Again, this is not a post about how something gets done, or how to do something, I'm just curious about popular methods is all.
    thanks

    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: How do YOU write sql statements?

    Asking this in the Database forum might get some better feedback.

    I've only used SQL databases on occasion, but I always type the column name into the SELECT statement, instead of using variables.

    I can't think of a reason why doing the opposite would be beneficial. Hm...
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do YOU write sql statements?

    The only reason that you might use the second option would be if you wanted to use the same value in multiple places.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: How do YOU write sql statements?

    Quote Originally Posted by jmcilhinney View Post
    The only reason that you might use the second option would be if you wanted to use the same value in multiple places.
    That's what I mean. It wouldn't really make anything easier or have a specific purpose, other than what was mentioned.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: How do YOU write sql statements?

    Was scolded.. err, I mean "taught" very early on this forum to use parameters and it actually is much nicer to code:

    vb Code:
    1. Dim cmd As New SqlCommand("SELECT FirstName, LastName FROM Employee WHERE FirstName = @fn", con)
    2.         cmd.Parameters.Add(New SqlParameter("@fn", SqlDbType.VarChar, 10)).Value = "Joe"

    It allows for better dynamics and less margin for error. As for the SELECT part (the table names) I usually use variable names that hold the table names. It lets me/the user define them much easier than a hard-coded value.

  6. #6
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: How do YOU write sql statements?

    everyone has got their own style of coding.....

    If you find my reply helpful , then rate it

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do YOU write sql statements?

    Quote Originally Posted by stateofidleness View Post
    Was scolded.. err, I mean "taught" very early on this forum to use parameters and it actually is much nicer to code:

    vb Code:
    1. Dim cmd As New SqlCommand("SELECT FirstName, LastName FROM Employee WHERE FirstName = @fn", con)
    2.         cmd.Parameters.Add(New SqlParameter("@fn", SqlDbType.VarChar, 10)).Value = "Joe"

    It allows for better dynamics and less margin for error. As for the SELECT part (the table names) I usually use variable names that hold the table names. It lets me/the user define them much easier than a hard-coded value.
    This question isn't about the values though; it's about the identifiers, i.e. the table and column names rather than the field values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: How do YOU write sql statements?

    Quote Originally Posted by jmcilhinney View Post
    This question isn't about the values though; it's about the identifiers, i.e. the table and column names rather than the field values.
    right, right.

    just giving me some insight on how I've been doing my sql statements as a whole
    vb.net Code:
    1. SELECT variableField1, variableField2 FROM variableTable WHERE ....

    as far as using constants though, seems like it would just "bloat" the code. since it won't change anyway, you might as well just hard-code it into the statement. at least that way you know what the field name is right away instead of referencing your list of constants at the top of your code.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do YOU write sql statements?

    Moved To Database Development

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How do YOU write sql statements?

    Personally I write out the column name implicitly every time. When someone is looking at my SQL statements I want to ensure they know exactly what I'm looking for in the statement.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: How do YOU write sql statements?

    I could see a benefit in using constant declaration BUT ONLY if you expect to be changing the table names in future versions of your application. For example if you referred to a table many times in your code then if you changed the table name then you'd only need to edit the const.

    But I can't think of any case where I've ever changed the name of a table or field. So putting the real table names in the actual sql is easier to read. Why make source harder to read.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width