What is best alternate for cursor in T-SQL? - sql

I have a design concern which I need to implement without using cursor.
There is source table 'A' which will have all column in varchar data type. I want to iterate over them and convert each column to destination table data type and if conversion/parsing fails, I need to log that row in extra error table.
Any suggestions to go ahead will be helpful.

In SQL Server, you would use try_convert():
insert into t2 ( . . . )
select . . .
from (select try_convert(?, col1) as col1,
try_convert(?, col1) as col2,
from staging_t
) t
where col1 is not null and col2 is not null and . . .;
Then run a second query to get the rows where the value is NULL.
If NULL is a permitted value in the staging column, then this is a bit more complex:
insert into t2 ( . . . )
select new_col1, new_col2, . . .
from (select try_convert(?, col1) as new_col1, col1,
try_convert(?, col1) as new_col2, col2,
from staging_t
) t
where (new_col1 is not null or col1 is null) and
(new_col2 is not null or col2 is null) and
. . .;

In Sql Server 2012 and up: each of these will return null when the conversion fails instead of an error.
try_convert(datatype,val)
try_cast(val as datatype)
try_parse(val as datatype [using culture])
Example of all varcharcol that would fail conversion to int:
select id, varcharcol
from a
where try_convert(int,varcharcol) is null

Use Above Query Using User Define Table Type
Create Type Table - Procedure - User Define Table Type
#UserDefineTypeTable UserDefineTypeTable readonly
insert into table1
col1,
col2,
col3
(select
type.col1,
type.col2,
type,col3
from #UserDefineTypeTable as type)

Related

Presto insert value in to a column of (array<struct<pos:int, date:string>>)

I have a column 'col2' which is of type
array<struct<pos:int, date:string>>
I need to check if the column is empty and then insert values to the column and then unnest the values in the column
case WHEN CARDINALITY(col2) = 0 THEN ARRAY[(0,'value1'),(0,'value2')] else col2 end as col2
Below is sql
WITH CTE AS
(SELECT
col1,
case
WHEN CARDINALITY(col2) = 0 THEN ARRAY[(0,'value1'),(0,'value2')]
else col2
end as col2
FROM table1
)
SELECT
col1
column2.value1 AS pos,
column2.value2 AS date,
FROM CTE
CROSS JOIN UNNEST(col2) AS t(column2)
Because the case expression returns [{field1=1,field2=2020-03-01},{field1=1,field2=2020-01-09}]
i am not able to unpack it as value1 and value2, and above expression throws error.
Can anyone help me to fix this?
When the elements of an array are of type row, UNNEST expands them into separate columns. You need to adjust the UNNEST clause to reflect this.
Here's an example (tested with Trino 351, formerly known as Presto SQL):
WITH
data(entries) AS (VALUES
ARRAY[],
ARRAY[(1,'x'),(2,'y')]
),
cte(entries) AS (
SELECT if(cardinality(entries) = 0, ARRAY[(0,'value1'),(0,'value2')], entries)
FROM data
)
SELECT pos, date
FROM cte
CROSS JOIN UNNEST(entries) AS t(pos, date)

How to apply NVL function to every column in redshift query

I want to apply a NVL function to every column in my query:
I.E I want to something like:
select nvl(student.*,'')
from student ;
Put another way, I would like this question answered but instead of Oracle SQL, use Redshift SQL
You have to go through one by one. If data type is int then replace it with coalesce(col1, 0) as col1
Example:
select
coalesce(col1, '') as col1,
coalesce(col2, '') as col2,
.
.
coalesce(colN, '') as colN
from table

enter parameter value with order by in sql

I'm getting a enter parameter value on cooltable.distance when Access executes ORDER BY, which is strange since distance is a column created in cooltable. What am I missing?
SELECT
target_postcodes.target_postcode,
population_postcodes.population_postcode,
cooltable.distance,
SQR( ( Population_postcodes.Longitude - target_postcodes.longitude)^2 + (Population_postcodes.Latitude - target_postcodes.latitude)^2 ) as distance
INTO
cooltable
FROM
Population_postcodes,
Target_postcodes
ORDER BY
cooltable.distance;
INSERT INTO CoolTable( col1, col2, col3 )
SELECT .....
FROM ...
WHERE ...

SQL Server SELECT shows my own value not from database

I'm looking for way to add my data to some columns,
select co1, col2, col3 from tbl
I want to cod3 exist but only show my data
select co1, col2, col3=3 from tbl
output should be
1, 0, 3
I have problem with CR9 and this is only way I guess .
if you want to call the 3rd column col3 just do
select co1,col2, '3' as col3 from tbl
by the way
select co1,col2,col=3 from tbl
was valid acceptable but not recommenened by microsoft until SQL2008R2 in 2012 is not accepted anymore
just use "select co1,col2,'3' from tbl".
try this answer
select co1,col2, '3' as col3 from tbl

INSERT INTO SELECT + 1 custom column

I need to copy data from original table and add custom column specified in query
Original table struct: col1, col2, col3
Insert table struct: x, col1, col2, col3
INSERT INTO newtable
SELECT *
FROM original
WHERE cond
and I'm getting this error
Column count doesn't match value count at row 1
HOW can I insert X value in this single query?
I tought something like this can pass
INSERT INTO newtable
SELECT 'x' = NULL, *
FROM original
WHERE cond
Any ideas?
Is it possible to use *? Because that table has so many columns and X has to be first value
I know this all is bad but I have to edit unbeliveable ugly db with even worse php code
The second statement is almost correct, but instead of 'x' = null, use null x (I'm assuming you want to store a null value in a column named x);
INSERT INTO newtable
SELECT null x, o.* FROM original o WHERE cond
Select Null as X, *
into newtable
from original
where ...
INSERT INTO newtable
SELECT null as x, col1, col2, col3 FROM original WHERE cond