Sql Query Create a new Table - sql

Can anyone offered syntax for a sql query that takes an already created table and inserts some of the data into a new table with a new id(primary key)? So far I tried this
SELECT ((ID INT IDENTITY(1,1) PRIMARY KEY), column1, column2, column3)
INTO table_name
FROM table_name
I keep coming up with a syntax error.

SELECT column1, column2, column3
INTO table_name_new
FROM table_name_old
use like this

INSERT INTO newtable (column1, column2, column3)
SELECT (column1, column2, column3)
FROM table_name

INSERT INTO new_table
(Column1, Column2 . . . )
SELECT Column1, Column2 FROM old_table
Don't include the identity column (the primary key with the auto-generated numbers) in the insert list.

Just make it like below, Aa new id will be automatically created while inserting the record
insert into table1 (col1,col2,col3) select col1,col2,col3 from table2;

According to the documentation on MSDN, the IDENTITY property will transfer through with a SELECT INTO clause, but constraints will not. That means you can create a new table with an IDENTITY column if you include the original IDENTITY column from your source table, but the column in the new table won't be a primary key.
The sane way to do this is to use ALTER TABLE after the table is created, as #Kai has suggested.

Related

Get columnnames as string sepparated by comma / list

i am doing some inserts with some of my tables and all fields but a few need to be inserted or updated. Like here.
Is there a quick way to get the columns of a table returned as a string so i can copy them into my sql query?
For example i have a table:
create table myTable1
(
column1 integer not null,
column2 integer not null ,
column3 integer not null,
column4 integer not null,
column5 integer not null,
...a lot more fields...
)
And later i want to insert something from another table myTable2 that has the same fields (dont question my ways and why i have two identical tables).
INSERT INTO myTable SELECT column1, column2, column3, column4, ...
FROM table_source
But the tables have so many fields it is cumbersome to write them donw manualy and it would be faster to have a string where i can just delete the column names i dont need. Is there a nice query that ouputs "column1, column2, column3, column4, ..." so i dont have to write that myself and can copy tha into my query?
Found the answer quickly.
SELECT table_catalog, string_agg(column_name, ', ')
FROM information_schema.columns
WHERE table_schema = 'mySchema'
AND table_name = 'myTable' GROUP BY 1;
This query does the trick for me.

How to skip exception raised in INSERT statement?

When I run the following SQL code:
INSERT INTO table
(column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
ON CONFLICT DO NOTHING;
I get
PG::CheckViolation: ERROR:
new row for relation "table" violates
check constraint "tables_column1_valid_first_char"
How can I skip rows violating that check constraint?
I tried
INSERT INTO table
(column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
ON CONFLICT ON CONSTRAINT tables_column1_valid_first_char DO NOTHING;
But it fails with
PG::WrongObjectType: ERROR:
constraint in ON CONFLICT clause has no associated index
The constraint looks as following:
ADD CONSTRAINT tables_column1_valid_first_char
CHECK (SUBSTRING(text, 1,1) ~* '^[a-zàâçéèêëîïôûùüÿæœ]');
ON CONFLICT only deals with unique constraints, it doesn't work for check constraints (or foreign key constraints).
You need to exclude those rows by applying a WHERE clause:
INSERT INTO table (column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
and SUBSTRING(column1, 1,1) ~* '^[a-zàâçéèêëîïôûùüÿæœ]'
ON CONFLICT DO NOTHING;

MS SQL output inserted with insert from select

I'm trying to figure out how to combine INSERT FROM SELECT and returning id value of inserted record.
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
OUTPUT inserted.ID (SELECT TOP 1 590675, column2, column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817)
If you are trying to get the new ID after your INSERT statement you can use SCOPE_IDENTITY, IDENT_CURRENT or ##IDENTITY. For example:
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
SELECT TOP 1
590675,
column2,
column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817
ORDER BY ...
SELECT SCOPE_IDENTITY(); -- Last identity generated in current session and current scope
SELECT ##IDENTITY; -- Last identity generated in current session across all scopes
SELECT IDENT_CURRENT([someDB].[dbo].[OBJ]) -- Last identity generated for the given table in any session and any scope
Because you are inserting just one row, SCOPE_IDENTIY() would be the best approach.
Hope it helps.
Your syntax is a little wrong here.
You want:
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
OUTPUT inserted.ID
SELECT TOP 1
590675,
column2,
column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817
ORDER BY ...?; --You have a TOP 1, thus you really need an ORDER BY as well.

INSERT INTO subtract 2 values

Is it possible for an INSERT query to subtract 2 values you have entered to create a 3rd value that can then be inserted into a table - if that makes sense...
e.g.
INSERT INTO table1 (column1, column2, column3)
VALUES ('50', '25', column1 - column2)
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', column1 - column2) from table1 where conditions)
This is a sample query! hope it helps!
Convoluted:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
(select 50 as column1,
25 as column2
) t
Since you can't reference other columns from the same SELECT clause, you have to do it as a subquery. I've also switched to using int literals rather than strings, because I can't make subtraction make sense in my head otherwise.
You could also do it using a Table Value Constructor:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
( VALUES (50, 25)
) AS t (column1, column2);
As indicated in my comment though, if the relationship should always hold, I'd build table1 as:
CREATE TABLE table1 (
column1 int not null,
column2 int not null,
column3 as column1 - column2
--More columns
)
Because that way, the column3 value is always correct.
You can create function that subtracts values and use this function in insert. This is the right way to do such things:
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', your_function() ) from table1 where conditions)
/
Using the "INSERT INTO" would do this:
INSERT INTO Table1Name (column1, column2, column3,)
(select 'X', 'Y', X - Y as Z)
Here is a link to SQL Authority with more examples of INSERT INTO
Another method would to be add a trigger to the table, where on insert of data, the third column would be updated with the difference of the first two columns.

insert into with select + new information

I'm trying to write a sql query that can copy specific columns from a table and insert it in the same table + extra information for the other columns
Simply copying certain information would be something like this:
INSERT INTO table (column1, column2)
SELECT column1, column2
FROM table
WHERE columnx = 'some value'
But I need to also insert some new information in column3. How can I do that?
I have the information that will go in "column3", I don't have to get it from an other table or source.
This is for a repeat appointment where basically all the information is the same except for date, planner and appointment_id.
If you know what the values are . . .
INSERT INTO table (column1, column2, column3)
SELECT column1, column2, <value for column3>
FROM table
WHERE columnx = 'some value'