search dynamically from any table - dynamic

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.

Related

Add array data type to sql database

Is it possible to add array data type to the postgreSQL database?
Like this :
ALTER TABLE table_name
ADD COLUMN new_column_name ARRAY;
You will need to define the type of the array, e.g. if you want an array of integers, use int[]
ALTER TABLE table_name
ADD COLUMN new_column_name int[];
(or use the ARRAY keyword as shown in Sebastian's answer)
For more details, see the manual
You can use ARRAY on any built-in or user-defined base type, enum type, or composite type like this:
ALTER TABLE table_name ADD COLUMN new_column_name INTEGER ARRAY;
demo on dbfiddle.uk

SELECT command: String or binary data would be truncated

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'

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.

ISDATE Function in Oracle

I am developing a web application which is getting data from an Oracle DB. The select statements are created dynamically. What I want to do is, whenever I select a date field in a table, it should return it to a string with the format of dd.mm.yyyy
what I need is basically a way to have a function like isdate(COLUMN_NAME, true stmt, false stmt)
SELECT ISDATE(First Column, to_char(FirstColumn,'dd.mm.yyyy'), FistColumn)
FROM ANYTABLE
is there a way for this?
You can check to see what the data type is for that table using the data dictionary, and connect multiple versions of the same query to handle whatever data type it might be.
For example let's say you had this table:
create table tbl_char (dt varchar2(10));
insert into tbl_char values ('01.03.2013');
And then ran:
select to_char(dt, 'dd.mm.yyyy')
from tbl_char
where exists (select 'x'
from all_tab_cols
where table_name = 'TBL_CHAR'
and column_name = 'DT'
and data_type = 'DATE')
union all
select dt
from tbl_char
where exists (select 'x'
from all_tab_cols
where table_name = 'TBL_CHAR'
and column_name = 'DT'
and data_type = 'VARCHAR2')
You would get one row, "01.03.2013", as output, because only the 2nd query actually ran. The first would have returned an error if not for the filter resulting from the EXISTS subquery. Now, if we were to change that varchar field over to a date, we would get exactly the same output, only the result would technically be from the first query. The second would run and return no rows.
sql fiddle: http://sqlfiddle.com/#!4/0001d/1/0

How to replace a string in a SQL Server Table Column

I have a table (SQL Sever) which references paths (UNC or otherwise), but now the path is going to change.
In the path column, I have many records and I need to change just a portion of the path, but not the entire path. And I need to change the same string to the new one, in every record.
How can I do this with a simple update?
It's this easy:
update my_table
set path = replace(path, 'oldstring', 'newstring')
UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')
I tried the above but it did not yield the correct result. The following one does:
update table
set path = replace(path, 'oldstring', 'newstring') where path = 'oldstring'
UPDATE CustomReports_Ta
SET vchFilter = REPLACE(CAST(vchFilter AS nvarchar(max)), '\\Ingl-report\Templates', 'C:\Customer_Templates')
where CAST(vchFilter AS nvarchar(max)) LIKE '%\\Ingl-report\Templates%'
Without the CAST function I got an error
Argument data type ntext is invalid for argument 1 of replace function.
You can use this query
update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'
all answers are great but I just want to give you a good example
select replace('this value from table', 'table', 'table but updated')
this SQL statement will replace the existence of the word "table"
(second parameter) inside the given statement(first parameter) with the third parameter
the initial value is this value from table but after executing replace function it will be this value from table but updated
and here is a real example
UPDATE publication
SET doi = replace(doi, '10.7440/perifrasis', '10.25025/perifrasis')
WHERE doi like '10.7440/perifrasis%'
for example if we have this value
10.7440/perifrasis.2010.1.issue-1
it will become
10.25025/perifrasis.2010.1.issue-1
hope this gives you better visualization
select replace(ImagePath, '~/', '../') as NewImagePath from tblMyTable
where "ImagePath" is my column Name. "NewImagePath" is temporery
column Name insted of "ImagePath" "~/" is my current string.(old
string) "../" is my requried string.(new string)
"tblMyTable" is my table in database.
you need to replace path with the help of replace function.
update table_name set column_name = replace(column_name, 'oldstring', 'newstring')
here column_name refers to that column which you want to change.
Hope it will work.
If target column type is other than varchar/nvarchar like text, we need to cast the column value as string and then convert it as:
update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'
You also can replace large text for email template at run time, here is an simple example for that.
DECLARE #xml NVARCHAR(MAX)
SET #xml = CAST((SELECT [column] AS 'td','',
,[StartDate] AS 'td'
FROM [table]
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
select REPLACE((EmailTemplate), '[#xml]', #xml) as Newtemplate
FROM [dbo].[template] where id = 1