SELECT command: String or binary data would be truncated - sql

When I'm trying to run the query:
SELECT id
FROM tbl_MessageData
WHERE data LIKE 'very_long_text'
I get the error:
String or binary data would be truncated
The data field is defined to be Text and I succeeded inserting the "very_long_text" (the actual text is about 10000 chars long) into it.

You can only use 8000 chars in a query. Text data type will be removed in a future version of Microsoft SQL Server, instead use varchar or nvarchar.
You can see your column INFO:
SELECT column_name, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'YourTable'
You need to modify your column size like this:
Alter table tbl_MessageData
ALTER COLUMN data VARCHAR(max) NOT NULL
Than you can try:
SELECT id
FROM tbl_MessageData
WHERE data LIKE 'very_long_text'

Related

In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

I am able to obtain column names and data types from a database table with:
SELECT COLUMN_NAME, DATA_TYPE
FROM information_schema.columns
WHERE TABLE_NAME = 'xxx'
How can I get the column name and data type from a view, function, or stored procedure? I imagine I'd have to obtain them using the results of each but I'm unsure.
Thanks for the help
Columns for view:
SELECT * FROM sys.columns c where c.object_id = OBJECT_ID('<schema>.<view name>')
Columns for table valued function:
SELECT * FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS rc WHERE rc.TABLE_NAME = '<udf name>'
Columns for stored procedure
For SQL server 2012 and later:
SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set_for_object
(
OBJECT_ID('<shcema>.<sp name>'),
NULL
);
Taken from Retrieve column names and types of a stored procedure?. Read answers there for possible ways to do this (for example with pre 2012).

How to assign a column to a dynamic variable

One column in one table of my database say (Table A and Column A) can be either of Numeric type or VARCHAR type. Datatype is decided dynamically and then table gets created.
I need to create a dynamic variable (#Dynamic) which should check the datatype of this column and assign a different column (column B or column C) to it accordingly i.e.
If column A is NVARCHAR, assign column B to #Dynamic
If column A is NUMERIC, assign column C to #Dynamic
I've to do this in both SQL Server and Oracle.
Any help to write a function for this would be greatly appreciated.
In sql server you can check column data type
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'Table A'
AND COLUMN_NAME = 'Column A'
In oracle
SELECT Type
FROM user_tab_columns
WHERE table_name = 'Table A'
AND column_name = 'Column A';
Sql_variant is a dynamic type in SQL Server. The eqivalent in oracle would be anydata type.
But if you are using dynamic SQL why don't you store the data in nvarchar as it is big enough for the convertet numeric values as well and you can use it directly for your dynamic SQL statement?
Actually this was a generic question and did not need any sample data to answer.
The approach I am following is:
I wrote a function
CREATE FUNCTION [dbo].[GET_DATA_TYPE] (#input VARCHAR)
RETURNS VARCHAR(255) AS
BEGIN
DECLARE #l_data_type VARCHAR(255)
SELECT #l_data_type=DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = #input AND
COLUMN_NAME = 'AssetID'
RETURN #l_data_type
END
I would call this function in my stored procedures as
SELECT #dataType = dbo.GET_DATA_TYPE(#input);
I declared another variable i.e. #FinalType
IF #dataType == 'numeric'
THEN #FinalType = columnC
IF #dataType == 'nvarchar'
THEN #FinalType = columnD
Then I'll use this #FinalType variable in all my dynamic sqls.
Any other efficient way to do this.

search dynamically from any table

I want to make a function module that make the same query, for example:
Select column_id from table_name where column_name = name_value.
I want to pass the table_name, column name and name_value, so, no matter what table is, I can get the id of the provided name.
Could you lead me in how to do that in abap using function modules?
Let's say you took the following parameters as input.
DATA: table_name TYPE string VALUE 'MARA',
column_id TYPE string VALUE 'MATNR',
column_name TYPE string VALUE 'MTART',
name_value TYPE string VALUE 'HALB'.
First, dynamically create a table of the type you will select into.
DATA: results TYPE REF TO data,
tablety TYPE string.
FIELD-SYMBOLS <results> TYPE STANDARD TABLE.
tablety = table_name && '-' && column_id.
CREATE DATA results TYPE TABLE OF (tablety).
ASSIGN results->* TO <results>.
Then use a dynamic query to fill the table.
DATA: condition TYPE string.
condition = column_name && ` = name_value`.
SELECT (column_id) FROM (table_name)
INTO TABLE results
WHERE (condition).
Pass back the generically-typed reference to the calling program.

SQL: Why does SYS.COLUMNS show my columns having different properties than what I set

I added a table to a database with the following column names and datatypes
"ID" bigInt, "Description" nchar(11)
If I check the design view of the table, the datatypes of the columns are as I set them.
If I write a query that will get the columns in that table from SYS.COLUMNS the column properties are different e.g.(the max_length of description is 22, not 11)
Can someone explain why that is?
I am using Microsoft SQL Server Enterprise Edition (64-bit) if that is relevant.
The column max_length in sys.columns show the max length in bytes. nchar is double byte so 11 chars take 22 bytes.
If you want the number of characters you could use INFORMATION_SCHEMA.COLUMNS
select COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'YourTable'
Or you could use the function COLUMNPROPERTY
select name,
columnproperty(object_id, name, 'charmaxlen') as charmaxlen
from sys.columns
where object_name(object_id) = 'YourTable'

Alter every double fields of a table

I need to change every double precision field of a table to numeric(15,3) type,
how can i do this job quickly with a stored procedure that iterate through the field of a given table and if the type is double precision alter column to numeric ?
following query should return query to update these tables... you can add table filter if you want. Copy the result and run it.
select 'ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name + 'TYPE numeric(15,3)'
from information_schema.columns
where data_type = 'double precision'
and table_name = 'YOUR_TABLE_NAME'
TEST IT BEFORE RUN IT. I DID NOT TEST THIS YET.
While another question will do it for all columns; a simple "alter table [tablename] alter column [columnToAlter] type numeric(15,3). You shouldn't need to run them through a cursor; any value that's not going to be affected by this should remain unaffected.
If you can't do it by changing the datatype itself, a simple update [tablename] set [columnname] = cast(columnname as numeric(15,3) should also work.
Hope that helps!