Multiple values into one cell SQL Server - sql

I have an item A than has one-many relationship with Table T1 (Col1, Col2, Col3, Col4).
Currently I am retrieving data from T1 for Item A in XML into a single column. Is there any more efficient way to do so rather than parsing into XML.
The thing is in my query I have 1 column for Table T1's value.

Related

How to add NULL to a union of multiple tables that don't have the same number of columns?

I am trying to do a union of ten tables, all of which I want in a single flat file for data visualization. They share a lot of columns in common, but there is a decent amount of columns unique to each table that won't exist in others.
If it were only a few columns, I would do the following (see below) but this would take way too long for all of the columns and tables I am working with. Is there a way to do this without going through each column individually with NULL AS 'missingColumn'
SELECT COL1, COL2, COL3 FROM TABLE 1
UNION ALL
SELECT COL1, COL2, NULL AS COL3 FROM TABLE 2
I am using SQL Server
Thank you!

Copy related rows from two tables into those same tables with new values in SQL?

I have two tables, "table1" and "table2". They contain rows that are related to each other. The table "table2" has pairs of rows with a column named "table1_id" and these pairs refer to the sequential "id" columns in "table1".
The task that needs to be performed is that I need to copy rows from both tables and put these rows into the same tables with new data while maintaining the relationships between them.
I have read several posts on StackOverflow and some articles on "mssqltips.com", but I am still not sure how I should do this. Should I use a cursor or a query with joins and a temporary table? What is the best-practice way to achieve the above task and if possible, could you demonstrate a short example?
You cannot "copy both rows from both tables and put these into the same tables" verbatim if the id column is being copied and is truly an ID (a unique key). If you want to duplicate some other (non-key) columns into new rows with new IDs, that's fine. If that's what you want, you can create the primary key (table1) row(s) first, and reference the newly created keys. How you do this depends on whether the id column is a generated or explicitly specified key, and whether you have any keys (primary or foreign keys) in table2 as well.
It might look something like this:
insert into table1
select col1, col2, etc
from table1
where id = 'somekey'
insert into table2
select 'newkey' as table1_id, col3, col4, etc
from table2
where table1_id = 'somekey'
If you want to do this atomically, wrap it in a begin transaction, and make use of an OUTPUT clause or similar to collect the new IDs; see the documentation about that.

SQL 2012 Data importing and merging data

I am doing a project on database programming using SQL Server 2012 and also visual studio. I have created some tables and I have a excel file with lots of data. The specification at this stage is to merge two excel sheets of data that has 11 columns each (same columns for both files with different data) into a separate table that is then used later on in the project for paging etc.
My original vision was to create two tables, one table that had one excel tab of data and another table exactly the same except for the name to house the second data set, and then using a union join to merge the two tables into one. However importing straight to the tables is impossible (possibly due to the existence of a composite key in column 1 of the data) so I then created two new tables altogether that now does contain the data from the excel sheets however this doesn't meet the specification of merging the files into the table (as the data is still in two tables not one, and also it has to be in a certain table that was created by DDL earlier). Also it doesn't solve the problem as there doesn't seem to be a way to query those tables into an existing table (or is there?)
Anyway, thanks for reading, hopefully I have included enough information, if it seems I've missed something, feel free to ask. I think the ideal solution for this would involve joins of some description such as union but there doesn't seem to be anyway to then relate that join back to the existing table.
Basically, you will want to do something similar this:
SET IDENTITY_INSERT MainTable ON
INSERT INTO MainTable (col1, col2, col3 ... col11)
SELECT col1, col2, col3 ... col11
FROM Table1
UNION
SELECT col1, col2, col3 ... col11
FROM Table2
SET IDENTITY_INSERT MainTable OFF
You haven't specifically mentioned you have an IDENTITY field for your Primary Keys but I'm assuming you are, hence including the SET IDENTITY_INSERT commands.

SQL question regarding the insertion of empty tuples to prepare for update statements

I am making a table that will be borrowing a couple of columns from another table, but not all of them. Right now my new table doesn't have anything in it. I want to add X number of empty tuples to my table so that I can then begin adding data from my old table to my new table via update statements.
Is that the best way to do it? What is the syntax for adding empty rows? Thanks
Instead of inserting nulls and then updating them, cant you just insert data from the other table directly. using something like this -
INSERT INTO Table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM Table2
WHERE <some condition>
If you still want to insert empty records, then you will have to make sure that all your columns allow nulls. Then you can use something like this -
Table1
PrimaryKey_Col | col1 | col2 | col3
Insert INTO Table1 (PrimaryKey_col) values (<some value>)
This will make sure your a new row is inserted with a primary key and the rest of the columns are nulls. And these records can be updated later.
No, this is not even a good way, let alone the best way.
If you look at it conceptually adding empty rows serves no purpose.
In databases each row of a table corresponds to a true statement (fact). Adding a row with all NULLs (even if possible) records nothing and represents inconsistent data on its own. Especially having multiple empty records.
Also, if you are even able to add a row with all NULLs to a table that's an indication that you have no data integrity rules for the row so and that's mostly what databases are about - integrity rules and quality of data. A well designed database should not accept contradictory or meaningless data (and empty row is meaningless)
As Pavanred answered inserting real data is a single command, so there is no benefit in making it two or more commands.

Exporting data from spreadsheet to Pgsql database

I have one huge spreadsheet file (1000+ lines) and one postgreSQL table. I need to compare spreadsheet data with postgresql table data and add fill the blank fields in table with data from spreadsheet and add entries not present in db table.
Yes, I can convert (via csv) whole spreadsheet into database table. But, there are unique values in both documents, so I will lose data doing this. Or, is it possible to compare 2 tables and fill missing fields in table A with data from table B?
Thanks in advance!
It's easy in SQL to compare two tables, and insert rows not in one table. For example:
INSERT INTO TableA
(col1, col2, col3)
SELECT
col1, col2, col3
FROM SpreadSheetTable
WHERE NOT EXISTS (
SELECT *
FROM TableA
WHERE TableA.col1 = SpreadSheetTable.col1
)
This query inserts all rows from SpreadSheetTable into TableA, except those rows for which TableA already contains a row with the same "col1" value.