Error when inserting data in sparse table SQL - sql-server

I am trying to run an insert query on a SQL wide table (Sparse Table), but I am getting an error:
"Column name or number of supplied values does not match table definition"
Query:
Insert [dbo].[Table1] (Table1 is a sparse table)
select id, [A],[B], [C], [D], [E] from (
Select ID,
CategoryId,
1 as Flag
From dbo.table2
) a Pivot(Avg(Flag) For CategoryID In (
[A],
[B],
[C],
[D],
[E]
)) As PivotTable
I am able to run the query for a normal sql table but it fails for a sparse table. I would really appreciate any help on this
Thanks in Advance

You should do:
Create an INSERT statement that explicitly lists the columns it will insert into - assuming that ID might be an IDENTITY column
that you don't want / can't insert into
Define the exact number of values to fill into these columns
your INSERT statement should be something like:
insert into table_1 (cola, colb, colc)
select cola, colb, colc from table_2
insert into tb1 values('1', '2','3') - this works fine as long you only have 3 columns
if you have 4 columns but only want to insert into 3 of them.
You have to include column names in INSERT INTO
insert into tb1 (Col1,col2,col3) select col1, col2, col3 from tb_2
Note: Always explicitly define the list of columns that an INSERT statement should fill data into

Related

Union all in snowflake

I'm using union all in my code but one of the tables contains a column that the other one doesn't, how can I insert that column in the output, I don't wanna add it in the table itself.. I need that column to be there and the number of columns should be the same from both tables
Thanks
The normal way to deal with this is to pad the short query with 'dummy' columns.
Suppose TABLE1 has four columns called COL1, COL2, COL3 and COL4.
The TABLE2 3 called COLA, COLB and COLC.
SELECT
COL1
,COL2
,COL3
,COL4
FROM TABLE1
UNION
SELECT
COLA
,COLB
,COLC
,NULL -- or empty string or zero or any padding value compatible with the data-type of COL4.
FROM TABLE2 ;
The results should have 4 columns (COL1 thru COL4) because the column names are taken from the first query.
This practice of padding out data structures to the same signature is common in Databases when combining related but different types and I know it as The Battenberg Manoeuvre.

Output insert and table values when doing insert into

I'm inserting into a table in MS SQL Server 2008 (it's rather a copy of values from the same table) and want to get the output values for the insert. I want to get the id value of the select statement (t.id in the example below), the INSERTED.id works just fine
create table tmp.tbl_inserted (fromId int, toId int)
INSERT INTO mytable (name)
OUTPUT t.id, INSERTED.id INTO tmp.tbl_inserted
SELECT t.name FROM mytable t
Thanks in advance
You can't do it directly from an INSERT:
from_table_name
Is a column prefix that specifies a table included in the FROM clause of a DELETE, UPDATE, or MERGE statement that is used to specify the rows to update or delete.
Note that INSERT isn't mentioned.
What you have to do instead is cheat and use a MERGE:
MERGE INTO mytable m
USING (name,id FROM mytable) t ON 1=0
WHEN NOT MATCHED THEN INSERT (name) VALUES (t.name)
OUTPUT t.id, INSERTED.id INTO tmp.SizeCurveGroup_inserted
;

How can I use the plus operation for a column in SQL Server with the result in the next row?

I have a table in SQL Server that I want to plus amount of a specific column and have the result in next row.
How can I do that?
I want to plus amount of a specific column and have the result in next row.
In case you are looking to insert another from the previous row after adding certain amount, you could use the following:
INSERT INTO MyTable (Col1, Col2, Col3)
SELECT Col1, Col2 + <additional amount>, Col3
FROM MyTable
WHERE
<Criteria to select that row of interest>
In case you are looking to select all the rows in a table and aggregate the amount column and show the result in a separate row, then you could use the following:
SELECT Col1, Col2, Col3 FROM MyTable
UNION
SELECT '', SUM(Col2), '' FROM MyTable

SQL Merge with inserting into the third table

I want to create a merge that will compare two tables and insert not matched values into another third table or table variable
something like this:
MERGE Assets AS target
USING (#id, #name)FROM Sales AS source (id, name) ON (target.id = SOURCE.id)
WHEN MATCHED THEN
UPDATE SET target.Status = #status, target.DateModified = SYSUTCDATETIME()
WHEN NOT MATCHED THEN
INSERT INTO #tableVar (id, name, status, dateModified)
VALUES (#id, #name, #status, SYSUTCDATETIME())
Can this be done or are there other methods?
You just cannot do this. MERGE operates on two tables only - source and target.
For your requirement, you need to e.g. use a CTE (Common Table Expression) to find the rows that don't match - and insert those into the third table.
Something like:
;WITH NonMatchedData AS
(
-- adapt this as needed - just determine which rows match your criteria,
-- and make sure to return all the columns necessary for the subsequent INSERT
SELECT (columns)
FROM dbo.SourceTable
WHERE ID NOT IN (SELECT DISTINCT ID FROM dbo.TargetTable)
)
INSERT INTO dbo.ThirdTable(Col1, Col2, ....., ColN)
SELECT Col1, Col2, ....., ColN
FROM NonMatchedData
You CAN do this very easily...
You can wrap the MERGE statement within a INSERT INTO FROM:
http://technet.microsoft.com/en-us/library/bb510625.aspx#sectionToggle2
-OR-
You can do it directly within the merge statement:
Quick example:
WHEN NOT MATCHED THEN
DELETE
OUTPUT Deleted.* INTO dbo.MyTable;
This will insert the non-matches into your existing destination table. You can use the Updated, Inserted, Deleted v-tables to direct data other places.

SQL Insert into Select failure due to one of the set of records

I am trying to insert a set of records into a table using
Insert into tbl1 Select * from tbl
One of the records failed due to check constrain in tbl1. But i want to insert the other records which have passed the check constraint and others i want to catch them as exception. Could someone please help.
In that case, you need to be more selective about your SELECT - exclude those rows that are trouble from your selection:
INSERT INTO dbo.tbl1(Col1, Col2, ...., ColN)
SELECT Col1, Col2, ....., ColN
FROM dbo.tbl
WHERE (some condition here to exclude those rows that don't match the `CHECK` constraint.....)

Resources