How to assign a column to a dynamic variable - sql

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.

Related

How to apply functions stored in a column to other values in the row SQL?

I have a table with function names 'testTable' and would like to use the functions stored in the 'Function' column on values 'Value 1' and 'Value 2'. The 'Function' stores the names of functions which I already have in my schema.
I have this code, but obviously it does not like the dbo.[Function] I have tried using the function as a variable and without the 'dbo' but still, doesn't work.
SELECT *,
CASE
WHEN [Function] IS NOT NULL
THEN dbo.[Function]([Value 1],[Value 2])
ELSE NULL END AS Result
FROM testTable t
Thanks for any help!

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).

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.

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!

How can i check in MS SQL Server 2005 if a column is of a specific type

I want to change the type of a column in MS SQL Server 2005, but before i change the type of that column i want to check if that column is of the type i want to change. How can i do that in SQL?
Thanxs,
Bas Hendriks.
Based on the anwser i wrote the following query that did the trick:
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'e_application'
AND COLUMN_NAME = 'txt_locked_by'
AND DATA_TYPE = 'nvarchar'
AND CHARACTER_MAXIMUM_LENGTH = 15 )
BEGIN
ALTER TABLE.....
END
You can query the INFORMATION_SCHEMA tables.
E.g.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable'
you can use sp_help TableName , it also retrieve's the column's data types and other properties