In sql server enterprise manager, how do you write an insert statement and pass in null values?
INSERT INTO atable (x,y,z) VALUES ( NULL,NULL,NULL)
If you're using SSMS (or old school Enterprise Manager) to edit the table directly, press CTRL+0 to add a null.
Related
(Came up with this question in the course of trying to answer this other one)
Consider the following MS-SQL table, called GroupTable:
GroupID
-------
1
2
3
where GroupID is the primary key and is an Identity column.
How do you insert a new row into the table (and hence generate a new ID) without using IDENTITY_INSERT ON?
Note that this:
INSERT INTO GroupTable() Values ()
... won't work.
edit: we're talking SQL 2005 or SQL 2008 here.
This should work:
INSERT INTO GroupTable DEFAULT VALUES
Here you go:
INSERT INTO GroupTable DEFAULT VALUES
It is possible to insert more than one row at a time.
For e.g., to insert 30 rows.
INSERT INTO GroupTable DEFAULT VALUES
GO 30
This will insert 30 rows by incrementing the identity column each time.
Can you try using a Sequence or something similar? Where you select from a Sequence and it will give you the next value in the sequence.
This will work actually--
insert into TABLE default values
I have the following SQL statement:
insert into [Order](UserId,MembershipType,PaymentAmt,PaymentStatus,StartDate,EndDate,Status)output INSERTED.OrderId values('18','Yearly','9.99','','2017-02-14 15:13:22','2018-02-14 15:13:22','1');Select Scope_Identity()
Which will insert the data in to my dbo.Order table in my Microsoft SQL Server 2014 database locally on my PC.
But when I run this query on a live 3rd party web server (With an identical database table) I get the following error:
exception=Cannot insert the value NULL into column 'OrderId', table 'db1128212_MYDATABASE.dbo.Order'; column does not allow nulls. INSERT fails.
Why is this?
Set the OrderId column on the remote table to identity/auto increment.
Auto increment primary key in SQL Server Management Studio 2012
I'm adding data into a table in my database from a VB application. But, I need to know, is it possible that some of the data I added can be imported to another table?
example:
table1(ID,FisrtName,LastName) data are added completely
table 2 specific (FirstName, LastName)
insert into table2 (FirstName,LastName) select FirstName,LastName from table1;
Use a Trigger AFTER INSERT on the table 1 and the thing is done automatically after inserting the data on table 1 or if the data has already been inserted then use a select insert statement like.
INSERT INTO dbo.Table2(FirstName,LastName)
SELECT T.FirstName,T.LastName FROM dbo.Table1 AS T
And that should be it.
How should my SQL-Statement (MS-SQL) look like if I wan't to insert a row into a table, which contains only one column with the autogenerated id?
Following two queries won't work:
INSERT INTO MyTable (MyTableId) VALUES (Null)
-- or simply
INSERT INTO MyTable
Thx for any tipps.
sl3dg3
INSERT INTO MyTable (MyTableId) VALUES (Null) implicitly tries to insert a null into the identity column, so that won't work as identity columns may never be nullable and without a SET option you cannot insert an arbitrary value anyway, instead you can use:
INSERT INTO MyTable DEFAULT VALUES
For completeness' sake, the SQL standard and most databases support the DEFAULT VALUES clause for this:
INSERT INTO MyTable DEFAULT VALUES;
This is supported in
CUBRID
Firebird
H2
HSQLDB
Ingres
PostgreSQL
SQLite
SQL Server (your database)
Sybase SQL Anywhere
If the above is not supported, you can still write this statement as a workaround:
INSERT INTO MyTable (MyTableId) VALUES (DEFAULT);
This will then also work with:
Access
DB2
MariaDB
MySQL
Oracle
For more details, see this blog post here:
http://blog.jooq.org/2014/01/08/lesser-known-sql-features-default-values/
I'm a long time desktop app C++ programmer new to SQL. I need to insert into a table with an autoincrment field, and have that sql statement return the new value for the autoincrement field.
Something LIKE:
INSERT INTO Entrys ('Name','Description')
VALUES ('abc','xyz')
SELECT Entrys.EntryID WHERE EntryID=[THE ONE JUST INSERTED!]
Sorry for being a noob.
Assuming that you're using SQL Server, you can use scope_identity to return "the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch."
INSERT INTO Entrys ('Name','Description') VALUES ('abc','xyz');
SELECT SCOPE_IDENTITY()
select scope_identity
insert into table values ('string value');select scope_identity()
Details here
In SQL Server, you can also use the OUTPUT clause on your INSERT statement:
INSERT INTO Entrys('Name', 'Description')
OUTPUT Inserted.EntryID
VALUES ('abc','xyz')
This will output the newly created IDENTITY field value.
In MySQL you can use LAST_INSERT_ID()
As #marc_s said for SQL Server, in PostgreSQL you can obtain the same behaviour with:
INSERT INTO Entrys ('Name','Description')
VALUES ('abc','xyz')
RETURNING EntryID;
RETURNING is also really useful when you're inserting several tuples and you want all the generated IDs for each of the rows.