Nested table type in SQL server - sql

How can I define a table type in SQL server when one of the columns is an array of decimals?
I'm trying to pass to stored procedure an .NET object one of the fields of which is an array of decimals.
Thanks

t-sql does not support arrays.
You do, however, have some options: here are 3 of them, from the best to the worst:
Create 2 table types, have a column in one act as a foreign key to the other.
Create a table type with a varchar(max) column that will hold your decimal
values as a comma delimited string.
Create a table type with an xml data type column.

Related

Data Type Conversions with SQL and sqlalchemy.types

I am creating a table in SQL and sending data to it though some helper functions using python. For my function (call it: submit_to_sql), I send the dataframe, table name, and the data types. That data is then in the table on SQL. However, for one of my columns, I am having trouble figuring out which datatype I should provide.
The values I would like to pass are in the form of a decimal, 00.00. There are 2 significant digits with (in theory) unlimited characteristics before it. What is the correct data type that I should assign my column within my script, such that on SQL, the values display properly?
This is how I defined it at first, but it rounded the values:
# Define Column types in my script
import sqlalchemy
from sqlalchemy import types as types
#... skipping some code for brevity
'Column': types.Numeric()
--Create MyTable in SQL
CREATE TABLE [Table].[TableName](
[Column] [dec] NULL
)

importing data with commas in numeric fields into redshift

I am importing data into redshift using the SQL COPY statement. The data has comma thousands separators in the numeric fields which the COPY statement rejects.
The COPY statement has a number of options to specify field separators, date and time formats and NULL values. However I do not see anything to specify number formatting.
Do I need to preprocess the data before loading or is there a way to get redshift to parse the numbers corerctly?
Import the columns as TEXT data type in a temporary table
Insert the temporary table to your target table. Have your SELECT statement for the INSERT replace commas with empty strings, and cast the values to the correct numeric type.

Store SQL query result (1 column) as Array

After running my query I get 1 column result as
5
6
98
101
Is there a way to store this result as array so that I can use it later
in queries like
WHERE NOT IN ('5','6','98','101')
I am aware of storing single variable results but is this possible?
I can not use #Table variable as I will be rerunning the query again in the future and it goes out of scope
There are multiple way of storing those column data like using Temporary Tables or View or Table valued function but IMO there is no need of storing that column data anywhere. You can directly use that column in any query saying below (or) perform a JOIN which would be much better option than NOT IN
select * from
table2
where some_column not in (select column1 from this_table);
While this method is not recommended, storing an array in a single column can be done using CSV's(Comma Separated Values). Simply create a VARCHAR array and store it by storing a string containing the values in a specific order. Basically store all of your values into a string with each value being separated by a comma in that string. Store that into a column of your choice. You can later fetch the string and parse it with a string parser i.e using the .split() function in python. AGAIN I do not recommend doing this, I would instead use multiple columns, one referring to each value and access them that way instead
Using separate columns would make it easy to use in a Stored Procedure.

Changing a column type and keeping the data

I have a sql server 2005 database where one of the columns is an int. The database is full of data so I can't lose any of the data.
I need to change the the of the column from an int to a varchar(50) but this column is used in a lot of stored procedures where they are assigned to variables as int.
I want this column to store varchar now so how would I be able to do this?

How to pass two list of values to stored procedure in SQL Server?

So i am trying for writing a procedure which need to accept the input values in form of two lists as follows:
execute ProcedureForInsert 'john,marry,tom' '1,2,3' 40
where first parameter is list of Varchar type and second is list of int type and length of both lists are to be same and third parameter is also of int type.
My script is to use these two list values to insert into some table along with third parameter.
How to store and iterate two lists for creation of Insert Query .
Thanks in advance.
You need a "split string" function that takes a comma separated list and produces a table of the individual strings as output. There are a lot of these to be found, but look here to start:
How to split a comma-separated value to columns