Inserted less records than selected - snowflake-cloud-data-platform

I have a simple SQL insert on Snowflake:
insert into table(
column1,
column2,
column3,
column4,
column5)
select
substring(data_string,1,3),
substring(data_string,4,3),
substring(data_string,7,3),
current_timestamp,
'1'
from table1
Select gives me ~690K records but it inserts only ~334K records. When I delete ~200K records from table it still inserts less.
There are no duplicates.
Columns are the right size.
I am not able to post the data because of the security.
So I am just asking if someone had the similar problem and the solution?

Related

MSSQL Insert Data From One Table To Another With Fixed Value

Hello I would like to insert in a table called Data multiple columns from another table called SourceTable and one colum that has a standar value for every row added in Data.
Assume that you have Column1 and Column2 in the table called SourceTable and source_id is precalculated and it will be the same for every row added into Data on this query.
INSERT INTO Data (Columns1, Column2, source_id)
SELECT Column1, Column2
FROM SourceTable
UNION SELECT 2;
I tried this one but is not working, most likely because the SELECT 2 returns only one row.
Your issue is that you're giving SQL 3 columns to insert 2 values into, if source_id is going to be 2 as your union selects then you'd want something like this;
INSERT INTO Data (Columns1, Column2, source_id)
SELECT Column1, Column2, 2
FROM SourceTable
The number of columns you're inserting needs to match the number of columns that you're inserting to. The way you were doing it would have produced this result;
Column1 Column2 source_id
Value1 Value2
2
but even the union would have failed as the queries that you're unioning need to have the same number of columns.

how to insert multiple lines repeatedly in a SQL file in SQL Server

In a SQL file, is it possible to insert multiple lines in multiple queries all at once? For example, I have a stored procedure SQL file that has several queries. Each query writes into a table the exact same way. It writes in the following way:
INSERT INTO some_table
SELECT
'some_value' AS column1,
'some_other_value' AS column2,
'another_value' AS column3
FROM source_table
....
I'm trying to add the following lines between the last column and the FROM clause:
'stuff' AS column4,
'more_stuff' AS column5
So, the final query will look like:
INSERT INTO some_table
SELECT
'some_value' AS column1,
'some_other_value' AS column2,
'another_value' AS column3,
'stuff' AS column4,
'more_stuff' AS column5
FROM source_table
....
While I can do this manually, it will be very time-consuming to do it for every query. Is there a way to do it for all queries at once?
In the find-replace method, I don't think I can do the following:
1) Find all lines that read
'some_stuff'
2) Replace each of the identified lines with the following set of multiple lines:
'some_stuff'
'more_stuff'
'yet_more_stuff'

How can I create a select query that returns columns and rows and then combines some of the columns into XML

In TSQL is there a function to select a row as normal and then as an aliased column return some of those fields in XML format.
I'm creating an error log and I'd like to insert into a table the Primary key as a standard column and then some of the fields in the table as combined xml. I'm pretty new to dealing with xml and I've found out how to return xml data but not as a aliased column of a select query.
Is this possible? I apologise if this has been answered already. I have a feeling I just don't know what the right search terms are.
Thanks!
You can use a sub-select with FOR XML to to this, something along the lines of this;
select
id,
column1,
column2,
column3,
(select
column4,
column5,
column6
from
my_table t2
where
t2.id = t1.id
for xml path, type) as columnx
from
my_table t1

Inserting multiple rows into SQL Server 2008 table, copied from other rows with one column changed

I have a table with several columns (and thousands of rows). I am trying to make a new set of rows work over several shops, so I need to copy a group of rows, change one column, and then reinsert them into my table (the column I'm changing is a key, so that's not a problem).
So far, I've gotten to this point:
INSERT INTO TableName( Column1, Column2, Column3, Column4, Column5, Column6)
(SELECT Column1, Column2, Column3, Column4, Column5, 'NewShopCode'
FROM TableName
WHERE Column5 = 'DistinguishingValue'
For each new shop, I need to do this for two different 'DistinguishingValue's, and I have about ten different 'NewShopCode's.
I have tried ('NewShopCode1', 'NewShopCode2') in place of the single literal insert above, as well as using VALUES before my SELECT statement. Neither of these work, returning a syntax error in each case.
Also, while, in this instance, it's not too much of a hassle to just do two separate queries for DV1 and 2, it would be nice if I could incorporate that into the one single query as well.
For clarification, I know I could just do the query 10 times with copy and paste and it would be pretty quick, and I totally would if it were just a database that I used around my office. Unfortunately, this is an update I need to script and send out to all my customers with the next version release of our software; ergo, single query = less phone calls complaining about how nobody's database works since it didn't update correctly.
Thanks in advance for your consideration.
Use a cross join. You didn't say what version of SQL you were using, so I'll low-ball it:
INSERT INTO TableName( Column1, Column2, Column3, Column4, Column5, Column6)
(SELECT Column1, Column2, Column3, Column4, Column5, a.[sc]
FROM TableName as t
cross join (
select 'NewShopCode1' as [sc]
union
select 'NewShopCode2' as [sc]
union
select 'NewShopCode3' as [sc]
) as a
WHERE Column5 = 'DistinguishingValue'
and not exists (select 1 from TableName as t where t.Column1 = a.Column1 and t.Column2 = a.Column2)
)
Try the select by itself to see that it produces the correct results before inserting.

SQL Server : creating Temp Table with dynamic column

I having a problem with my current situation, I searched through any solution on the net but still I can't get it.
Here the question:
I have a bunch of SQL statements that need to be executed in a stored procedure by using cursor inside, each of the statements is performing an insert by selecting from a different database as well as table.
For example:
INSERT INTO Database1.Table1(column1, column2, column3)
SELECT column1,column2, column3
FROM Database2.Table2
WHERE --Some Condition
and maybe another executed SQL statement is like this
INSERT INTO Database1.Table3(column1, column2, column3, column4)
SELECT column1, column2, column3, column4
FROM Database3.Table3
WHERE --Some Condition
Okay, basically my process is like this
Execute Sql to insert into temp Tables --> Insert into a permanent Table from Temp Tables
From above two SQL statements, my executed result from database2 or 3 or may be 4,5 and etc. I will goes to my database1 for permanent store. In more summary way of telling that's is, I just want to made another copy of data that get from different source of database and store into my local database to do some further processing.
My main problem is my person in charge (or manager) told me to trow all executed result into a TEMP table or #Table before execute into the permanent.
Something like this:
INSERT INTO #Table3(column1, column2, column3, column4)
SELECT column1, column2, column3, column4
FROM Database3.Table3
WHERE --Some Condition
I went through some research on #Temp tables and I found it most of them is creating with 'FIX' column such as
CREATE TABLE #Table
(
column1 VARCHAR(10),
column2 VARCHAR(10),
column3 VARCHAR(10)
)
PROBLEM: is there anyway to create it with dynamic columns? More detail way of asking, is there anyway to insert by select into a #Temp table without prefix the column? Because that is impossible for me to create a bunch of temp table for each of executed SQL.
Thank you
PS 1: I am quite a newbie to SQL Server, please don't hesitate to voice out my mistake or error. We all learn from mistakes.
PS 2: Sorry for my poor English level, I tried my best to elaborate it more clearly.
Regards:
LiangCk
You say that you want something like
INSERT INTO #Table3(coloum1,coloum2,coloum3,coloum4)
Select coloum1,coloum2,coloum3,coloum4
FROM Database3.Table3
WHERE --Some Condition
without having to create the temp table with fixed columns first.
This would work:
Select coloum1,coloum2,coloum3,coloum4
INTO #Table3
FROM Database3.Table3
WHERE --Some Condition
You don't have to create the temp table or specify the columns first, just select into the temp table and it will be created on the fly.
It sounds like you want to do something more than this... I can't quite figure out what that is, but it sounds like maybe selecting all of your data from various tables into a single temp table (I don't know why you would want to do this)... If that's the case then UNION or UNION ALL should work. You can still use these with the dynamically created temp table.
Select coloum1,coloum2,coloum3,coloum4
INTO #Table3
FROM Database3.Table3
WHERE --Some Condition
UNION
Select column1, column2, column3, null
FROM Database1.Table1
WHERE --Some condition
The null above is just to give the 2nd select the same number of columns as the first (I used both selects from your post); this is a requirement for UNION.

Resources