Quantcast
Viewing all articles
Browse latest Browse all 15889

After doing an ALTER TABLE, inserts not working correctly

I upgraded my SQL Server from 2008 to 2012, and everything worked o.k.

I then added three columns to some tables. I used the following script to alter the tables:

    private int ModifyNewColumns(string sSymbol) {
      int nRowsAffected = 0;
      try {
        var connectionString = ConnectionString.GetConnectionString();
        var queryString = "BEGIN " +"ALTER table [day" + sSymbol + "] add Bid DECIMAL(8,2) NOT NULL default(0.0)" +"ALTER table [day" + sSymbol + "] add Ask DECIMAL(8,2) NOT NULL default(0.0)" +"ALTER table [day" + sSymbol + "] add VWAP DECIMAL(8,2) NOT NULL default(0.0)" +"END";

        using (var connection = new SqlConnection(connectionString)) {
          using (var command = new SqlCommand(queryString, connection)) {
            command.Connection.Open();
            nRowsAffected = command.ExecuteNonQuery();
            Console.WriteLine("day" + sSymbol + ", " + nRowsAffected);
          }
        }
      } catch (SqlException e) {
        if (!e.Message.Contains("Column names in each table must be unique.")) {
          Console.WriteLine("ModifyNewColumns on :" + sSymbol + ", error: " + e.Message);
        }
      }
      return nRowsAffected;
    }

This worked without any errors. However, I notice that when I try to INSERT a record with the three new values, the insert does not work correctly. The first 5 columns are updated, but the last three contain 0's. The code I am using to do the INSERT is:

static public void InsertDayRecord( string symbol, long iVolume, decimal dLast, int mSeconds, decimal dBid, decimal dAsk, decimal dVWAP ) {

// Check to make sure that the new values are non-zero if (dBid == 0.00M) { var minutes = (mSeconds/1000 - MarketHours.MarketOpenTime())/60; var s = String.Format("{0} {1} Last: {2,7:f3} bid: {3,7:f3} ask: {4,7:f3} VWAP: {5,7:f3}", minutes, symbol, dLast, dBid, dAsk, dVWAP); Debug.WriteLine(s); } var tableName = "day" + symbol; using (var connection = new SqlConnection(connStr)) { connection.Open(); //var command = connection.CreateCommand(); using (var command = new SqlCommand()) { command.Connection = connection; try { command.CommandText ="INSERT INTO [" + tableName + "] " +"(Symbol, Volume, Last, mSeconds, Bid, Ask, VWAP) VALUES " +"(@symbol, @volume, @last, @mseconds, @bid, @ask, @vwap)"; command.Connection = connection; command.CommandTimeout = 0; command.Parameters.Add("@symbol", SqlDbType.VarChar).Value = symbol; command.Parameters.Add("@volume", SqlDbType.BigInt).Value = iVolume; command.Parameters.Add("@last", SqlDbType.Decimal).Value = dLast; command.Parameters.Add("@mseconds", SqlDbType.Int).Value = mSeconds; command.Parameters.Add("@bid", SqlDbType.Decimal).Value = dBid; command.Parameters.Add("@ask", SqlDbType.Decimal).Value = dAsk; command.Parameters.Add("@vwap", SqlDbType.Decimal).Value = dVWAP; var nRowsChanged = command.ExecuteNonQuery(); if (nRowsChanged != 1) { var minutes = (mSeconds/1000 - MarketHours.MarketOpenTime())/60; var s = String.Format("status: {0} {1} Last: {2,7:f3} bid: {3,7:f3} ask: {4,7:f3} VWAP: {5,7:f3}", minutes, symbol, dLast, dBid, dAsk, dVWAP); Debug.WriteLine(s); } } catch(Exception ex) { SMErrorLog.WriteLog("InsertDayRecord, symbol: " + symbol, ex); } } } }

Notice that I test to make sure that dBid, dAsk and dVWAP are always non-zero, and the rows are all modified. I don't get an error on the INSERT. However, when to retrieve all the rows of the table, the last three columns are zero.

When I look at the design of each table, it looks like:

    Nth            int                   Unchecked
    Symbol      varchar(12)     Unchecked
    Volume      bigint              Unchecked
    Last           decimal(8, 2)   Unchecked
    mSeconds  int                   Unchecked
    Bid             decimal(8, 2)   Unchecked
    Ask            decimal(8, 2)   Unchecked
    VWAP        decimal(8, 2)   Unchecked

The default value for Bid, Ask and VWAP is: ((0.0)).

I do not understand why only the original first five columns are updated, and the new columns contain the default value (0.0)

Any help will be greatly appreciated.
Charles


Viewing all articles
Browse latest Browse all 15889

Trending Articles