How to insert an empty line to SQL table? [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inserting rows into a table with one IDENTITY column only
I have a SQL table with just one column. Column is an autoincrement field like:
Id INT IDENTITY
I need to insert a rows to this table (this is a bulk/dummy table temporarily):
INSERT INTO my_table VALUES ()
But I'm got a syntax error.
How can I do this?

INSERT INTO my_table DEFAULT VALUES

Try to insert explicit null values:
INSERT INTO my_table VALUES (NULL, NULL);

Related

TSQL get identity column value for the inserted rows without using cursor [duplicate]

This question already has answers here:
Sql Server return the value of identity column after insert statement
(7 answers)
How to get the identity of an inserted row?
(15 answers)
Closed 28 days ago.
I have a result set like below, RefId column will be empty initially. I need to loop through this result set insert values into one table ex : TableA , get the inserted row's identity (primary key) column value of the inserted rows and do some more manipulations.
I tried with cursors, since they are not recommended to use, how else can I do this in tsql ?
Result set query :
Insert into TableA(column1,column2,column3)
select column1, column2, column3 from Table B -- I want the identity values of the rows inserted into TableA,
I need them in another result set/ temp table or table variable along with the source values (TableB's select list values)

SQL - Insert only explicit values and not repeated ones [duplicate]

This question already has answers here:
Sql insert multiple rows if not exists
(7 answers)
Closed 10 months ago.
I have a SQL script with about 250 entries that are being inserted. The script on its own runs fine however, I run into problems in case of duplicate entries. I could try doing something like this:
IF NOT EXISTS (SELECT * FROM [TS].[Configs] WHERE [Id] = 15)
INSERT . . .
However, I cannot write this repeated statement over and over for the 250 entries. I have tried setting Identity_Insert to ON but I still get this error:
Exception Message: Violation of PRIMARY KEY constraint 'Tbl_LangFiles'. Cannot insert duplicate key in object '[TS].[Configs]'
How can I go about only inserting unique entries in this table?
Have you tried the UPSERT statement?
UPSERT INTO employees (id, name, email)
VALUES (2, ‘Dennis’, ‘dennisp#weyland.corp’);
This will attempt to insert a new record on the database, but if it already exists, it will take the parameters and update.
L.

PostgreSQL insert or return id [duplicate]

This question already has answers here:
Get or else insert in PostgreSQL
(1 answer)
Function to select existing value or insert new row
(2 answers)
Doing either SELECT or INSERT
(2 answers)
Closed 1 year ago.
I'm trying to insert or return id of existing value in case of uniqueness conflict (the data is already there).
I'm guessing it could be something like this, but can't quite get it to return the actual ID
INSERT INTO my_table (my_column) VALUES ('my value')
ON CONFLICT DO NOTHING
RETURNING ID;
where my_column has a unique constraint.
Any ideas?

PostgresSql error: more than one row returned by a subquery used as an expression [duplicate]

This question already has answers here:
Insert into ... values ( SELECT ... FROM ... )
(27 answers)
Closed 3 years ago.
i am running this query where in rel_branding there are more than 1 rows and i want to insert brandingid of every record in rel_branding_permission.
This is for PostgresSQL.
insert into rel_branding_permission (brandingid,permissionid)
values((select brandingid from rel_branding), 404);
I want to insert this query in my DB as for all branding ids
You should be using an INSERT INTO ... SELECT here, something like this:
INSERT INTO rel_branding_permission (brandingid)
SELECT brandingid
FROM rel_branding
WHERE <some condition>
You would probably want to include more than one column in the insert into the rel_branding_permission table, and you may modify the above to fit your need by adding the additional columns to both the INSERT and the SELECT.

How to use more than one table with OUTPUT clause? [duplicate]

This question already has an answer here:
Insert Into... Merge... Select (SQL Server)
(1 answer)
Closed 6 years ago.
Is it possible to insert columns of different tables in the OUTPUT clause of an INSERT statement, such as:
DECLARE #insertedrecords TABLE (Id int, [Guid] uniqueidentifier);
INSERT INTO mytable
(column names here...)
OUTPUT inserted.id_no, b.[Guid] INTO #insertedrecords
SELECT
column names here...
FROM #myTVP b
Currently, using the above I get the following error:
The multi-part identifier "b.Guid" could not be bound.
Inserted special table will hold the records those are inserted into the Target table not all the data from Source table. When a column from target table is not part of Insert list then in OUTPUT clause it will be NULL
So you need two different Inserts
DECLARE #insertedrecords TABLE (Id int, [Guid] uniqueidentifier);
INSERT INTO mytable
(column names here...)
SELECT
column names here...
FROM #myTVP b
Insert into #insertedrecords(Id,Guid)
select id_no, [Guid]
From #myTVP