INSERT to a table without specifying column names

I have INVOICE TABLE I want to insert a value without specifying column names using SQL Server , I tried this, but it does not work. Please, help

 INSERT INTO INVOICE VALUES( 1,1,KEYBOARD,1,15,5,75) 
+7
source share
4 answers

As long as you have the correct number of columns in the INSERT statement and as long as all values ​​except KEYBOARD are numeric data types, and as long as you have the appropriate permissions, this should work.

 INSERT INTO INVOICE VALUES( 1,1,'KEYBOARD',1,15,5,75); 

SQL requires single quotes around text values.

But using column names is not good practice. This is not to say that people change the order of columns in a table. Reordering columns is also not good practice, but some people insist on it anyway.

If someone does this and changes the 5th and 7th columns in your table, your INSERT statement will still succeed - both of these columns are numeric, but INSERT will corrupt your data.

+18
source

Why do you need this? Not specifying column names is a bad coding practice.

In your case, the keyboard should be surrounded by single quotes:

 INSERT INTO INVOICE VALUES( 1,1, 'KEYBOARD',1,15,5,75) 

If you just don't want to enter column names, you can easily get them in SQL Server Management Studio. Open Object Browser, open the database, and select Tables. Select the Invoice table. One option is Columns.

Just click on the name "Columns" (no need to open it) and drag it into the query window. It will insert a list of columns.

+8
source

yes, you can directly insert values ​​into the table as follows:

 insert into `schema`.`tablename` values(val1,val2,val3); 
+2
source
 INSERT INTO INVOICE VALUES( 1,1,'KEYBOARD',1,15,5,75); 

you forget to include single quotes in the keyboard, the text requires single quotes in sql

+1
source

All Articles