Tag Archives: sql

Error Number:8152,State:10,Class:16 String or binary data would be truncated. The statement has been terminated. – Entity Framework

Hi everyone,

I ran into the following error after adding model validation attributes to a database with existing values:

Error Number:8152,State:10,Class:16
String or binary data would be truncated.
The statement has been terminated.

The solution is pretty straightforward if you’re happy to truncate the values. Simply run the query below, swapping Title for your column name, Products for your table name and 25 for your new column length:

UPDATE Products
SET Title = LEFT(Title, 25)
WHERE LEN(Title) > 25

You should then be able to run update-database without any issues:

PM> update-database
Specify the ‘-Verbose’ flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201806240741543_product_validation].
Applying explicit migration: 201806240741543_product_validation.
Running Seed method.
PM>

Thanks to David in for his answer on Stackoverflow: https://stackoverflow.com/a/24931522/522859

The installer has encountered an unexpected error installing this package.This may indicate a problem with this package.The error code is 29506 – SQL Server Management Studio

Hey everyone,

I ran into the following error while trying to install Sql Server Management Studio on Windows 8:

The installer has encountered an unexpected error installing this package.This may indicate a problem with this package.The error code is 29506

To fix it, all you need to do is run the exe with administrative privileges.

View Generated SQL in Entity Framework (EF)

Hey everyone,

Just a quick post on how to view the generated sql in entity framework. To start with, just add the following line to your db context constructor:

public class TestDbContext : DbContext
    {
        public TestDbContext() : base("name=TestDbContext")
        {
            this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); //This line
        }
        ...

With this line added, you should now be able to see all of your generated sql in the output window.

SELECT TOP (1) 
    [Project1].[VidId] AS [VidId], 
    [Project1].[CreatedAt] AS [CreatedAt], 
    [Project1].[CHECK] AS [CHECK], 
    [Project1].[SourceCheck] AS [SourceCheck]
    FROM ( SELECT 
        [Extent1].[VidId] AS [VidId], 
        [Extent1].[CreatedAt] AS [CreatedAt], 
        [Extent1].[CHECK] AS [CHECK], 
        [Extent1].[SourceCheck] AS [SourceCheck]
        FROM [dbo].[Vids] AS [Extent1]
        WHERE [Extent1].[VidId] < @p__linq__0
    )  AS [Project1]
    ORDER BY [Project1].[VidId] DESC
 
-- p__linq__0: '535' (Type = Int32)
-- Executing at 28/06/2014 5:00:16 PM +10:00
-- Completed in 0 ms with result: SqlDataReader

If you’re not using EF6, there were a few other options where I came across this solution: http://stackoverflow.com/a/20751723/522859

ORA-28000: the account is locked – PeopleSoft

Hey everyone,

Just a small PeopleSoft issue I ran into while running an SQR:

(SQR 5528) ORACLE OCISessionBegin(RDBMS) error 28000 in cursor 0:
   ORA-28000: the account is locked
(SQR 4701) Cannot logon to the database.

SQR for PeopleSoft: Program Aborting.

For some reason the sysadm account had been locked, thankfully there’s a fairly easy fix:

  1. ALTER USER sysadm ACCOUNT UNLOCK;

If you don’t have access you may need to get a DBA to run it.

Oracle – Number of Weekdays Between Two Dates

Hey everyone,

Just a quick post on how to select the number of weekdays between two dates:

SELECT temp_tbl.days
--SELECT COUNT(temp_tbl.days)
FROM (          
        SELECT (TRUNC(TO_DATE ('01/08/2012', 'DD/MM/YYYY'), 'dd') + LEVEL - 1) days
        FROM DUAL
        CONNECT BY LEVEL <= TO_CHAR(TO_DATE('10/08/2012', 'DD/MM/YYYY') - TO_DATE('01/08/2012', 'DD/MM/YYYY'))
     ) temp_tbl
WHERE TO_CHAR(temp_tbl.days, 'D') >= 2
      AND TO_CHAR(temp_tbl.days, 'D') <= 6

Sending Email – PL/SQL

A quick post on how to send an email with PL/SQL.

There’s a LOT of documentation available at the following link:
http://www.orafaq.com/wiki/Send_mail_from_PL/SQL

  1. PROCEDURE EMAIL_ERROR_REPORT IS
  2.  
  3.        /* Create vars */
  4.        v_From        VARCHAR2 (80) := 'test@test.com';
  5.        v_Recipient   VARCHAR2 (80) := 'me@me.com';--REPORT_RECEIVER;
  6.        v_Subject     VARCHAR2 (80) := 'test subject';
  7.        v_Mail_Host   VARCHAR2 (30) := 'smtp.test.com';
  8.        v_Mail_Conn   UTL_SMTP.Connection;
  9.        crlf          VARCHAR2 (2) := CHR (13) || CHR (10);
  10.     BEGIN
  11.  
  12.        /* Define connection */
  13.        v_Mail_Conn := UTL_SMTP.Open_Connection (v_Mail_Host, 25);
  14.        UTL_SMTP.Helo (v_Mail_Conn, v_Mail_Host);
  15.        UTL_SMTP.Mail (v_Mail_Conn, v_From);
  16.        UTL_SMTP.Rcpt (v_Mail_Conn, v_Recipient);
  17.        UTL_SMTP.Data (
  18.           v_Mail_Conn,
  19.              'Date: '
  20.           || TO_CHAR (SYSDATE, 'Dy, DD Mon YYYY hh24:mi:ss')
  21.           || crlf
  22.           || 'From: '
  23.           || v_From
  24.           || crlf
  25.           || 'Subject: '
  26.           || v_Subject
  27.           || crlf
  28.           || 'To: '
  29.           || v_Recipient
  30.           || crlf
  31.           || crlf
  32.           || 'some message text'
  33.           || crlf
  34.           ||                                                       -- Message body
  35.             'more message text'
  36.           || crlf);
  37.        UTL_SMTP.Quit (v_mail_conn);
  38.     EXCEPTION
  39.  
  40.        /* Catch exceptions */
  41.        --WHEN OTHERS THEN         
  42.     END;

Unique Values – Ruby on Rails

Just a quick post on how to select unique values in rails using the uniq operator:

  1. #All recent categories
  2. <% recent_categories = Category.joins(:products).where('products.user_id = ?', current_user.id).order('products.created_at desc') %>
  3.  
  4. #Results
  5. Cars
  6. Trucks
  7. Cars
  8. Technology
  9. Phones
  10. Technology
  11. ...
  1. #Unique recent categories
  2. <% recent_categories = Category.joins(:products).where('products.user_id = ?', current_user.id).order('products.created_at desc').uniq %>
  3.  
  4. Cars
  5. Trucks
  6. Technology
  7. Phones
  8. ...

ERROR: column “category” does not exist

I’ve recently switched from SQLite3 to PostGreSQL and came across the following error this morning:

my_app_development=> select category from codes;
ERROR:  column "category" does not exist
LINE 1: select category from codes;

The following post solved the issues in one sentence:

http://archives.postgresql.org/pgsql-novice/2007-01/msg00032.php

Yes. In postgres, unquoted column and table names are converted to lower case.

The user also added that when using PostGreSql you should either be always, or never using quotes around column names.

undefined method `to_sym’ for nil:NilClass – Ruby on Rails Migration

While trying to do a migration today I received the following message:

undefined method `to_sym’ for nil:NilClass

#After running a trace (rake db:migrate –trace)
undefined method `to_sym’ for nil:NilClass
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:in `method_missing’

This was the migration:

class AddDefaultValuesToFeedbacks < ActiveRecord::Migration
  def change
    change_column :stores, :total_feedbacks, :integer, :default => 0
    change_column :stores, :rating, :decimal, :precision => 8, :scale => 2, :default => 0
  end
end

Unfortunately this problem also prevented me from performing any other migrations on the table. I attempted deleting all relevant migration files and then re-running them, restarting the server and rolling back to a previous migration – none of which worked.

Eventually I resorted to exporting the dataset, dropping the table via sqlite3 and recreating it. Thankfully this seems to have worked. The only hint I’ve come across that may explain the cause of this problem, other than typos or referring to non-existant columns, is that it can occur if a migration is screwed over before it finishes and fails to rollback properly.

If anyone else has any concrete answers, please let me know in the comments!