False error message for binary data truncation(Msg 8152) - sql

I am getting false error message while data inserting to some table.
The data type for the field is Varchar(20) and the data being inserted has max
data length is 6.
I don't understand where's the issue is. Although I can avoid this by adding
SET ANSI_WARNINGS OFF
But it'd be workaround not a solution:

It seems your column SchemaName maxlength property smaller then value which you going to insert.
Update your column length as per your data.
ALTER TABLE Temp ALTER COLUMN SchemaName VARCHAR(XXXX)

I just found the root cause of this issue.
Actually I was trying to insert sysname data type value into varchar().
So must specify proper length to hold sysname data type.

Your target table is not sufficient to hold the data that you are trying to insert. It seems your target size would be varchar(256) but you have given varchar(10). Just extend the column size to resolve this issue.
Run this DDL
Alter table temp alter column SchemaName varchar(256)

This error come when your column has less size them.
Update size of your column.

Related

How to copy SQL column data to new column then drop the original column?

I am following the recommendation in this sql snowflake forum in order to transform an integer data column into a varchar by creating a new column. I want to drop the original integer column when I am done, but doing so always results in the new column no longer working and any future queries erroring out.
For instance, I have test_num is the integer and test_num_to_char is the varchar
alter table test_table
add test_num_to_char varchar as CAST(test_num as varchar)
then
alter table test_table
drop column test_num
select *
from test_table
results in an error message:
SQL execution internal error: Processing aborted due to error 300002:224117369
Is there a different transformation method that removes the dependency on the original integer column so I can drop it?
alter table test_table add test_num_to_char varchar(10);
go
update test_table set test_num_to_char = CAST(recno as varchar);
Try the TO_DECIMAL transformation method.
It's documentation is given here

How to change column data type of metadata table . What will be the update statement be like?

enter image description here
I need to change data type from int to bigint. This has to be changed in metadata as well after changing the source table . Please someone get back to me fast .
You can use the Alter Command.
ALTER TABLE tableNAme ALTER COLUMN columnName bigint;
GO
This will automatically change your metadata table as well. you don't need to change it yourself

ALTER TABLE using WHERE clause condition

I was under the assumption that they was a WHERE Clause for ALTER and I have understood now after some research that WHERE Clause doesn't exist for ALTER Command. How to handle cases where we might need to check some conditions in the ALTER Command?
this is my query , what's im doing wrong please ?
ALTER TABLE cp_asset_translations CHANGE caption caption VARCHAR(1000) DEFAULT NULL WHERE LENGTH(caption) > 255;
There is no WHERE. And there is no ability to change the length of a column in some rows but not others. But that is not a problem. You can change the length of the caption to VARCHAR(1000) and because the string is variable length, no additional space is used for shorter strings:
ALTER TABLE cp_asset_translations CHANGE caption caption VARCHAR(1000) DEFAULT NULL;
Note: I assume that this ALTER TABLE is valid in the database you are using. The statement varies significantly across databases.

Alter Table Column Type Does not Work in Aster - I am Probably Screwing It Up

I am using Aster as there are some groovy Random Forest Functions to use. My dependent, or response variable, is a boolean dichotomous variable; a 0 or 1.
When I run it through the Random Forest Function of choice it creates a predicted value of the response variable. It calls this variable prediction and it automatically creates it as a VARCHAR(REALLY BIG INTEGER IN HERE).
To do some of my calculations I simply wish to cast or convert it to an integer from a string. All of the resulting character strings are either a 0 or a 1:
alter table a0q892.zf_predict alter column prediction int;
does not work. The error message I receive is:
Executed as Single statement.
Failed [34 : 42000] [AsterData][ASTERJDBCDSII](34) ERROR: syntax error at or near "int" ()
I am pretty sure there are lots of fancy & elegant ways to do this. But I would think I could simply just make it an integer for future calculations?
As per the aster docs, there are limited options to manipulate colums. You cannot change a column data type.
However aster allows you to change the size of a varchar column. You mentioned that you want to cast to INTEGER, but I guess that in your use case VARCHAR(1) would be fine too. If yes, then you can go :
ALTER TABLE a0q892.zf_predict ADD prediction VARCHAR(1);
If you really need an INTEGER (or any other type than VARCHAR(n)), then you have to proceed the old way :
create a new column in the table with the correct type
fill it from the old column
drop the old column
rename the new column
SQL Aster :
ALTER TABLE a0q892.zf_predict ADD prediction_new int;
UPDATE TABLE a0q892.zf_predict SET prediction_new = CAST(prediction AS int);
ALTER TABLE a0q892.zf_predict DROP prediction;
ALTER TABLE a0q892.zf_predict RENAME prediction_new TO prediction;

How should i throw exception error from stored procedure such as " String or binary data would be truncated. "?

SQL Server 2012. Based on the stored procedure variable length; input value is automatically trimmed and inserted to the table.
Example : I am passing a variable #name varchar(10):
#name VARCHAR(10) = null
However, on inserting the record which is more than 10 characters through stored procedure, record get inserted by trimming the characters to first 10 digit.
I am expecting to get the error exception such as
String or binary data would be truncated
How should I throw an exception error from the stored procedure?
CREATE TABLE tbl_test
(
[ID] INT,
[NAME] VARCHAR(10),
)
GO
CREATE PROCEDURE usp_test
(#name VARCHAR(10) = NULL)
AS
SET ANSI_WARNINGS ON
BEGIN
INSERT INTO tbl_test
VALUES (1, #name)
INSERT INTO tbl_test([ID], [NAME])
VALUES (2, #name)
END
The behavior depends on the ANSI_WARNINGS session setting.
With ANSI_WARNINGS ON (the default in modern Microsoft SQL Server APIs), you will get the expected "string or binary data would be truncated" error when data are inserted into a column with a shorter length. ANSI_WARNINGS ON is implicitly set by ANSI_DEFAULTS ON.
With ANSI_WARNINGS OFF, the data will be silently truncated.
However, when you pass a parameter value that is longer than the defined parameter length, the value is truncated without error or warning regardless of the session setting. This is documented behavior that may not be what one expects:
SET ANSI_WARNINGS is not honored when passing parameters in a
procedure, user-defined function, or when declaring and setting
variables in a batch statement. For example, if a variable is defined
as char(3), and then set to a value larger than three characters, the
data is truncated to the defined size and the INSERT or UPDATE
statement succeeds.
so it is important to ensure supplied values do not exceed the defined parameter length.
One way to get this to work is to make sure your parameters are longer than the table column. Doesn't have to be much longer - one character would be enough. Then if you get passed a longer string, it will still be longer inside the stored procedure.
At that point you can either test for the length being too long and raise your own error, or if you try and put it in the table you'll get an error anyway. Either way, at least you'll know.