MSSQL error #120 - sql-server

I have the following sql:
INSERT INTO [dbo].[table1]
([col1],[col2],[col3],...[col14])
SELECT * FROM [dbo].Table2
GO
Running it throws MSSQL Err #120 which means the number of columns line up to the INSERT. table2 has 5 columns and table1 has 14. Would I be correct in assuming that this is the cause of the error? The reason I ask is that a) Not familiar with MSSQL and b) I am unfamiliar with their database.

Yes, this is the cause of your error.
You will have to:
INSERT INTO [dbo].[table1]
([col1],[col2],[col3],...[col14])
SELECT
col1, col2, col3, col4, col5, --your table's columns
'const1', null, 1, etc... --other values that you want for the other columns,
FROM [dbo].Table2 --defaults, constants, null, etc.
GO
or
INSERT INTO [dbo].[table1]
([col1],[col2],[col3],[col4],[col5]) --the rest will have their default value.
SELECT
col1, col2, col3, col4, col5
FROM [dbo].Table2
GO

Related

Is it possible to create SQL query templates?

I have a couple of tables as data sources which have extremely similar structure. I only care about some columns of them and I want to join them. So what I do at the moment is:
SELECT 'table_a' AS source, col1, col2, col3, col4
FROM table_a as source_table
INNER JOIN other on source_table.id = other.id
UNION ALL
SELECT 'table_b' AS source, col1, col2, col3, col4
FROM table_b as source_table
INNER JOIN other on source_table.id = other.id
UNION ALL
SELECT 'table_c' AS source, col1, col2, col3, col4
FROM table_c as source_table
INNER JOIN other on source_table.id = other.id
UNION ALL
SELECT 'table_d' AS source, col1, col2, col3, col4
FROM table_d as source_table
INNER JOIN other on source_table.id = other.id
I would like to do something like this:
query(param1, param2) := {
SELECT param1 AS source, col1, col2, col3, col4
FROM param2 as source_table
INNER JOIN other on source_table.id = other.id
}
query('table_a', table_a)
UNION ALL
query('table_b', table_b)
UNION ALL
query('table_c', table_c)
UNION ALL
query('table_d', table_d)
I know how to do this within the programming language (using a templating engine and constructing the query string).
Is something like this possible within SQL (Snowflake Warehouse)?
You can't do exactly that I'm afraid. However, you can use Snowflake Stored Procedures (SP) to effectively achieve this. You can construct the SQL query text in SP based on the parameters passed to it, and then executing it. You can e.g. pass to it an array of table names etc.
One problem is that today SPs in Snowflake do not return the result of the query directly. To overcome this, you can e.g. save the result of the query in a new table (with the name hardcoded in SP, or passed as a parameter to SP) and then query it with a separate SELECT.

Error when inserting data in sparse table SQL

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

What's the most efficient syntax to use Merge to upsert many rows at once?

There's 2 ways I've found of upserting many rows into a table with SQL Server 2008.
One of which is found here http://technet.microsoft.com/en-us/library/bb522522(v=sql.105).aspx says to create a temp table, then insert values to temp table, and finally merge that table with target able.
This doesn't seem very efficient to me because you have to create a table, fill the table, merge to target table, and then delete the temp table.
The only other thing I can think of is as follows...
MERGE dbo.targettable as tgt
USING (
SELECT 12 as col1, 13 as col2, 'abc' as col3, 'zyx' as col4
UNION ALL
SELECT 11 as col1, 11 as col2, 'def' as col3, 'def' as col4
(etc etc)
UNION ALL
SELECT 7 as col1, 10 as col2, 'jfj' as col3, 'tub' as col4)
as new
ON tgt.col1=new.col1
WHEN MATCHED THEN UPDATE SET tgt.col2=new.col2, tgt.col3=new.col3, tgt.col4=new.col4
WHEN NOT MATCHED THEN INSERT (col1, col2, col3, col4)
VALUES(new.col1, new.col2, new.col3, new.col4);
Based on usr's answer I was able to find http://msdn.microsoft.com/en-us/library/bb510625.aspx
I think this is the way to do it. Could someone verify that this syntax appears correct?
MERGE dbo.targettable as tgt
USING (VALUES(12, 13, 'abc', 'zyx'), (11, 11, 'def', 'def'),(7, 10, 'jfj', 'tub'))
AS new (col1, col2, col3, col4)
ON tgt.col1=new.col1
WHEN MATCHED THEN UPDATE SET tgt.col2=new.col2, tgt.col3=new.col3, tgt.col4=new.col4
WHEN NOT MATCHED THEN INSERT (col1, col2, col3, col4)
VALUES(new.col1, new.col2, new.col3, new.col4);
Where does the data to be merged come from?
If it comes from a query, inline the query into the merge.
If it
comes from the app, use table-valued parameters.
If it is generated
iteratively, use a temp table or table variable.
If it is a constant like in your example use the VALUES clause. Don't use UNION ALL because it is more verbose, does not document semantics nicely and increases query compile time because the optimizer has to convert it to VALUES form.

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