Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I dont have a detail knowledge of SQL ,

We have created a database on SQL server . As per design , another database is writing values on this database . But when it starts writing the tag values in predefined table.

I have checked the logs . Below is a sample of Error log , Sql is not able to write the tags in the table.

5/09/2020 18:01:55.876 khuDbClient SYSLOG ERROR SqlHelper.SqlInsertCommand() --> Values: 10 ,FP100_FDHL160_MPFM_MVHIST.FI101H ,9/15/2020 6:00:54 PM ,80 ,DM_QUALITY_GOOD ,
15/09/2020 18:01:55.960 khuDbClient SYSLOG ERROR SqlHelper.SqlInsertCommand() --> could not execute INSERT INTO WT_SNAPSHOT(WT_ID, TAG_NAME, TIMESTAMP , VALUE , QUALITY)VALUES(@param1,@param2,@param3,@param4,@param5)
System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Siemens.KHU.SQL.SqlHelper.SqlInsertCommand(String cmdText, Object[] values)
ClientConnectionId:a45c9b05-941c-4737-a622-a21085c6d786
Error Number:8152,State:2,Class:16

What I have tried:

Both database on the same machine.

DNS resolution

Firewall is disable

Also , the inbound and outbound rules define for port no 1433
Posted
Updated 15-Sep-20 22:27pm
Comments
[no name] 16-Sep-20 4:23am    
String or binary data would be truncated will be shown e.g. when you try to insert a string of 50 characters into a database field of e.g. VARCHAR 20

1 solution

Look at the error: it contains explicit information.
String or binary data would be truncated.

What that means is that one of the fields you are attempting to INSERT to is shorter than the data you are trying to insert.
For example, an NVARCHAR(5) field and the data 'ABCDEF'
Because the string is longer than the space provided, SQL rightly refuses to INSERT the data because it would have to throw away some of it and it doesn't like doing that.

To fix this, first find out what data you are trying to INSERT, and compare that against your table definition to find out which field is showing the problem.
Then either enlarge the field to fit the data, or in your presentation software validate the inputs to ensure they do fit in the existing column.

In practice, you should probably do both!
Quote:
One last thing , can you please tell me the meaning of (@param1 , @param2 .......)
I can understand a little that these are 5 fields of the table which SQL will write.


A basic SQL INSERT is like this:
SQL
INSERT INTO MyTable (Column1Name, Column2Name) VALUES (ValueForColumn1, ValueForColumn2)

But that's dangerous when you start sending text from an application because it leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
This is generally considered a Bad Thing. :D

The way to prevent that is to use parameterized queries - or to be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

A parameterized query looks much the same as a basic INSERT:
SQL
INSERT INTO MyTable (Column1Name, Column2Name) VALUES (@ValueForColumn1, @ValueForColumn2)
The "@" indicates to SQL what value to insert from which parameter to the query, and the parameters are added via special code which differs from language to language.
For C#:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
        cmd.ExecuteNonQuery();
        }
    }
And the "@name" parts tie them together.
 
Share this answer
 
v2
Comments
Member 14939572 16-Sep-20 4:36am    
I will try to look for the options , you have mentioned.
Member 14939572 16-Sep-20 10:38am    
We have the same system at site running properly . We haven't faced any issue at site. I made a replica of this system for in house testing and facing this issue. I have checked the character size for all the fields . It seems to be OK. Actually , I dont have the application source code . Is there any way to check the logs in database for exact error.
OriginalGriff 16-Sep-20 11:14am    
The logs will just show what you have, pretty much - they won't show the data you tried to add, and that's what you need to know.

If you don't have the source code, there isn't a lot of point trying to fix this as you can't change what is sent to SQL.
Member 14939572 16-Sep-20 11:47am    
ok , i got it , thanks for the info
Member 14939572 16-Sep-20 11:48am    
I will try to get the source code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900