vbscript/sql adding a column to an insert statement - sql

I am trying to add an additional column and value to an existing insert query - both integers, and running into trouble.
Anything to look out for?

you don't give much to go in in your question:
I am trying to add an additional
column and value to an existing insert
query - both integers, and running
into trouble.
Anything to look out for?
it is best practice to list all columns you intend to include values for in the list of columns, so make sure you add them there, as well as the VALUES list:
insert into YourTable (col1, col2,..., newCol1, newCol2)
VALUES (1,2,...,new1, new2)
make sure the you get the column names spelled correct and that the table actually has those new columns in it.

make sure the column name sequence is the same as your insert data sequence.
Example
INSERT INTO TABLENAME
(ColumnName1,ColumnName2) VALUES (1,'data')
Becomes
INSERT INTO TABLENAME
(ColumnName1,ColumnName2,ColumnNameNEW) VALUES (1,'data','newcolumndata')
Notice both the new column name and the new data are in the third position in the sequence.

Related

How to delete specific column values in multiple "insert into" query

Sorry if you think my question is too basic, because i'm a newbie.
Lets say i have 1000 lines of query like this
INSERT INTO table (column1,column2,column3) VALUES (1,2,3);
INSERT INTO table (column1,column2,column3) VALUES (3,33,333);
....
I want to delete column2 with its value, Is there any way so that i dont need to delete 1000 rows manualy. Thanks.
Perhaps you want to completely drop the second column:
ALTER TABLE yourTable DROP COLUMN column2;
Although you may have used many statements to build up the data in your table, dropping a particular column only requires a one-liner.

Need Help using a loop to perform a mass insert in SQL

First off, i should say up front that i am not a very strong SQL person, so please be gentle :)
I need to perform about 400 inserts into a particular table. The data that i will be using for these inserts, i can collect from a SELECT statement that runs off a different table. I only need the data from 1 column from this table.
So, im hoping someone can help me write the SQL that will basically take the list of id's that are returned from my select, and use that list to do a mass insert into another table.
In psuedocode, something like this:
Select BankID from BankTable; - this returns 300 rows
Insert Into AccountTable -- this will add all 300 rows into the 2nd table
Values
(BankID)
thanks in advance guys...
Very simple, you basically said it. :-)
INSERT Into AccountTable (BankId, SecondColumn) SELECT BankId,'XXX' as staticText FROM BankTable;
It can be done in one statement:
Insert Into AccountTable (bankid)
Select BankID from BankTable
This assumes that the column in AccountTable is also named bankid; if not, just set the name appropriately in the parenthesis.
Keep in mind your INSERT statement must include the columns if the select statement does not match your table definition precisely.
INSERT AccountTable (BankID)
SELECT BankID
FROM BankTable
For multiple columns simply include the column list:
INSERT AccountTable (BankID, BankName)
SELECT BankID, BankName
FROM BankTable
You can also run into type conversion issues if the data types of the columns don't match (i.e. integer fields won't take alpha characters, etc.), but it is not a necessity that the column names match. Just get the order of columns and types right and you should be good.

INSERT into table without specifying Column names

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..please help
INSERT INTO INVOICE
VALUES( 1,1,KEYBOARD,1,15,5,75)
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.
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.
yes you can directly insert values into table as follows:
insert into `schema`.`tablename` values(val1,val2,val3);
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

Can't INSERT INTO SELECT into a table with identity column

In SQL server, I'm using a table variable and when done manipulating it I want to insert its values into a real table that has an identity column which is also the PK.
The table variable I'm making has two columns; the physical table has four, the first of which is the identity column, an integer IK. The data types for the columns I want to insert are the same as the target columns' data types.
INSERT INTO [dbo].[Message] ([Name], [Type])
SELECT DISTINCT [Code],[MessageType]
FROM #TempTableVariable
This fails with:
Cannot insert duplicate key row in object 'dbo.Message' with unique index
'IX_Message_Id'. The duplicate key value is (ApplicationSelection).
But when trying to insert just Values (...) it works ok.
How do I get it right?
It appears that the data "ApplicationSelection" is already in the database. YOu need to write the select to exclude records that are already in the database. YOu can do that with a where not exists clause or a left join. LOok up teh index to see what field is unique besides the identity. That will tell you what feild you need to check to see if teh record currently exists.

Adding auto-incremented values to a table with one column

I need to create a table that basically keeps a list of indices only. Therefore I've created a table with just one, auto-incremented column called 'id'. However, I can't seem to implicitly add auto-incremented values to this table.
I know that usually when you have such a column in a table (with more than just this column) you can do:
INSERT INTO TABLE (col1, col2 ...) VALUES (val1, val2 ...)
And if you don't specify the auto-incremented column, it would automatically get a value. However, things like:
INSERT INTO TABLE () VALUES ()
INSERT INTO TABLE
INSERT INTO TABLE ()
etc. all produce an error on my single-columned table. Can anyone offer a solution?
Thanks.
p.s. I'm using Sqlite, in case it matters.
Try this
INSERT INTO dbo.Table DEFAULT VALUES
See this answer:
Previous answer
Try the following:
INSERT INTO YOUR_TABLE(YOUR_ID) VALUES (NULL);