INSERT into table without specifying Column names

前端 未结 4 1477
鱼传尺愫
鱼传尺愫 2020-12-15 05:16

I have INVOICE TABLE I want to insert value by not specifying column names using SQL Server, I have tried this but it is not working..

相关标签:
4条回答
  • 2020-12-15 05:53

    Why would you want to do this? Not specifying column names is bad coding practice.

    In your case, though, keyboard needs to be surrounded by single quotes:

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

    If you just don't want to type the column names, you can get them easily in SQL Server Management Studio. Open the "Object Browser", open the database, and choose "Tables". Choose the Invoice table. One of the options is "Columns".

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

    0 讨论(0)
  • 2020-12-15 06:00

    yes you can directly insert values into table as follows:

    insert into `schema`.`tablename` values(val1,val2,val3);
    
    0 讨论(0)
  • 2020-12-15 06:04
    INSERT INTO INVOICE VALUES( 1,1,'KEYBOARD',1,15,5,75);
    

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

    0 讨论(0)
  • 2020-12-15 06:07

    As long as you have the right number of columns in your INSERT statement, and as long as all the values except KEYBOARD are some numeric data type, and as long as you have suitable permissions, this should work.

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

    SQL requires single quotes around text values.

    But not using column names isn't a good practice. It's not unheard of for people to change the order of columns in a table. Changing the order of columns isn't a good practice, either, but some people insist on doing it anyway.

    If somebody does that, and swaps the 5th and 7th columns in your table, your INSERT statement will still succeed--both those columns are numeric--but the INSERT will screw up your data.

    0 讨论(0)
提交回复
热议问题