Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL statement in multiple lines throwing error

To prep my question defensively, I've utilized Google, Bing and StackOv prior to posting :-). Also, I'm new to MVC3 and still grappling with the syntactical intricacies of the framework.

I have an error in my SQL statement in the code block below which is bugging me quite a bit. The syntax appears correct. I simplified the SQL statement with a Select * From.. and it returns data just fine.

Also, if there is a better way to do this (without using an EF object), definitely open to suggestions. I really like the flexibility and control of seeing the SQL Statement - either that, or just used to it as form of habit :-).

Thanks in advance!!

@using System.Data.SqlClient;
@using System.Configuration;

@{
    Layout = null;
}
@{
    SqlConnection cn = null;
    cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
    cn.Open();
   SqlCommand cmd = new SqlCommand((@"SELECT DISTINCT" +
                                    "tblSBT.sname," +
                                    "tblSBDetails.sid," + 
                                    "tblSBDetails.assignedtrack," + 
                                    "tblSBDetails.maxtrack," + 
                                    "tblSBDetails.currentvals," + 
                                    "tblSBDetails.maxvals," + 
                                    "tblSBDetails.lastupdated" +
                                    "FROM" +         
                                        "tblSBT (NOLOCK)" +
                                    "LEFT OUTER JOIN" +
                                        "tblSBDetails (NOLOCK)" +
                                    "ON" +
                                        "tblSBT.sid = tblSBDetails.sid" +                      
                                    "WHERE" +
                                    "tblSBDetails.lastupdated > DateADD(n, -5, GETDATE())"+
                                    "ORDER BY" +
                                    "tblSBT.sname" +), cn);

    var myreader = cmd.ExecuteReader();
}
like image 974
mynameisneo Avatar asked Jan 20 '12 04:01

mynameisneo


People also ask

How do you do multiple lines in SQL?

Push and hold the shift key. While doing that, push and hold the alt key, so you have the shift and alt held down right now. Then push the up arrow to move up in the list.

Can SQL keywords be split across lines?

Writing SQL StatementsKeywords cannot be split across lines or abbreviated. Clauses are usually placed on separate lines for readability and ease of editing.

Are SQL statements executed sequentially?

When Oracle is not parallelizing the execution of SQL statements, each SQL statement is executed sequentially by a single process. With parallel execution, however, multiple processes work together simultaneously to execute a single SQL statement.

Which character is used to continue a statement in the Sqlplus?

You can continue a long SQL*Plus command by typing a hyphen at the end of the line and pressing Return.


1 Answers

If you're using the @ symbol, you don't need to concatenate your strings like how you are doing it. It's also not the most efficient way of writing that piece of code when you're joining strings like that.

SqlConnection cn = null;
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
cn.Open();
SqlCommand cmd = new SqlCommand((@"SELECT DISTINCT
        tblSBT.sname,
        tblSBDetails.sid,
        tblSBT.sname,
        tblSBDetails.sid, 
        tblSBDetails.assignedtrack,
        tblSBDetails.maxtrack,
        tblSBDetails.currentvals,
        tblSBDetails.maxvals,  
        tblSBDetails.lastupdated
        FROM    tblSBT (NOLOCK)
                    LEFT OUTER JOIN tblSBDetails (NOLOCK)
                        ON .sid = tblSBDetails.sid
        WHERE   tblSBDetails.lastupdated > DateADD(n, -5, GETDATE())
        ORDER BY    tblSBT.sname"), cn);

var myreader = cmd.ExecuteReader();
like image 176
clyc Avatar answered Sep 17 '22 19:09

clyc